基于正则表达式的规则子句在 GitLab 中不起作用 CI

Regex based rules clause does not work in GitLab CI

当提交消息以特定字符串开头时,我希望我的 Gitlab CI 作业 而不是 运行:[maven-scm]

所以,我的 .gitlab-ci.yaml 文件中有以下配置:

image: maven:3.6.3-jdk-11-slim

stages:
  - test

test:
  stage: test
  cache:
    key: all
    paths:
      - ./.m2/repository
  script:
    - mvn clean checkstyle:check test spotbugs:check
  rules:
    - if: '$CI_COMMIT_MESSAGE !~ /^\[maven-scm\] .*$/'

我的提交信息是:[maven-scm] I hope the test job does not run

但测试工作仍然 运行 令我沮丧。我查看了 rules 的 GitLab 文档,但找不到作业仍然 运行 的原因。我不确定我是否遗漏了什么。

如果有人能指出正确的方向,那就太好了。

更新:

我尝试了 only/except 子句而不是规则。我将 yaml 文件修改为以下内容:

image: maven:3.6.3-jdk-11-slim

stages:
  - test

test:
  stage: test
  cache:
    key: all
    paths:
      - ./.m2/repository
  script:
    - mvn clean checkstyle:check test spotbugs:check
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /^\[maven-scm\] .*$/

当提交消息以 [maven-scm] 开头时,作业仍然 运行s。

这是一个棘手的问题,因为问题不在 rules 部分。问题实际上是正则表达式。您只需要在提交消息的开头指定所需的模式,即不需要以下通配符。以下工作并且已经过测试:

test-rules:
  stage: test
  rules:
    - if: '$CI_COMMIT_MESSAGE !~ /^\[maven-scm\] /'
  script:
    - echo "$CI_COMMIT_MESSAGE"

已使用以下提交消息对此进行了测试:

  • This commit message will run the job
  • This commit message [maven-scm] will run the job
  • [maven-scm] This commit message will NOT run the job

仅供参考 GitLab 文档指定 rules 优于 only/except,因此最好坚持使用 rules: if。参见 onlyexcept-basic