添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • View environments and deployments
  • Search environments
  • CI/CD variables
  • Types of environments
  • Deployment tier of environments
  • Configure manual deployments
  • Track newly included merge requests per deployment
  • Working with environments
  • Related topics
  • Troubleshooting
  • Environments and deployments

    Environments describe where code is deployed.

    Each time GitLab CI/CD deploys a version of code to an environment, a deployment is created.

    GitLab:

    • Provides a full history of deployments to each environment.
    • Tracks your deployments, so you always know what is deployed on your servers.

    If you have a deployment service like Kubernetes associated with your project, you can use it to assist with your deployments.

    View environments and deployments

    Prerequisites:

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

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

    Search environments

    Version history Introduced in GitLab 15.5.
  • Searching environments within a folder was introduced in GitLab 15.7 with Feature flag enable_environments_search_within_folder . Enabled by default.
  • To search environments by name:

    1. On the left sidebar, select Search or go to and find your project.
    2. Select Operate > Environments .
    3. In the search bar, enter your search term.
    4. The length of your search term should be 3 or more characters .
    5. Matching applies from the beginning of the environment name.
    6. For example, devel matches the environment name development , but elop does not.
    7. For environments with a folder name format, matching applies after the base folder name.
    8. For example when the name is review/test-app , search term test matches review/test-app .
    9. Also searching with the folder name prefixed like review/test matches review/test-app .

    CI/CD variables

    To customize your environments and deployments, you can use any of the predefined CI/CD variables , and define custom CI/CD variables.

    Types of environments

    An environment is either static or dynamic:

  • Dynamic environment
  • Usually created in a CI/CD pipeline and used by only a single deployment, then either stopped or deleted.
  • Has a dynamic name, usually based on the value of a CI/CD variable.
  • A feature of review apps .
  • Create a static environment

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

    In the UI

    Prerequisites:

    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:

    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.

    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.
    3. url : Optional. Prefix the hostname with a related CI/CD variable like $CI_ENVIRONMENT_SLUG .
    note
    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.