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

Viewed 6116

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?

4 Answers

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.*/

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.

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
Related