How do I run a .gitlab-ci.yml job only on a tagged Master branch?
job:
script:
- echo "Do something"
only:
- master
- tags
The above code will run if either condition exists: a Master branch or a tagged commit.
My goal is to have this run for a production deploy, but it would require that it be on the Master branch and that it be tagged (with a version). Otherwise, I'll have another job that will push to staging if its missing a tag.
gitlab-ci. yml , you can specify some jobs with the tag testing . If a runner with this tag associated is available, it will pickup the job. In Git, within your repository, tags are used to mark a specific commit.
To configure a job to be executed only when the pipeline has been scheduled, use the rules keyword.
GitLab CI uses a YAML file ( . gitlab-ci. yml ) for project configuration. This file is placed in the root of the repository and defines the project's Pipelines, Jobs, and Environments.
Run a pipeline manually from the Branches viewIn Bitbucket, choose a repo and go to Branches. Choose the branch you want to run a pipeline for. Click (...) , and select Run pipeline for a branch.
How do I run a .gitlab-ci.yml job only on a tagged Master branch? job: script: - echo "Do something" only: - master - tags The above code will run if either condition exists: a Master branch or a tagged commit.
A .gitlab-ci.yml file might contain: In this example, the build-code-job job in the build stage runs first. It outputs the Ruby version the job is using, then runs rake to build project files. If this job completes successfully, the two test-code-job jobs in the test stage start in parallel and run tests on the files.
15 How to trigger a specific job in gitlab 1 Gitlab-ci.yml Configuration 9 Gitlab CI: Run Pipeline job only for tagged commits that exist on protected branches 0 Gitlab-CI run Stage conditionally 0 GitLab CI Pipeline not triggered for events on default branch 2
To delegate some work to GitLab CI you should define one or more jobs in .gitlab-ci.yml. Jobs should have names and it's your responsibility to come up with good ones. Every job contains a set of rules and instructions for GitLab CI, defined by special keywords.
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
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 😕 I made it work with this 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
I had the same problem. I wanted to trigger a deploy to our staging-environment on a push or merge, and only when applying a tag deploy it our production-environment.
We need 2 variables for this: $CI_COMMIT_BRANCH
and $CI_COMMIT_TAG
. With these variables we could deduct if the pipeline was triggered by a commit or a tag. Unfortunately the first variable is only set when committing on a branch, while the second variable only is set on applying a Tag. So this was no solution...
So I went for the next-best setup by only do a production-release when a tag is set by specified conventions and by a manual trigger. This is my .gitlab-ci.yml file:
stages:
- deploy:staging
- deploy:prod
deploy-to-staging:
stage: deploy:staging
rules:
- if: $CI_COMMIT_BRANCH == 'master'
script:
- echo "Deploying to Staging..."
deploy-to-production:
stage: deploy:prod
rules:
- if: $CI_COMMIT_TAG =~ /^v(?:\d+.){2}(?:\d+)$/
when: manual
script:
- echo "Deploying to Production..."
If you really want to automate this, you have to do a little scripting to find out if the applied tag actually belongs to a commit that is on the master-branch. Check this comment on the GitLab issuetracker for more information: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/31305#note_28580169
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With