我希望我的 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]
开头时,作业仍会运行.
最佳答案
这是一个棘手的问题,因为问题不在于 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 .
关于continuous-integration - 基于正则表达式的规则子句在 GitLab CI 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63426990/