gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:18:09 25 4
gpt4 key购买 nike

我正在尝试根据它自己的执行进度创建一个动态的 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 脚本来决定部署什么,您可以使用 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"

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

关于continuous-integration - 管道启动后是否可以更改 Gitlab CI 变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com