如何使用变量设置动态 gitlab 作业标签?

Ale*_*hen 5 gitlab gitlab-ci

我正在尝试创建一个动态作业,可以根据给定的标签在两个 gitlab 运行程序之间切换。我想用环境变量来做到这一点,但似乎不能使用。以下工作:

runner_test:
  image: alpine
  tags:
    - $MY_RUNNER
  stage: deploy_main
  script:
    - echo foobar
  retry: 2
Run Code Online (Sandbox Code Playgroud)

导致管道暂停并出现错误: This job is stuck because you don't have any active runners online or available with any of these tags assigned to them: $MY_RUNNER

ash*_*ium 13

动态工作标签现在可以实现(最初在 14.10 上测试,在 16.2 上更新和测试)。变量插值适用于作业标签,但是,一个未记录的限制是作业 YAML 中的变量更改将不适用(即使规则:关键字应该在其他任何内容之前解释。)这意味着我们需要设置使用工作流程:规则:变量键在全局级别动态运行器标签。

以下示例将导致大多数分支的默认作业标签为“staging”,以及针对项目的默认分支运行的管道的覆盖值“product”。

# global pipeline variable for the default value
variables:
  RUNNER_TAG: "staging"  # default CI variable value

# global workflow:rules:variables can change variables outside of jobs
workflow:
  rules:
    # if the pipeline is on the default branch, then change the RUNNER_TAG variable
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        RUNNER_TAG: "production"
    # ensure pipeline still runs if above conditions are not met
    - when: always 

# ci job using the dynamic runner tag
job:
  tags:
    - $RUNNER_TAG
  script:
    - hello world from runner matching $RUNNER_TAG
Run Code Online (Sandbox Code Playgroud)

注意:workflow:rules:variables是通过功能标志在版本 13.11 中添加的,并且在版本 14.1 中无需功能标志即可使用


Rek*_*vni 5

这是目前不可用的。当前有一个未解决的问题在请求此功能的积压中:https : //gitlab.com/gitlab-org/gitlab-runner/-/issues/1809


也许可以使用规则扩展来解决

.template:
  stage: deploy_main
  script:
    - echo foobar

runner_test_1:
  extends: .template
  tags:
    - runner_1
  rules:
    - if: $RUNNER_TAG == runner_1
    
runner_test_2:
  extends: .template
  tags:
    - runner_2
  rules:
    - if: $RUNNER_TAG == runner_2
Run Code Online (Sandbox Code Playgroud)

或类似的东西。