为什么GitLab CICD工作流程:规则:如果:变量未能设置变量?
stages:
- test
# Default vars
variables:
DEPLOY_VARIABLE: "dev-deploy"
workflow:
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
variables:
DEPLOY_VARIABLE: "master-deploy" # Override globally-defined DEPLOY_VARIABLE
my_project_test:
stage: test
script:
- env | grep CI
- echo $DEPLOY_VARIABLE // this always outputs dev-deploy.
使用Gitlab-Runner 14.10.1运行。
无论我是在本地还是在gitlab上尝试,var永远不会设置。
在本地我使用gitlab-runner exec shell my_project_test
运行它。
env | GREP CI
是:
CI_SERVER_VERSION=
CI_RUNNER_EXECUTABLE_ARCH=darwin/amd64
CI_COMMIT_REF_NAME=master
CI_JOB_TOKEN=
CI_PROJECT_ID=0
CI_RUNNER_REVISION=f761588f
... etc
根据其文档: 如果规则匹配,则何时:始终为默认值,何时:如果什么都不匹配,则不是默认值。
我什至尝试了,如果:'1 == 1'
等等。
stages:
- test
# Default vars
variables:
DEPLOY_VARIABLE: "dev-deploy"
workflow:
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
variables:
DEPLOY_VARIABLE: "master-deploy" # Override globally-defined DEPLOY_VARIABLE
my_project_test:
stage: test
script:
- env | grep CI
- echo $DEPLOY_VARIABLE // this always outputs dev-deploy.
Running with gitlab-runner 14.10.1.
No matter if i try that locally or on Gitlab that var is never set.
On local I run it with gitlab-runner exec shell my_project_test
.
env | grep CI
is:
CI_SERVER_VERSION=
CI_RUNNER_EXECUTABLE_ARCH=darwin/amd64
CI_COMMIT_REF_NAME=master
CI_JOB_TOKEN=
CI_PROJECT_ID=0
CI_RUNNER_REVISION=f761588f
... etc
As per their documentation:
If a rule matches, when: always is the default, and when: never is the default if nothing matches.
I even tried if: '1 == 1'
and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gitlab-runner exec
具有限制并且不实现/考虑YAML定义的许多功能,包括工作流程:规则:[]变量
在这种情况下。但是,通过gitlab.com Gitlab的
工作流程:规则:
将正确评估。请记住,在某些情况下,在其他地方设置的变量将优先级对YAML定义的变量,例如在项目,组或实例设置中设置变量时。
gitlab-runner exec
has several limitations and does not implement/consider many features of YAML definitions, includingworkflow:rules:[]variables
in this case.However, when run through gitlab.com or a self-hosted instance of GitLab,
workflow:rules:
will evaluate properly.Keep in mind, there are a few cases where variables set elsewhere will take precedence over variables defined in the YAML, such as when variables are set in project, group, or instance settings.
如果您将条件放入任务,则作业应该有效。
但是,该变量仅在您的工作范围内,在您的情况下,它不会覆盖另一个作业中的全局价值。
您真正需要在作业之间传递变量:
设置全局变量gitlab-ci
the assignment should works if you put the condition in your task.
However the variable is only in the scope of your job, which is under your if condition, it won't overwrite the global value in the another job.
What you really need to pass variable between jobs :
set up global variables dynamically in gitlab-ci