基于Regex的规则子句在GitLab CI中不起作用



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

因此,我在.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

但测试工作仍然让我感到沮丧。我查看了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]开头时,作业仍在运行。

这是一个棘手的问题,因为问题不在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

FYI GitLab文档指定rules优先于only/except,因此最好使用rules: if。仅查看除基本外的内容。

最新更新