【问题标题】:How to properly define regex as global variables in gitlab-ci.yml如何在 gitlab-ci.yml 中正确地将正则表达式定义为全局变量
【发布时间】:2021-12-26 00:53:37
【问题描述】:

我正在尝试将正则表达式定义为gitlab-ci.yml 中的全局变量。

变量是:

variables:
  -SPEEDY_REGEX: '(chore|docs|feat|fix|refactor|style|test)\:( |\t)+\[SPEEDY\].*'

现在我想在 if 语句中使用它来运行特定的构建:

build:
  rules:
    - if : '$CI_COMMIT_MESSAGE =~ $SPEEDY_REGEX'
      when: never
    - when: on_success
build:speedy:
  rules:
    - if : '$CI_COMMIT_MESSAGE =~ $SPEEDY_REGEX'
      when: on_success
    - when: never

由于我忽略的原因,这不想工作。

【问题讨论】:

  • 你尝试过 / 代替 " 在正则表达式中吗?许多语言使用 / 来引用正则表达式。也许 SPEEDY_REGEX=/(chore|docs|feat|fix|refactor|style|test)\:( |\t)+[速度].*/
  • @DaveH。是的,但它没有工作

标签: gitlab gitlab-ci gitlab-ci-runner


【解决方案1】:

很遗憾,由于rules: 中正则表达式的解析方式,您将无法将变量用作正则表达式的一部分。

相反,对于 DRY 方法,您可以考虑使用其他方法来重用 YAML 部分,例如锚点、extends!reference 标记。

.speedy_rules:
  rules:
    - if : '$CI_COMMIT_MESSAGE =~ /(chore|docs|feat|fix|refactor|style|test)\:( |\t)+\[SPEEDY\].*/'
      when: never
    - when: on_success

build:
  rules: !reference [.speedy_rules, rules]
  # ...

example:
  rules:
    - if: '$SOMETHING == "another rule"'
    - !refernece [.speedy_rules, rules]

example2:
  extends: .speedy_rules
  # ...

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2021-05-29
    • 2012-03-26
    相关资源
    最近更新 更多