Regex based rules clause does not work in GitLab CI

Viewed 2862

I want my Gitlab CI job to not run when the commit message starts with a particular string: [maven-scm]

So, I have the below configuration in my .gitlab-ci.yaml file:

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\] .*$/'

My commit message is: [maven-scm] I hope the test job does not run

But the test job still runs to my frustration. I went over the GitLab documentation for rules but could not find the reason why the job still runs. I am not sure if I am missing something.

Would be great if someone can point me in the right direction.

Update:

I tried the only/except clause instead of the rules. I modified the yaml file to below:

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\] .*$/

The job still runs when the commit message starts with [maven-scm].

1 Answers

This was a tricky problem, because the issue was not with the rules section. The problem is actually the regex. You only need to specify the desired pattern at the start of the commit message, i.e. don't need the following wildcard. The following works and has been tested:

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

This has been tested with the following commit messages:

  • 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 documentation specifies that rules is preferred over only/except, so best to stick with rules: if. See onlyexcept-basic.

Related