可以在GitLab CI脚本中访问作业标签列表吗?

huangapple go评论53阅读模式
英文:

Is it possible to access the list of job tags in a GitLab CI script?

问题

Is a GitLab CI job's list of tags somehow exposed to its script?

我们想要确保我们的项目可以在各种不同的构建工具链中构建,因此我们为每个我们想要支持的工具链都有CI作业。构建作业通过在作业标签中指定工具链来选择适当的运行程序,但是任何给定的运行程序可能会提供多个工具链,因此作业脚本还需要知道要运行哪个工具链。

现在,这意味着我们在作业定义中重复了所选的工具链:一次在标签列表中,一次在脚本中。如果我们可以从脚本中获取作业的标签列表,那么我们可以自动提取指定工具链的标签。

例如:

build-xcode13:
  stage: build
  tags:
    - xcode-13.4.1
  script:
    - XCODE_VERSION=13.4.1 build-tools/ci-scripts/build-debug.sh

请注意,“13.4.1”同时出现在标签和脚本主体中。当有人复制粘贴作业定义但忘记更新其中一个值时,这似乎是不必要的容易出错的地方。如果我们有标签列表可用,我们可以通过扫描标签以找到与模式匹配的标签来轻松提取版本。

我能找到的唯一相关的预定义变量是 CI_RUNNER_TAGS,但结果是列出了运行程序定义的所有标签,而不是作业定义的标签。

另一种解决方法:我想我们在作业的标题中也有在某种程度上重复工具链,并且CI_JOB_NAME 可以在脚本中使用的,因此我们可以使标题包含精确的工具链规范,但这与在脚本中明确设置变量一样可靠 - 真正重要的是运行程序选择规范与脚本实际尝试使用的内容匹配。

英文:

Is a GitLab CI job's list of tags somehow exposed to its script?

We want to make sure that our project builds in a variety of different build toolchains, so we have CI jobs for each toolchain we want to support. Build jobs select an appropriate runner by specifying the toolchain in the job tags, but any given runner might provide more than one toolchain, so the job script additionally needs to know which toolchain to run.

Right now, this means we're duplicating the selected toolchain in the job definition: once in the tag list, and once in the script. If we could get at the job's tag list from the script, we could extract the tag that specifies the toolchain automatically.

For example:

build-xcode13:
  stage: build
  tags:
    - xcode-13.4.1
  script:
    - XCODE_VERSION=13.4.1 build-tools/ci-scripts/build-debug.sh

Note how "13.4.1" appears in both the tags and the script body. This seems unnecessarily error-prone when someone copy-pastes the job definition but forgets to update one or the other value. If we had the list of tags available, we could easily extract the version by scanning the tags for one that matches the pattern.

The only relevant predefined variable I could find is CI_RUNNER_TAGS, but this turns out to list all the tags defined for the runner, not the tags specified in the job.

Another workaround: I suppose we're also sort-of duplicating the toolchain in the job's title, and CI_JOB_NAME is exposed to scripts, so we could make the title contain the exact toolchain specification, but that's no more reliable than explicitly setting a variable in the script - what really matters is that the spec for runner selection matches what the script actually tries to use.

答案1

得分: 2

不确定我是否理解你的意思。如果只是指定标签两次是问题,你可以将其保留为变量。

build-xcode13:
  stage: 构建
  variables:
    XCODE_VERSION: 13.4.1
  tags:
    - xcode-$XCODE_VERSION
  script:
    - build-tools/ci-scripts/build-debug.sh

如果这不是你需要的,请告诉我。我会尽力帮助并更新答案。
英文:

Not sure if I get you correctly. If just specifying the tag twice is the issue you can keep it as variable.

build-xcode13:
  stage: build
  variables:
    XCODE_VERSION: 13.4.1
  tags:
    - xcode-$XCODE_VERSION
  script:
    - build-tools/ci-scripts/build-debug.sh

Let me know if this is not what you need. Will try to help and update the answer then.

huangapple
  • 本文由 发表于 2023年5月29日 17:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定