7. Environments & Deployments

Deploying code is only half the battle. Once your code is running in a live environment, you need to track which version is currently active on which server, monitor performance, and have the ability to execute one-click rollbacks if a release goes wrong. GitLab Environments provide a visual control room to monitor and track your application server environments.

Configuring Environments in Pipelines

You can tell GitLab to track deployments to specific servers by using the environment: keyword inside a job description. This maps the output of the job to an environment console in the GitLab web UI.

deploy_to_production:
  stage: deploy
  script:
    - scp -r ./build/* user@prod-server.com:/var/www/html
  environment:
    name: production
    url: https://my-app.com
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Manual Gates and Approvals

For safety, organizations rarely deploy directly to production automatically. Instead, they require a manual validation trigger. In GitLab, you implement this using the when: manual keyword. The pipeline will halt at this stage, presenting a "Play" button in the dashboard for authorized developers or release managers to click manually.

deploy_to_production:
  stage: deploy
  script:
    - echo "Deploying..."
  environment:
    name: production
  when: manual
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Dynamic Environments & Review Apps

One of GitLab's most revolutionary features is Review Apps. Review Apps spin up a unique, temporary, dynamic environment for every branch you create. This allows testers, designers, and managers to view code changes live inside a browser before the code is ever merged into `main`!

review_app:
  stage: deploy
  script:
    - spin-up-k8s-pod --name "review-$CI_COMMIT_REF_SLUG" --build "./build/"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://review-$CI_COMMIT_REF_SLUG.test-domain.com
    on_stop: stop_review_app

stop_review_app:
  stage: deploy
  script:
    - tear-down-k8s-pod --name "review-$CI_COMMIT_REF_SLUG"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  when: manual

One-Click Rollbacks

If a deployment is faulty and crashes in production, you can navigate to Operate > Environments in the GitLab sidebar. Here, you see a full audit log of every deployment. Next to previous working runs, you'll see a Rollback button. Clicking this will automatically trigger the deployment job with the previous successful commit's code, instantly restoring your service!

GitLab Releases

A Release is a snapshot of your code, compiled binaries, release notes, and tags. You can automate the creation of Releases within your pipeline using the official release-cli tool, linking your source code to production-ready binaries automatically.

DevOps Best Practice: Combine Review Apps with your merge requests! By giving reviewers a direct live web link to test modifications side-by-side with the code diffs, code reviews become 3x faster and significantly more comprehensive.