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

时间:2018-06-13 17:15:21

标签: continuous-integration gitlab gitlab-ci continuous-deployment

我正在尝试根据它自己的执行进度创建一个动态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"

1 个答案:

答案 0 :(得分:2)

无法在定义中评估变量。如果您真的想使用shell脚本来决定部署了什么,可以使用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    

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

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

这样只会执行您想要执行的构建作业。

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

对于子壳,

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

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

variables:
- $RELEASE == "general_availability"

变量的语法错误,请使用:

variables:
  VARIABLE_NAME: "Value of Variable"

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