Search code examples
gitlabpipeline

Why in the Gitlab Pipeline is my workflow rule being ignored?


This is my gitlab-ci.yml:

workflow:
  rules:
    - if: $CI_COMMIT_MESSAGE =~ /-draft$/
      when: never
    - if: $CI_PIPELINE_SOURCE == "push"

include:
  - '/gitlab-ci/includes.yml'
  - '/idt-test-stub/gitlab-ci.yml'

variables:
  ALPINE_VERSION: "3.16"
  NODE_VERSION: "14-alpine"
  ESLINT_CONFIG_PATH: "./.eslintrc.js"
  SOURCE_DIR: "."
  BUILD_TYPE: MAVEN
  MVN_CLI_OPTS: "--batch-mode"
  MVN_OPTS: "-Dmaven.repo.local=.m2-local -f wiremock-java/pom.xml"
  MAVEN_IMAGE: "maven:3-jdk-11"

stages:
  - test
  - code-quality
  - code-test
  - code-analysis
  - verify
  - transform
  - application-build
  - image-build
  - move-container-tests
  - container-image-test
  - image-push
.branches: &branches
  only:
    - branches

todo-check:
  <<: *branches

shell-check:
  <<: *branches

docker-lint:
  <<: *branches

unit-test:
  <<: *branches
  artifacts:
    expire_in: 20 mins
    paths:
      - ./coverage


verify:
  <<: *branches
  stage: verify
  image: node:$NODE_VERSION
  script:
    - npm install
    - ./run-verify.sh
  tags:
    - docker-in-docker-privilegedx

My understand is that if I commit a change including the word 'draft' in my commit message, when I push it, my workflow rules should stop the pipeline from running and yet it doesn't.

All the jobs in the pipeline get run.

The workflow rules part is copied directly from the Gitlab documentation which says this:

In the following example:

Pipelines run for all push events (changes to branches and new tags). Pipelines for push events with -draft in the commit message don’t run, because they are set to when: never. Pipelines for schedules or merge requests don’t run either, because no rules evaluate to true for them.


Solution

  • It's because of the $ at the end of /-draft$/. $ is a special character in regular expressions and matches the end of a piece of text.

    Even trying to use $ for its purpose in Gitlab and using a commit message like 'Commit -draft' still fails though. I don't know why. Perhaps they don't support the $ character at all.

    If you just want to check whether your commit message contains the word draft or not, you can omit the - and the $ at the end:

    workflow:
      rules:
        - if: $CI_COMMIT_MESSAGE =~ /draft/
          when: never
    

    I tested it on Gitlab:

    Commits

    PS. I've created a merge request on Gitlab to fix this part of their docs, so let's see what they say.