为什么GitLab CICD工作流程:规则:如果:变量未能设置变量?

发布于 2025-01-30 06:20:00 字数 884 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2025-02-06 06:20:00

gitlab-runner exec具有限制并且不实现/考虑YAML定义的许多功能,包括工作流程:规则:[]变量在这种情况下。

但是,通过gitlab.com Gitlab的工作流程:规则:将正确评估。

请记住,在某些情况下,在其他地方设置的变量将优先级对YAML定义的变量,例如在项目,组或实例设置中设置变量时。

gitlab-runner exec has several limitations and does not implement/consider many features of YAML definitions, including workflow: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.

寄居人 2025-02-06 06:20:00

如果您将条件放入任务,则作业应该有效。

my_project_test:
  stage: test
  rules:
     - if: '$CI_COMMIT_REF_NAME == "master"'
       variables:
        DEPLOY_VARIABLE: "master-deploy"  # Override globally-defined DEPLOY_VARIABLE
  script:
    - env | grep CI
    - echo $DEPLOY_VARIABLE // this always outputs dev-deploy.

但是,该变量仅在您的工作范围内,在您的情况下,它不会覆盖另一个作业中的全局价值。

您真正需要在作业之间传递变量:

设置全局变量gitlab-ci

the assignment should works if you put the condition in your task.

my_project_test:
  stage: test
  rules:
     - if: '$CI_COMMIT_REF_NAME == "master"'
       variables:
        DEPLOY_VARIABLE: "master-deploy"  # Override globally-defined DEPLOY_VARIABLE
  script:
    - env | grep CI
    - echo $DEPLOY_VARIABLE // this always outputs dev-deploy.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文