mbx
mbx

Reputation: 6526

Porting from `only/except`: How to run a gitlab-ci job only for tags excluding a pattern with `rules` in .gitlab-ci.yml?

I am trying to convert a .gitlab-ci.yml job from the deprecated only/except pattern to rules, specifically I want to run a job only if it's for a tag but not if that tag starts with config-.

Currently we use this, which at least works1.

build_image:
  stage: build_image
  only:
  - tags
  except:
  - /^config-.*$/

I have tried the rules based approach as follows, but that runs for every tag, including config:

build_image:
  stage: build_image
  rules:
    # only tags, except config
    - if: $CI_COMMIT_TAG
      when: on_success
    - if: '$CI_COMMIT_TAG =~ /^config-.*$/'
      when: never
    - when: never

Additionally I tried to use negative lookahead regex - without success, since gitlab's ci linter screams jobs:build_image:rules:rule if invalid expression syntax.

build_image:
  stage: build_image
  rules:
    # only tags, except config (using negative lookahead)
    - if: '$CI_COMMIT_TAG =~ /^(?!config-).*$/'
      when: on_success
    - when: never

So how can I trigger a job only for tags excluding a pattern with the new rules based approach?

1: apart from not being able to be combined with other rules later on

Upvotes: 5

Views: 3200

Answers (2)

Nicolas Pepinster
Nicolas Pepinster

Reputation: 6191

This rules worked for me (with Gitlab 13.12) :

  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^config-.*$/'

Upvotes: 3

mbx
mbx

Reputation: 6526

You just have to combine the unlike operator !~ with the regular tag check $CI_COMMIT_TAG:

build_image:
  stage: build_image
  rules:
    # only tags and only those not starting with config-
    - if: '$CI_COMMIT_TAG && ($CI_COMMIT_TAG !~ /^config-.*$/ )'
      when: on_success
    - when: never

Upvotes: 2

Related Questions