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

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

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

This rules worked for me (with Gitlab 13.12) :
rules:
- if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^config-.*$/'

Related

GitLab CI/CD stage run only on main branch and tags matches pattern

I want to run the release only on main branch and tag matches /^v\d+\.\d+\.\d+$/. I have below stage, If I tag non main branch this stage runs. how can I match both rules.
run-release:
stage: run-release
image: busybox
rules:
- if: ( $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ && $CI_DEFAULT_BRANCH == "main" )
script:
- echo "Run release"
Thanks

How to except regular expression in gitlab.ci?

I need to exclude branches with the names feature/ussues- from some steps. There is a regular expression, but when I set except,rule or only, either the job always was included, or was not included at all.
Initially I tried this using rules::
rules:
- if: $CI_COMMIT_BRANCH =~ '^feature\.*\/.*
when: never
I expected this to match on branches like feature/issues-70, feature/issues-771, etc.
The regex rule you used is slightly off. First, you need surrounding / for the regex pattern. Second, the \. in the pattern will mean to match a literal . character, which is not what you want, based on the branch names you expect to match. Lastly, you need a matching rule for the job to be created at all (the default case when your rule doesn't match).
This should match all the example branch names you provided:
rules:
- if: $CI_COMMIT_TAG
when: never # same as except: - tags
- if: $CI_COMMIT_BRANCH == "test"
when: never # same as except: - test
- if: $CI_COMMIT_BRANCH =~ /^feature\/.*/
when: never
- when: on_success # the default when the first rule doesn't match

Simplify rules in GitLab CI/CD pipeline

I see this kind of syntax for rules:if and rules:when in GitLab CI/CD pipeline a lot:
job:
script: echo "This job does NOT create double pipelines!"
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: never
- when: always
Is that also equivalent to this?
job:
script: echo "This job does NOT create double pipelines!"
rules:
- if: $CI_PIPELINE_SOURCE != "push"
- when: always
rules mean that job is evaluated in order until the first match.
first rules
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: never
- when: always
are equivalent second rules
rules:
- if: $CI_PIPELINE_SOURCE != "push"
but, I recommend using the first rules, because it is easy to know and easy to read.
I have a test for their two rules. first, I set the IS_JOB_ON variable to off, that pipeline only test job 3 create, and second set IS_JOB_ON to on, three jobs are created.
first .gitlab-ci.yml and Pipeline
second .gitlab-ci.yml and Pipeline
sample:
variables:
IS_JOB_ON: 'off'
test job 1:
script:
- echo "run job 1"
rules:
- if: $IS_JOB_ON == "off"
when: never
- when: always
test job 2:
script:
- echo "run job 2"
rules:
- if: $IS_JOB_ON != "off"
test job 3:
script:
- echo "run job 3 always run"

How to run Gitlab CI only for specific branches and some rules?

I am trying to build ci according to the following criteria:
Run only when pushing to master branch - already implemented
Run on commit [run ci] - already implemented
Manual launch from the Run pipeline - already implemented
do not launch when pushing to the master branch when only Tag changes
do not start in other cases if they do not fall under the first 3 options
The tag may change without changes in other files and do not need to run the assembly, I tried this
if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG =~ /^$/
but it doesn't work if there are changes in both files and tags.
I will be glad to help
My .gitlab-ci.yml
image: busybox:latest
build1:
stage: build
rules:
- if: $CI_COMMIT_MESSAGE =~ /\[run ci\]/
- if: $CI_COMMIT_BRANCH == "master"
- when: manual
allow_failure: false
script:
- echo "Do your build here"
test1:
stage: test
rules:
- if: $CI_COMMIT_MESSAGE =~ /\[run ci\]/
- if: $CI_COMMIT_BRANCH == "master"
- allow_failure: false
script:
- echo "Do a test here"
- echo "For example run a test suite"
deploy:
stage: deploy
rules:
- if: $CI_COMMIT_MESSAGE =~ /\[run ci\]/
- if: $CI_COMMIT_BRANCH == "master"
- allow_failure: false
script:
- echo "Do a test here"
- echo "For example run a test suite"

How to combine rules in gitlab ci/cd

I would like to create rule for:
changes in folder foo && branch = master && tag pushed
My current rules not working:
rules:
- if: $CI_COMMIT_TAG && '$CI_COMMIT_BRANCH == "master"'
- changes:
- foo/**/*
Looks like gitlab take like a OR.
What is wrong?
As stated in the gitlab documentation:
To conjoin if, changes, and exists clauses with an AND, use them in the same rule.
So it should be:
rules:
- if: $CI_COMMIT_TAG && '$CI_COMMIT_BRANCH == "master"'
changes:
- foo/**/*

Resources