How do I run a .gitlab-ci.yml job only on a tagged Master branch?
job:
script:
- echo \"Do something\"
only:
- master
- tags
The
My solutions was
job:
script:
- echo "Do something"
only:
refs:
- master
- tags
variables:
- $CI_COMMIT_BRANCH == "master"
Thanks to others like Matt Alioto who posted about the open issue (which is labeled Product Vision 2019
so hopefully they knock it out this year).
Specific to Carlson Cole's question, this would work:
job_for_master_no_tags:
stage: deploy
script:
- echo "Release to Staging"
only:
- master
job_for_master_tags_only:
stage: deploy
script:
- echo "Release to Production"
only:
- tags
except:
- /^(?!master).+@/ # Ruby RegEx for anything not starting with 'master'
- /^(?!master).+/
(without the @
) it doesn't work - learned that the hard way This behavior is not yet supported by gitlab-ci, although there is an open issue to add it.
In the meantime I've also heard anecdotal reports that
only:
- master
only:
- tags
will get the job done (as well as anecdotal reports that it won't).
I made it work and this is my working code snippet, all others were not working for me
only:
- tags # please mention the 's' compared to Sergio Tomasello's solution
except:
- branches
I use 11.4.3
This behavior will be introduced in version 12.
Open issue was recently update:
Jason Lenny @jlenny changed title from {-Update .gitlab-ci.yml to support conjunction logic for build conditions-} to Conjunction logic for build conditions MVC · 2 days ago
Jason Lenny @jlenny changed milestone to 12.0 · 2 days ago
(fingers crossed)
A solution is to use the except
keyword to exclude all the branches, in conjunction with only
to run on tags, in this way you run your pipeline only on tag in master branch:
only:
- tags
except:
- branches
I'm using version 11.3.4