简体   繁体   English

Gitlab CI/CD:设置与环境相关的变量

[英]Gitlab CI/CD: Setup variables relating to environment

I am working with gitlab version 14.10.5.我正在使用 gitlab 版本 14.10.5。

In the left panel, there is a "Deployments" icone that allow me to create environments.在左侧面板中,有一个“部署”图标可以让我创建环境。 In a gitlab repository I want to set up pipeline for dev, stage and main branches relating to 3 environments.在 gitlab 存储库中,我想为与 3 个环境相关的开发、阶段和主要分支设置管道。

In these environments, I want to setup specific variables that will chaneg over environments.在这些环境中,我想设置将改变环境的特定变量。

For example, I want the variable NINJA to be setup with the value:例如,我希望使用以下值设置变量 NINJA

  • "1" for the branch dev “1”代表分支开发
  • "2" for the branch stage “2”代表分支阶段
  • "3" for the branch main “3”代表分支主

So, in my.gitlab-ci.yml I have:所以,在 my.gitlab-ci.yml 我有:

build-testing:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: testing
  only:
    refs:
      - dev


build-staging:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: staging
  only:
    refs:
      - stage


build-production:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: production
  only:
    refs:
      - main

How to setup the variable NINJA in an environment, in gitlab?如何在环境中设置变量NINJA ,在 gitlab 中?

You can set variable scope, so a variable with same name will return different values in in different envs.您可以设置变量 scope,因此具有相同名称的变量将在不同的环境中返回不同的值。

  1. Navigate to Settings -> CI/CD导航到设置 -> CI/CD
  2. In Variables section press "Add Variable"在变量部分按“添加变量”
  3. Set key as NINJA将密钥设置为 NINJA
  4. Set "Environment scope" with proper env, ie testing使用适当的环境设置“环境范围”,即测试
  5. Repeat 2,3 and 4 with other envs (staging, production)对其他环境重复 2,3 和 4(暂存、生产)

See also: https://docs.gitlab.com/ee/ci/environments/#scope-environments-with-specs另请参阅: https://docs.gitlab.com/ee/ci/environments/#scope-environments-with-specs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM