Environments and Deployments
You'll learn how to define named deployment targets in GitLab CI/CD, track deployment history, and configure environment-specific URLs.
What Are Environments in GitLab?
Think of environments as labeled deployment targets—named stages where your application lives. You might deploy to staging for testing, then production for users. GitLab CI/CD tracks each deployment to these environments, creating a history of what was deployed, when, and by which pipeline.
Unlike simple job execution, environments provide:
- Deployment tracking and history
- Environment-specific URLs you can click to visit
- Rollback capabilities
- Protection rules (covered in future lessons)
Defining an Environment
You define environments using the environment keyword within a job:
deploy_staging:
stage: deploy
script:
- echo "Deploying to staging server..."
- ./deploy.sh staging
environment:
name: staging
url: https://staging.example.com
The name identifies the environment, while url provides a clickable link in GitLab's UI to visit the deployed application.
Dynamic Environments
You can create environments dynamically using variables:
deploy_review:
stage: deploy
script:
- ./deploy.sh review-$CI_COMMIT_REF_SLUG
environment:
name: review/$CI_COMMIT_REF_SLUG
url: https://review-$CI_COMMIT_REF_SLUG.example.com
This creates a unique environment per branch, perfect for review apps.
Viewing Deployments
GitLab tracks every successful deployment job to an environment. You can see:
- Deployment history timeline
- Which commit is currently deployed
- Quick access via the environment URL
- Who triggered each deployment
Key Takeaway: Environments in GitLab CI/CD transform deployment jobs into trackable, manageable deployment targets with URLs and history, providing visibility into what version runs where.