Ravindra BagaleCourses & study guides मराठी

Chapter 92: GitLab CI Deep Dive

DevOps

92.11 Environments, manual approval, protected environments and resource_group

Story (fictional teaching scenario). At a fictional pharmacy chain in Mysuru, two production deploys once ran at the same time and left mixed versions on the servers. Clara from Vienna made three changes. Deploys go to named environments, so GitLab shows what is live where. The production job is manual, so a person clicks Run. And a resource_group lets only one production deploy run at a time; the second one waits.

Deploy jobs name an environment. Production is manual and protected. resource_group runs one deploy at a time.
Deploy jobs name an environment. Production is manual and protected. resource_group runs one deploy at a time.

Why

Production needs a record, a human decision and no races.

What

  • environment — A named deploy target, like staging or production. GitLab lists deployments per environment.
  • when: manual — The job waits for someone to click Run.
  • Protected environment — Only allowed users or groups can deploy to it. Required approvals for deployments are a Premium/Ultimate feature (check your tier in the GitLab docs).
  • resource_group — Jobs with the same resource group never run at the same time.
  • allow_failure: false — On a manual job, makes the pipeline wait (blocked) until it is run.

How

  1. Add environment: to every deploy job.
  2. Make production when: manual with a default-branch rule.
  3. Protect the production environment in Settings → CI/CD → Protected environments (Operate settings vary by version).
  4. Add resource_group: production.

Example 1: Staging then manual production

deploy-staging:
  stage: deploy
  script: ["echo deploy to staging"]
  environment:
    name: staging
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

deploy-prod:
  stage: deploy
  needs: ["deploy-staging"]
  script: ["echo deploy to production"]
  environment:
    name: production
  resource_group: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
      allow_failure: false

Line by line: Staging deploys automatically on main. Production needs staging first, is tied to the production environment, can only run one at a time because of resource_group, and waits for a person because of when: manual. allow_failure: false keeps the pipeline blocked until someone decides.

Output (illustrative — pipeline not run)

Pipeline #1088  blocked
  deploy-staging  passed
  deploy-prod     manual  ▶ (waiting for a person)
...
deploy-prod: Waiting for resource: production   (another deploy is running)

What it means: The pipeline paused for a human. A second production deploy queued instead of racing the first.

Ravindra Bagale's Tip

Put resource_group on every production deploy job. It is one line and it prevents the worst kind of mixed-version outage.

Did you understand? Let us try it once more.