Skip to main content
In this guide, you’ll learn how to configure and manage environments and deployments in GitLab CI/CD. Environments represent the target locations—such as development, staging, or production—where your code runs. Each pipeline deployment creates a record, providing a clear history of changes and showing what version is currently live. We’ll cover:
  • Defining environments in .gitlab-ci.yml
  • A sample pipeline with containerization, testing, and deployment
  • Static vs. dynamic environments
  • Creating environments in the GitLab UI
  • Viewing, rolling back, and redeploying applications

1. Defining an Environment in .gitlab-ci.yml

You can declare environments directly in job definitions to track deployments and enable quick-access links.

Simple Environment Declaration

This tracks deployments under the production environment name.

Expanded Environment Configuration

  • name: The identifier displayed in the GitLab UI.
  • url: External link to the live application for rapid access.

2. Example Pipeline Configuration

Here’s a streamlined pipeline showcasing containerization, tests, and deployment to a Kubernetes namespace:

3. Static vs. Dynamic Environments

GitLab supports two environment types:

4. Creating a Static Environment

  1. In GitLab, navigate to Operations > Environments > New environment.
  2. Enter the Name and optional External URL.
  3. (Optional) Link a GitLab Agent for Kubernetes.
  4. Click Save.
The image shows a GitLab interface for creating a new environment, with fields for the environment name, external URL, and GitLab agent selection. The URL field has a validation message indicating it should start with "http://" or "https://".
After saving, the new environment appears in the list (initially with no deployments):
The image shows a GitLab environment page with a "development" environment listed but no deployments yet. The interface includes options for managing environments and related settings.

5. Running the Pipeline & Viewing Deployments

Once you push changes or create a Merge Request, the pipeline executes:
  • Containerization
  • Testing
  • Deployment
The image shows a GitLab CI/CD pipeline interface for a NodeJS project, displaying stages like containerization and deployment with job statuses.
After a successful deployment, visit Operations > Environments to see deployment details: commit ID, job name, timestamp, and more.
The image shows a GitLab environment page with a successful deployment in the "development" environment. It includes details about the trigger, job, and branch used for deployment.
Click Open live environment to launch your application. This link uses the url specified in your job’s environment settings and is also visible in Merge Requests for quick previews.

6. Rollback and Redeploy

If a deployment fails or you need to revert changes, GitLab offers Finish, Rollback, and Redeploy actions:
The image shows a GitLab environment page displaying a list of deployment jobs with their statuses, IDs, commit messages, and actions. The sidebar includes options for managing various aspects of the project, such as code, build, and deploy.
  • Rollback: Reverts to the last successful deployment (using the recorded commit ID).
  • Redeploy: Re-runs the deployment job for the same commit.
Use rollback carefully: any data migrations or schema changes may not be reversible.

Watch Video