Gitlab Runner 根据自定义变量在选定的 运行 人上 运行 工作
Gitlab Runner to run jobs on selected runners according to custom variable
我计划根据来自管道网络 UI 的变量在 运行ners 上安排作业。我有两个 运行ners 注册了一个项目,目前他们都在同一台机器上 now.Both 运行ners 有相同的标签,所以也无法区分标签。
这是我的 yaml 文件
stages :
- common
- specific
test1:
stage: common
tags:
- runner
- linux
script:
- echo $CI_RUNNER_DESCRIPTION
- echo "This is run on all runners"
test2:
stage: specific
tags:
- runner
- linux
script: echo "This is run on runner 1"
only:
variables:
- $num == $CI_RUNNER_DESCRIPTION
test3:
stage: specific
tags:
- runner
- linux
script: echo "This is run on runner 2"
only:
variables:
- $num == $CI_RUNNER_DESCRIPTION
所以进行选择的变量是“num”。 运行 人的描述可以是 1 或 2。
num 的默认值为 0,根据从管道 UI 传递的变量,要选择的作业 运行。
但是当我用 num 1 或 2 执行测试时,只有 test1 被执行,这对所有 运行ners 都是常见的。
这样的实现是否可行,或者我是否面临问题,因为 运行 用户在同一台机器上?
使用 variables only
不是 select 运行 用户的正确方法。现在你 select 2 个 运行 人之一,如果 运行 人的描述不匹配,尽量不要执行工作。即使这有效,也没有多大意义,因为您不知道该作业是否会执行。
我建议您为 运行 人添加特定的标签,并指定哪个工作应该 运行 在哪个 运行 人上。
test2:
stage: specific
tags:
- runner_1
script: echo "This is run on runner 1"
test3:
stage: specific
tags:
- runner_2
script: echo "This is run on runner 2"
如果你想 select 运行ner 用于基于 UI 输入的作业,也许你可以将变量添加到标签部分。老实说,我没有测试过这样的解决方案,也不知道它是否会起作用。然而,它仍然需要为您的 运行 用户创建特定标签。
tags:
- $tag_variable_selected_from_UI
您目前无法使用 gitlab 执行此操作。正如您在对已接受答案的评论中指出的那样,这将在 https://gitlab.com/gitlab-org/gitlab/-/issues/35742 得到解决后可用。
话虽如此,虽然“已接受”答案正确地指出“仅”是执行此操作的不正确方法,但它提出了另一种同样无效的方法。
一旦 dynamic tagging 支持被添加到 gitlab-ci,这将成为可能。目前,解决方案是提供独特的作业,每个作业都有自己的标签。
根据您要完成的具体目标,您可以通过添加作业模板并结合 extends 将每个作业的重复减少到几行,从而使您的代码尽可能保持干爽。
我计划根据来自管道网络 UI 的变量在 运行ners 上安排作业。我有两个 运行ners 注册了一个项目,目前他们都在同一台机器上 now.Both 运行ners 有相同的标签,所以也无法区分标签。
这是我的 yaml 文件
stages :
- common
- specific
test1:
stage: common
tags:
- runner
- linux
script:
- echo $CI_RUNNER_DESCRIPTION
- echo "This is run on all runners"
test2:
stage: specific
tags:
- runner
- linux
script: echo "This is run on runner 1"
only:
variables:
- $num == $CI_RUNNER_DESCRIPTION
test3:
stage: specific
tags:
- runner
- linux
script: echo "This is run on runner 2"
only:
variables:
- $num == $CI_RUNNER_DESCRIPTION
所以进行选择的变量是“num”。 运行 人的描述可以是 1 或 2。 num 的默认值为 0,根据从管道 UI 传递的变量,要选择的作业 运行。 但是当我用 num 1 或 2 执行测试时,只有 test1 被执行,这对所有 运行ners 都是常见的。
这样的实现是否可行,或者我是否面临问题,因为 运行 用户在同一台机器上?
使用 variables only
不是 select 运行 用户的正确方法。现在你 select 2 个 运行 人之一,如果 运行 人的描述不匹配,尽量不要执行工作。即使这有效,也没有多大意义,因为您不知道该作业是否会执行。
我建议您为 运行 人添加特定的标签,并指定哪个工作应该 运行 在哪个 运行 人上。
test2:
stage: specific
tags:
- runner_1
script: echo "This is run on runner 1"
test3:
stage: specific
tags:
- runner_2
script: echo "This is run on runner 2"
如果你想 select 运行ner 用于基于 UI 输入的作业,也许你可以将变量添加到标签部分。老实说,我没有测试过这样的解决方案,也不知道它是否会起作用。然而,它仍然需要为您的 运行 用户创建特定标签。
tags:
- $tag_variable_selected_from_UI
您目前无法使用 gitlab 执行此操作。正如您在对已接受答案的评论中指出的那样,这将在 https://gitlab.com/gitlab-org/gitlab/-/issues/35742 得到解决后可用。
话虽如此,虽然“已接受”答案正确地指出“仅”是执行此操作的不正确方法,但它提出了另一种同样无效的方法。
一旦 dynamic tagging 支持被添加到 gitlab-ci,这将成为可能。目前,解决方案是提供独特的作业,每个作业都有自己的标签。
根据您要完成的具体目标,您可以通过添加作业模板并结合 extends 将每个作业的重复减少到几行,从而使您的代码尽可能保持干爽。