我正在尝试根据自己的执行进度创建一个动态的 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"
变量不能在定义中求值。如果您确实想使用 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
然而,这是一个非常糟糕的设计。这两个作业都会在每次提交时执行,但只有一个作业会执行某些操作。最好通过分支来区分它们。仅将内容提交到您要部署到的分支:
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。此外,如果 shell 脚本位于同一目录中,则必须在前面添加 ./
。它应该看起来像:
export RELEASE=$(./check-release-type-dynamically.sh)
allow_failure: false
这是 gitlab-ci 中的默认设置,不是必需的。
variables:
- $RELEASE == "general_availability"
变量语法错误,请使用:
variables:
VARIABLE_NAME: "Value of Variable"
可以通过 gitlab UI 定义的变量是 CI/CD 变量。在作业执行期间可以更改/创建的任何变量都是与 CI/CD 变量无关的其他变量,它们仅存在于一个作业中,并且只能通过 dotenv 传递给后续任务。 https://docs.gitlab.com/ee/ci/variables/#:~:text=这些%20variables%20cannot%20be%20used%20as%20CI/CD%20variables%20to%20configure%20a%20pipeline