TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Environments & Manual Deployments

Environments & Manual Deployments

In GitLab, a job that deploys declares an environment. GitLab then records every deployment to it (which commit, which pipeline, who started it), shows the environment's current version, offers a one-click rollback to an earlier deployment, and, with protected environments, controls who may deploy at all.

The keywords

Keyword Meaning
environment: name the environment this job deploys to; created on first use
environment: url a link shown in merge requests and the environments page
environment: deployment_tier production, staging, testing, development or other (guessed from the name if omitted)
when: manual the job waits for someone to press "Run": continuous delivery's gate
resource_group at most one job of the group runs at a time across pipelines: no two deployments to production at once
environment: on_stop / action: stop the job that tears down a temporary (review) environment
environment: auto_stop_in stop a review environment automatically after a period

A staging → production pipeline

The build job publishes the image digest through a dotenv report, so both deployments receive $IMAGE_DIGEST; production is manual and only exists on main:

bash · try it yourself
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main

cat > .gitlab-ci.yml <<'YML'
stages: [build, staging, production]

build:
  stage: build
  script:
    - echo "IMAGE_DIGEST=sha256:4d9e9b0ad0be" > build.env   # in reality: the digest of the pushed image
  artifacts:
    reports:
      dotenv: build.env

deploy-staging:
  stage: staging
  environment:
    name: staging
    url: https://staging.shop.example.com
  resource_group: staging
  script:
    - echo "deploy $IMAGE_DIGEST to $CI_ENVIRONMENT_NAME ($CI_ENVIRONMENT_URL)"

deploy-production:
  stage: production
  environment:
    name: production
    url: https://shop.example.com
  resource_group: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
  script:
    - echo "deploy $IMAGE_DIGEST to $CI_ENVIRONMENT_NAME"
YML
git add . && git commit -qm "ci: environments"
echo "### push to main: production waits"
gitlab-ci-local --concurrency 1 2>&1 | grep -E ' > |PASS|environment:'
echo "### someone presses Run on deploy-production"
gitlab-ci-local --concurrency 1 --manual deploy-production 2>&1 | grep -E ' > |PASS'
### push to main: production waits
build             $ echo "IMAGE_DIGEST=sha256:4d9e9b0ad0be" > build.env
deploy-staging    > deploy sha256:4d9e9b0ad0be to staging (https://staging.shop.example.com)
 PASS  build
 PASS  deploy-staging
deploy-staging environment: { name: staging, url: https://staging.shop.example.com }
### someone presses Run on deploy-production
build             $ echo "IMAGE_DIGEST=sha256:4d9e9b0ad0be" > build.env
deploy-staging    > deploy sha256:4d9e9b0ad0be to staging (https://staging.shop.example.com)
deploy-production > deploy sha256:4d9e9b0ad0be to production
 PASS  build
 PASS  deploy-staging
 PASS  deploy-production

On the first run, the pipeline stopped after staging: deploy-production is a manual job, so it waited (in GitLab, the pipeline shows a "play" button and the status blocked or passed depending on allow_failure). Passing --manual deploy-production plays the role of the person who clicks it; gitlab-ci-local runs a fresh pipeline to do so, while GitLab runs only the waiting job, inside the original pipeline, with the artifacts that pipeline already produced.

Who may press the button

when: manual alone lets anyone with Developer access run the job. Two settings make it a real control:

  • Protected environments (Settings → CI/CD → Protected environments): only listed users, groups or roles may deploy to production, and optionally required approvals (a number of approvers, with rules per group) must be given before the job can run.
  • Protected branches and protected variables: the production credentials are CI/CD variables marked protected (only exposed to pipelines on protected branches and tags) and scoped to the production environment (environment_scope). A pipeline on a feature branch never sees them.
yaml
# what a deploy job with protected, environment-scoped variables looks like (non-run: needs a cluster)
deploy-production:
  stage: production
  image: $HELM_IMAGE              # a Helm 4 image, pinned by digest in a CI/CD variable
  environment:
    name: production
    url: https://shop.example.com
  resource_group: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
  script:
    # KUBECONFIG_PROD is a protected file variable scoped to "production"
    - helm upgrade --install shop ./chart --namespace shop
        --kubeconfig "$KUBECONFIG_PROD"
        --set image.digest="$IMAGE_DIGEST" --wait --timeout 5m

Review apps

A review app is a temporary environment per merge request, so reviewers can click through the change. Its name contains the branch slug, and a stop job tears it down:

bash · try it yourself
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main

cat > .gitlab-ci.yml <<'YML'
review:
  stage: deploy
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    url: https://$CI_COMMIT_REF_SLUG.review.shop.example.com
    on_stop: stop-review
    auto_stop_in: 2 days
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
  script:
    - echo "deploy review app for $CI_COMMIT_REF_SLUG"

stop-review:
  stage: deploy
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    action: stop
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: manual
  script:
    - echo "delete review app for $CI_COMMIT_REF_SLUG"
YML
git add . && git commit -qm "ci: review apps"
git switch -q -c feature/Coupons_v2
gitlab-ci-local --list 2>&1 | tail -n +3
gitlab-ci-local --concurrency 1 review 2>&1 | grep ' > '
name         description  stage   when        allow_failure  environment                needs
review                    deploy  on_success  false          review/feature-coupons-v2
stop-review               deploy  manual      false          review/feature-coupons-v2
review > deploy review app for feature-coupons-v2

CI_COMMIT_REF_SLUG turned feature/Coupons_v2 into a DNS-safe name (lowercase, other characters replaced by -), which is why it's the standard building block for review-app hostnames. When the branch is merged or deleted, GitLab runs stop-review automatically; auto_stop_in catches the ones nobody cleans up.

Rollback

On the environment's page, every past deployment has a Rollback (re-deploy) button that re-runs that old deployment job, with that pipeline's variables and artifacts. This only works if the old artifact still exists (registry retention, expire_in) and the old job is still valid against today's infrastructure, so practise it (Rollback & Database Changes).

Test yourself

What prevents two pipelines from deploying to production at the same time?

  1. when: manual
  2. resource_group
  3. environment: url
  4. stages

How do you keep production credentials away from feature-branch pipelines?

  1. Put them in .gitlab-ci.yml
  2. Mark the variables protected and scope them to the production environment
  3. Use allow_failure
  4. Use a manual job

Why use $CI_COMMIT_REF_SLUG in review app names?

  1. It's shorter than the SHA
  2. It is a DNS- and URL-safe version of the branch name
  3. It's random
  4. It's required by GitLab