How to Set Up Environments and Deployments in GitLab CI/CD
The environment keyword records each deploy against a named environment and surfaces its URL in the UI.
Add environment: with a name and url to a deploy job. GitLab logs the deployment, tracks the latest version, and links to the running app.
Steps
- Add
environment:nameto the deploy job. - Set
environment:urlso the UI links to the live site. - Use a per-branch name for parallel environments when needed.
Pipeline
.gitlab-ci.yml
deploy-staging:
stage: deploy
script: ./deploy.sh staging
environment:
name: staging
url: https://staging.example.com
rules:
- if: '$CI_COMMIT_BRANCH == "main"'Gotchas
- A deployment is recorded only when the job runs, so a skipped job does not update the environment.
- Use
environment:action: stopplus anon_stopjob to tear down dynamic environments.
Related guides
How to Create Review Apps in GitLab CI/CDSpin up a per-merge-request review app in GitLab CI/CD with a dynamic environment name and an on_stop job tha…
How to Add a Manual Job in GitLab CI/CDRequire a human click before a GitLab CI/CD job runs with when:manual, and use allow_failure to control wheth…