Mastering Environments in GitLab CI/CD: Static vs. Dynamic
Environments in GitLab CI/CD are essential for managing different stages of your application lifecycle. They allow you to deploy your application in various contexts, such as development, testing, staging, and production. By utilizing environments, you can streamline your deployment process and ensure that your code is tested in conditions that closely resemble production.
You can create static environments either through the GitLab UI or directly in your .gitlab-ci.yml file. For static environments, define a job in the deploy stage, specifying the environment name and URL. If the environment doesn’t exist when the pipeline runs, it will be created. For example, you might have a job like this:
1deploy_staging:
2 stage: deploy
3 script:
4 - echo "Deploy to staging server"
5 environment:
6 name: staging
7 url: https://staging.example.comDynamic environments, on the other hand, are typically created for review apps and are used for single deployments. They feature dynamic names based on CI/CD variables, making them flexible for testing branches or features. An example of a dynamic environment setup is:
1deploy_review_app:
2 stage: deploy
3 script:
4 make deploy
5 environment:
6 name: review/$CI_COMMIT_REF_SLUG
7 url: https://$CI_ENVIRONMENT_SLUG.example.com
8 rules:
9 - if: $CI_COMMIT_BRANCH == "main"
10 when: never
11 - if: $CI_COMMIT_BRANCHIn production, keep in mind that the state of an environment can be available, stopping, or stopped, depending on whether its stop job has run. Be cautious with dynamic environments; if the environment URL is invalid, it won’t update correctly. Also, remember that the stop_review job doesn’t generate a dotenv report artifact, which can lead to confusion if you try to reference environment variables that don’t exist in that context. Always ensure your scripts are accessible to the runners, especially when dealing with feature branches that may be deleted.
Key takeaways
- →Create static environments by defining jobs in your `.gitlab-ci.yml` file with specific names and URLs.
- →Utilize dynamic environments for review apps, leveraging CI/CD variables for naming.
- →Be aware of environment states: available, stopping, and stopped, and how they affect your deployments.
- →Ensure valid environment URLs to avoid issues with updates.
- →Remember that `stop_review` jobs do not generate dotenv report artifacts.
Why it matters
Properly managing environments can significantly reduce deployment errors and streamline your CI/CD pipeline. This leads to faster feedback loops and more reliable releases.
Code examples
1deploy_staging:
2 stage: deploy
3 script:
4 - echo "Deploy to staging server"
5 environment:
6 name: staging
7 url: https://staging.example.com1deploy_review_app:
2 stage: deploy
3 script:
4 make deploy
5 environment:
6 name: review/$CI_COMMIT_REF_SLUG
7 url: https://$CI_ENVIRONMENT_SLUG.example.com
8 rules:
9 - if: $CI_COMMIT_BRANCH == "main"
10 when: never
11 - if: $CI_COMMIT_BRANCH1review:
2 script:
3 - DYNAMIC_ENVIRONMENT_URL=$(deploy-script)
4 - echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
5 artifacts:
6 reports:
7 dotenv: deploy.env
8 environment:
9 name: review/$CI_COMMIT_REF_SLUG
10 url: $DYNAMIC_ENVIRONMENT_URL
11 on_stop: stop_reviewWhen NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsDeploy any app in seconds — no infrastructure config, no DevOps overhead. Instant deployments from GitHub, built-in databases, and automatic scaling.
Start deploying free →Mastering GitLab CI Runners: Timeout Configurations You Need to Know
GitLab CI runners are essential for managing job execution, but improper timeout settings can lead to wasted resources. Learn how to configure maximum timeouts and script execution limits effectively.
Maximizing GitLab CI Pipeline Efficiency: Strategies That Work
Pipeline efficiency is crucial for speeding up your CI/CD processes. By analyzing job workloads and leveraging caching, you can significantly reduce pipeline duration. Discover how to optimize your GitLab CI pipelines effectively.
Mastering GitLab CI/CD YAML: Essential Syntax for Production
Get your pipelines running smoothly with GitLab CI/CD YAML syntax. Understand how to leverage global and job keywords to configure your pipeline effectively. This article dives into the specifics that matter in production environments.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.