简体   繁体   English

如何读取 Gitlab CI 脚本中的标签

[英]How to read labels in Gitlab CI script

I have a few use cases in my Gitlab setup I would like to be able to support:我希望能够支持的 Gitlab 设置中有一些用例:

  1. If a certain label (let's call it “skip_build”) is set, the deployment steps should not be run when I merge an MR to a main branch.如果设置了某个 label(我们称之为“skip_build”),那么当我将 MR 合并到主分支时,不应运行部署步骤。 This would be useful when we have multiple MRs being merged right after another and only need the last one built.当我们有多个 MR 一个接一个地合并并且只需要构建最后一个时,这将很有用。
  2. If another label (we'll call it “skip_tests”) is set, I should be able to read it as an env var from within the script and alter the flow within the script accordingly (using normal bash syntax), eg to alter the package command parameters used a bit.如果设置了另一个 label(我们称之为“skip_tests”),我应该能够从脚本中将其作为环境变量读取并相应地更改脚本中的流程(使用正常的 bash 语法),例如更改package 命令参数用到了位。 This is useful for small changes where it might not make sense to run a lengthy test suite.这对于运行冗长的测试套件可能没有意义的小更改很有用。

Is this possible with Gitlab, and if so, how? Gitlab 是否可以做到这一点,如果可以,怎么做?

I've tried experimenting with CI_MERGE_REQUEST_LABELS, but it doesn't seem to be able to read that as an env var from within the script.我尝试过使用 CI_MERGE_REQUEST_LABELS 进行试验,但它似乎无法从脚本中将其作为环境变量读取。

You have to use merge request pipelines for the CI_MERGE_REQUEST_LABELS variable (and other MR-related variables) to be present as documented in predefined variables .您必须为CI_MERGE_REQUEST_LABELS变量(以及其他与 MR 相关的变量)使用合并请求管道,才能按照预定义变量中的说明出现。

You could use a rules: clause to skip jobs.您可以使用rules:子句跳过作业。 Something like就像是

build:
  rules:  # only run this job if the regex pattern does not match
    - if: $CI_MERGE_REQUEST_LABELS !~ /skip_build/

You can also do this on any other kind of predefined (or user-defined) variable, like branch name, commit messages, MR titles, etc. Whatever works for you.您还可以对任何其他类型的预定义(或用户定义)变量执行此操作,例如分支名称、提交消息、MR 标题等。任何适合您的。

For example, a built in feature of GitLab is that if your commit message contains [ci skip] it will prevent the pipeline from running.例如,GitLab 的一个内置特性是,如果您的提交消息包含[ci skip] ,它将阻止管道运行。 You could implement similar functionality for your jobs and/or pipelines through rules: or workflow:rules: .您可以通过rules:workflow:rules:为您的作业和/或管道实现类似的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM