CI Rules: If $variable not like x

Since rules are being favored over only/except I ran into a situation where I need to negate a regex match but fail to see how to do so in the documentation. I think I managed a work around with a negative look ahead in the regex but haven’t tested it yet. Does anyone know if there’s a nicer way of doing this?

Current stage:

some_stage:
  only:
    - on_this_branch
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /dont-run-this/

Negative lookahead:

some_stage:
  rules:
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "on_this_branch"  && 
          $CI_COMMIT_MESSAGE =~ /^((?!dont-run-this).)*$/

Do either of these syntax work?

some_stage:
  rules:
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "on_this_branch" &&
          $CI_COMMIT_MESSAGE !~ /dont-run-this/
some_stage:
  rules:
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "on_this_branch" && 
      not $CI_COMMIT_MESSAGE =~ /dont-run-this/
1 Like

There is a specific exclusion pattern matching for variables expression.

Here is the documentation on variable matching. (See item 6 in that section)

Negative pattern matching was introduced in Gitlab 11.11.

So your syntax can use the !~ to do negative pattern matching

2 Likes

That’s exactly what I was looking for, thank you!