管道启动后是否可以更改 Gitlab CI 变量值?

Rom*_*ter 7 continuous-integration continuous-deployment gitlab gitlab-ci

我正在尝试根据它自己的执行进度创建一个动态的 gitlab 管道。例如,我有 2 个环境,并且每个环境的部署将根据before_script 中脚本的执行启用/禁用。它对我不起作用,似乎管道启动后无法更改管道变量值。有什么建议?(请参阅下面我的 gitlab-ci.yml)

variables:
  RELEASE: limited

stages:
  - build
  - deploy


before_script:
  - export RELEASE=${check-release-type-dynamically.sh}

build1:
  stage: build
  script:
    - echo "Do your build here"

## DEPLOYMENT
deploy_production_ga:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "general_availability"


deploy_production_limited:
  stage: update_prod_env
  script:
  - echo "deploy environment for limited customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "limited"
Run Code Online (Sandbox Code Playgroud)

mle*_*les 4

无法在定义中评估变量。如果您确实想使用 shell 脚本来决定部署什么 get,您可以使用 bash if 子句:

stages:
  - build
  - update_prod_env

build1:
  stage: build
  script:
    - echo "Do your build here"

deploy_production_ga:
  stage: update_prod_env
  script:
  - if [ "$(./check-release-type-dynamically.sh)" == "general_availability" ]; then
      echo "deploy environment for all customers"
    fi
  only:
  - branches    

deploy_production_limited:
  stage: update_prod_env
  script:
  - if [ "$(./check-release-type-dynamically.sh)" == "limited" ]; then
      echo "deploy environment for all customers"
    fi
  only:
  - branches    
Run Code Online (Sandbox Code Playgroud)

然而,这确实是一个糟糕的设计。这两个作业都会在每次提交时执行,但只有一个作业会执行某些操作。最好通过分支来区分它们。仅将内容提交到您要部署到的分支:

stages:
  - build
  - update_prod_env

build1:
  stage: build
  script:
    - echo "Do your build here"

deploy_production_ga:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  only:
  - branches-general_availability    

deploy_production_limited:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  only:
  - branches-limited
Run Code Online (Sandbox Code Playgroud)

这样,只有您想要执行的构建作业才会被执行。

我注意到的其他一些事情:

export RELEASE=${check-release-type-dynamically.sh}使用 () 而不是 {} 作为子 shell。此外,如果 shell 脚本位于同一目录中,则必须在前面添加./. 它应该看起来像: export RELEASE=$(./check-release-type-dynamically.sh)

allow_failure: false这是 gitlab-ci 中的默认设置,不是必需的。

variables:
- $RELEASE == "general_availability"
Run Code Online (Sandbox Code Playgroud)

变量语法错误,请使用:

variables:
  VARIABLE_NAME: "Value of Variable"
Run Code Online (Sandbox Code Playgroud)

看看https://docs.gitlab.com/ee/ci/yaml/