GitLab Tutorial
- 1. Introduction to GitLab
- 2. Architecture & Self-Hosting
- 3. Groups, Projects & Namespaces
- 4. Issue Tracking & Agile
- 5. GitLab CI/CD Basics
- 6. GitLab Runners & Executors
- 7. Environments & Deployments
- 8. Merge Requests & Code Review
- 9. GitLab Container Registry
- 10. Package & Infra Registry
- 11. Security & Compliance
- 12. Monitoring & Analytics (DORA)
- 13. GitLab Pages Hosting
- 14. APIs, Webhooks & Integrations
- 15. GitLab Workflow & Best Practices
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: manualOne-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.