OpsCanary
Back to daily brief
cicdgitlab ciPractitioner

Mastering Environments in GitLab CI/CD: Static vs. Dynamic

5 min read GitLab DocsApr 23, 2026
PractitionerHands-on experience recommended

Environments in GitLab CI/CD exist to help you manage different configurations and deploy code at various stages of your software lifecycle. They solve the problem of needing distinct settings for development, testing, and production without the hassle of manual configuration each time. By using environments, you can automate deployments and ensure that your code behaves consistently across different stages.

You can create two types of environments: static and dynamic. Static environments, like staging and production, have fixed names and can be created manually or as part of a CI/CD pipeline. Dynamic environments, on the other hand, are generated during the pipeline execution and are typically used for single deployments. They have names based on CI/CD variables, making them flexible for review apps. You can also set environment URLs and capture generated URLs as dotenv variables, which is particularly useful for dynamic environments.

In production, you need to be aware of some gotchas. If the environment URL is malformed, the system won’t update it, which can lead to confusion. Also, the stop_review job doesn’t generate a dotenv report artifact, meaning it won’t recognize the DYNAMIC_ENVIRONMENT_URL variable if you try to set it there. Remember that to create environments, you need specific roles in your project, such as Developer or Maintainer. With the introduction of features in GitLab 15.2 and 15.3, managing URLs has become more flexible, but be cautious with version changes, like the removal of environment renaming in GitLab 16.0.

Key takeaways

  • Differentiate between static and dynamic environments for effective deployment management.
  • Utilize dotenv variables to capture environment URLs seamlessly.
  • Be aware that malformed URLs won't update in the system, leading to potential deployment issues.
  • Understand that `stop_review` jobs won't recognize `DYNAMIC_ENVIRONMENT_URL` variables.
  • Ensure you have the correct roles to create environments in your GitLab project.

Why it matters

In production, managing environments effectively can significantly reduce deployment errors and streamline your release process. Properly configured environments ensure that your application behaves as expected across different stages, enhancing overall reliability.

Code examples

chroma
deploy_staging:stage:deployscript:-echo "Deploy to staging server"environment:name:stagingurl:https://staging.example.com
chroma
deploy_review_app:stage:deployscript:make deployenvironment:name:review/$CI_COMMIT_REF_SLUGurl:https://$CI_ENVIRONMENT_SLUG.example.comrules:-if:$CI_COMMIT_BRANCH == "main"when:never-if:$CI_COMMIT_BRANCH
chroma
review:script:-DYNAMIC_ENVIRONMENT_URL=$(deploy-script)# In script, get the environment URL.-echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env# Add the value to a dotenv file.artifacts:reports:dotenv:deploy.env# Report back dotenv file to rails.environment:name:review/$CI_COMMIT_REF_SLUGurl:$DYNAMIC_ENVIRONMENT_URL# and set the variable produced in script to `environment:url`on_stop:stop_reviewstop_review:script:-./teardown-environmentwhen:manualenvironment:name:review/$CI_COMMIT_REF_SLUGaction:stop

When 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 docs

Test what you just learned

Quiz questions written from this article

Take the quiz →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.