43

Is there a way to instruct the pipeline to only do a step for certain tags that matches a regular expression?

I would like it to do a deploy when I push a tag on the format 1.2.3 (for example). Is there a way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Magnus Lundberg
  • 596
  • 1
  • 4
  • 17

3 Answers3

56

This should only be run for refs that are not branches named matching the given regex.

   job:
      only:
        - /^(\d+\.)?(\d+\.)?(\*|\d+)$/
      except:
        - branches
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
  • 1
    Thanks for your suggestion with except. Unfortunately you missed the mandatory slashed at start and end of your regex. @see https://docs.gitlab.com/ee/ci/yaml/#only-and-except-simplified – JaXt0r Sep 05 '18 at 10:52
  • This part of the regex is odd: `(\*|\d+)`. Putting literal asterisks in a git tag would be unconventional, at best. – Joe Atzberger Oct 23 '20 at 19:34
31

Yes, you can do this with the only option:

job:
  # Use regexp
  only:
    - /^issue-.*$/


job:
  # Use special keywords
  only:
    - tags
    - triggers
    - schedules

See only/except (basic).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • 14
    As i just researched some CD/CI Features of gitlab i came acress this. Beware of one "feature": only is not capable of combining regex and special words, so only: - /^issue-.*$/ - tags will trigger on any tag _and_ any tags, branches,... starting with "issue-". As i understand this, there is no way to limit the regex to be used on only tags or only branches (correct me if I'm wrong - I really hope someone does!). – FastJack Jun 29 '18 at 06:53
  • not quite sure.. what about combining it with `except: [tags]` – Rufinus Jun 29 '18 at 10:59
  • 10
    you probably mean - combine it with `except: -branches` – Chris Oct 23 '18 at 12:51
16

You can also make use of rules:

job:
  script: echo "Hello, World!"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/'
ricekot
  • 300
  • 4
  • 6
  • 4
    As `only and except are not being actively developed. rules is the preferred keyword to control when to add jobs to pipelines.` ([source](https://docs.gitlab.com/ee/ci/yaml/#only--except)) it appears [rules](https://docs.gitlab.com/ee/ci/yaml/#rules) is now the way to go. – pxul Aug 20 '21 at 09:18