Environments

  • Tier : Free, Premium, Ultimate
  • Offering : GitLab.com, GitLab Self-Managed, GitLab Dedicated

A GitLab environment represents a specific deployment target for your application, like development, staging, or production. Use it to manage different configurations and deploy code during various stages of your software lifecycle.

With environments, you:

  • Keep your deployment process consistent and repeatable
  • Track what code is deployed where
  • Roll back to previous versions when problems occur
  • Protect sensitive environments from unauthorized changes
  • Control deployment variables per environment to maintain security boundaries
  • Monitor environment health and get alerts if something goes wrong

View environments and deployments

Prerequisites:

There are a few ways to view a list of environments for a given project:

  • On the project’s overview page, if at least one environment is available (that is, not stopped). A project overview page displaying the number of available environments as an incremental counter.

  • On the left sidebar, select Operate > Environments . The environments are displayed.

    A list of available environments in a GitLab project, showing environment names, statuses, and other relevant details.

  • To view a list of deployments for an environment, select the environment name, for example, staging . A list of deployments for a selected environment, displaying deployment history and related details.

    Deployments show up in this list only after a deployment job has created them.

  • To view a list of all manual jobs in a deployment pipeline, select the Run ( play ) dropdown list.

    Viewing a manual job in a deployment pipeline

Environment URL

The environment URL is displayed in a few places in GitLab:

  • In a merge request as a link: Environment URL in merge request
  • In the Environments view as a button: Open live environment from environments view
  • In the Deployments view as a button: Environment URL in deployments

You can see this information in a merge request if:

  • The merge request is eventually merged to the default branch (usually main ).
  • That branch also deploys to an environment (for example, staging or production ).

For example:

Environment URLs in merge request

Go from source files to public pages

With GitLab Route Maps , you can go directly from source files to public pages in the environment set for review apps.

Types of environments

An environment is either static or dynamic.

Static environments:

  • Are usually reused by successive deployments.
  • Have static names. For example, staging or production .
  • Are created manually or as part of a CI/CD pipeline.

Dynamic environments:

  • Are usually created in a CI/CD pipeline and are used by only a single deployment, then either stopped or deleted.
  • Have dynamic names, usually based on the value of a CI/CD variable.
  • Are a feature of review apps .

An environment has one of three states, depending on whether its stop job has run:

  • available : The environment exists. There might be a deployment.
  • stopping : The on stop job has started. This state does not apply when there is no on stop job defined.
  • stopped : Either the on stop job has run, or a user manually stopped the job.

Create a static environment

You can create a static environment in the UI or in your .gitlab-ci.yml file.

In the UI

Prerequisites:

  • You must have at least the Developer role.

To create a static environment in the UI:

  1. On the left sidebar, select Search or go to and find your project.
  2. Select Operate > Environments .
  3. Select Create an environment .
  4. Complete the fields.
  5. Select Save .

In your .gitlab-ci.yml file

Prerequisites:

  • You must have at least the Developer role.

To create a static environment, in your .gitlab-ci.yml file:

  1. Define a job in the deploy stage.
  2. In the job, define the environment name and url . If an environment of that name doesn’t exist when the pipeline runs, it is created.

Some characters cannot be used in environment names. For more information about the environment keywords, see the .gitlab-ci.yml keyword reference .

For example, to create an environment named staging , with URL https://staging.example.com :

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging server"
  environment:
    name: staging
    url: https://staging.example.com

Create a dynamic environment

To create a dynamic environment, you use CI/CD variables that are unique to each pipeline.

Prerequisites:

  • You must have at least the Developer role.

To create a dynamic environment, in your .gitlab-ci.yml file:

  1. Define a job in the deploy stage.
  2. In the job, define the following environment attributes:
    • name : Use a related CI/CD variable like $CI_COMMIT_REF_SLUG . Optionally, add a static prefix to the environment’s name, which groups in the UI all environments with the same prefix.
    • url : Optional. Prefix the hostname with a related CI/CD variable like $CI_ENVIRONMENT_SLUG .

Some characters cannot be used in environment names. For more information about the environment keywords, see the .gitlab-ci.yml keyword reference .

In the following example, every time the deploy_review_app job runs the environment’s name and URL are defined using unique values.

deploy_review_app:
  stage: deploy
  script: make deploy
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    url: https://$CI_ENVIRONMENT_SLUG.example.com
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: never
    - if: $CI_COMMIT_BRANCH

Set a dynamic environment URL

Some external hosting platforms generate a random URL for each deployment, for example: https://94dd65b.amazonaws.com/qa-lambda-1234567 . That makes it difficult to reference the URL in the .gitlab-ci.yml file.

To address this problem, you can configure a deployment job to report back a set of variables. These variables include the URL that was dynamically generated by the external service. GitLab supports the dotenv ( .env ) file format, and expands the environment:url value with variables defined in the .env file.

To use this feature, specify the artifacts:reports:dotenv keyword in .gitlab-ci.yml .

You can also specify a static part of the URL at environment:url , such as https://$DYNAMIC_ENVIRONMENT_URL . If the value of DYNAMIC_ENVIRONMENT_URL is example.com , the final result is https://example.com .

The assigned URL for the review/your-branch-name environment is visible in the UI.

Environment tier Environment name examples
production Production, Live
staging Staging, Model, Demo
testing Test, QC
development Dev, Review apps , Trunk
other

↓ Scope / Environment → production staging review/feature-1 review/feature-2
* Match Match Match Match
production Match
staging Match
review/* Match Match
review/feature-1 Match