Bill
Bill

Reputation: 2973

Trigger Gitlab-CI Pipeline only when there is a new tag, with some rules

in the answer of this (Trigger Gitlab-CI Pipeline only when there is a new tag ), I understand to set only as filter

But How can I trigger the pipeline only:

  1. there is a new tag created, these tags are semantic versioning tags, how can I filter them? something like
only:
  - /[0-9]+\.[0-9]+\.[0-9]+/

but the tags are not always as 1.0.2, sometimes, they have beta or fc, such as 0.2.3-rc.1 or 2.3.5-beta, will the filter to be /[0-9]+\.[0-9]+\.[0-9]+.*/ is fine?

  1. second rule is, the trigger is only on master or main branch

Regarding the Semantic versioning regex, I found this gist

https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39

the sample is too complex

^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$

Upvotes: 2

Views: 3960

Answers (1)

Marty
Marty

Reputation: 1151

Using only is deprecated in favor of rules.

rules replaces only/except and they can’t be used together in the same job. https://docs.gitlab.com/ee/ci/yaml/#rules

Create a rule and check the predefined variable CI_COMMIT_TAG for your pattern.

job:
  script: echo "Hello, Rules!"
  rules:
    - if: $CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+.*/

You can list multiple of these rules. So if you want to execute the job also on every push to the master branch, you'd add another rule:

rules:
  - if: ...
  - if: $CI_COMMIT_REF_NAME == "master"

The extended pattern with .* at the end would be fine, but you could also be a bit more specific like this (-[0-9a-zA-Z.]+)? if you want to enforce a specific syntax.

Upvotes: 4

Related Questions