'How to limit a job in gitlab ci to a tag matching a pattern?

I want to be able to trigger a deployment to a special server every time a tag matching a pattern is pushed.

I use the following job definition:

# ...
deploy to stage:
  image: ruby:2.2
  stage: deploy
  environment:
    name: foo-bar
  script:
    - apt-get update -yq
    - apt-get install -y ruby-dev
    - gem install dpl
#    - ...
  only:
    - tags

Now my question: how can I limit this to tags that have a certain name, for example starting with "V", so that I can push a tag "V1.0.0" and have a certain job running?



Solution 1:[1]

Only accepts regex patterns so for your use case it would be:

only:
  - /^V.*$/
except:
  - branches
  - triggers

Solution 2:[2]

The best way to do is filtering by the Gitlab CI/CD variables matching your pattern

only
  variables:
    - $CI_COMMIT_TAG =~ /^my-custom-tag-prefix-.*/

As the documentation says:

  • CI_COMMIT_TAG: The commit tag name. Present only when building tags.

Solution 3:[3]

Since only / except are now being deprecated, you should now prefer the rules keyword

Things get pretty simple, here's the equivalent using rules:

rules:
  # Execute only when tagged starting with V followed by a digit
  - if: $CI_COMMIT_TAG =~ /^V\d.*/

Solution 4:[4]

rules should help you here. Below would restrict the job to run only if its triggered due to GIT Tag matching the pattern for e.g. V1.0.0 or V1.14.45 (see + after \d)

rules:
    - if: '$CI_COMMIT_TAG =~ /^V\d+.\d+.\d+$/'
      when: manual

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tesch1
Solution 2 marc_s
Solution 3
Solution 4 Sanjay Bharwani