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

Dha*_*ran 5 continuous-integration gitlab gitlab-ci gitlab-ci-runner

我希望我的 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\] .*$/'
Run Code Online (Sandbox Code Playgroud)

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

但测试工作仍然让我感到沮丧。我查看了 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\] .*$/
Run Code Online (Sandbox Code Playgroud)

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

DV8*_*2XL 5

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

test-rules:
  stage: test
  rules:
    - if: '$CI_COMMIT_MESSAGE !~ /^\[maven-scm\] /'
  script:
    - echo "$CI_COMMIT_MESSAGE"
Run Code Online (Sandbox Code Playgroud)

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

  • 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。请参阅only except-basic