6

I would like to build docker image on master branch only when release tag is set. This is my .gitlab.ci:

build:
  rules:
    - if: '$CI_COMMIT_TAG != null && $CI_COMMIT_REF_NAME == "master"'
  script:
    - echo "Building $IMAGE:${CI_COMMIT_TAG}"

This does not work, I merged to master and release tag, but the build job did not even start.

I also tried with only section:

build:
  only:
    - master
    - tags
  script:
    - echo "Building $IMAGE:${CI_COMMIT_TAG}"

This run everytime, even when CI_COMMIT_TAG does not exists. Is there a way, how to force to run job only if CI_COMMIT_TAG exists on master branch?

dorinand
  • 1,397
  • 1
  • 24
  • 49

5 Answers5

5

There is no proper build in solution in gitlab so far for this problem. To keep track of the development of a proper solution and to keep a working workaround updated I created: Gitlab CI: Run Pipeline job only for tagged commits that exist on protected branches

Kound
  • 1,835
  • 1
  • 17
  • 30
2

The job "deploy-latest" only runs if the branch is master and if it's been tagged.

deploy-latest:
  tags: [shell]
  stage: deploy
  script:
    - echo "whatever"
  only:
    - tags && master

Partiban
  • 136
  • 3
  • 9
  • 1
    No, this is NOT working. `The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines.` – CodingNow Feb 23 '22 at 07:16
0

I am looking for the solution as well and it seems it is not possible because for example if you tag your main branch with tag:"0.333" you will get:

      CI_COMMIT_TAG="0.333"
 CI_COMMIT_REF_SLUG="0-333"
 CI_COMMIT_REF_NAME="0.333"

Unfortuana

akelsey
  • 99
  • 1
  • 4
0

You cannot do it since tag is not related to a branch but for a commit, so you can compare the tag's commit to master's HEAD, this is my solution: the condition only checks that the last commit was on a tag. in the script I compare the tag's commit hash and master HEAD's commit hash and fails the step if they are not equal

rules:
- if: '$CI_COMMIT_TAG != null
  when: always
...
script:
# verifying that the tag that triggered the pipeline is done on the head of master
- latest_commit=$(git rev-parse master)
- echo "CI_COMMIT_SHA - $CI_COMMIT_SHA"
- echo "latest_commit - $latest_commit"
- if [ $latest_commit != $CI_COMMIT_SHA ]; then echo "tagging and deploying a version is allowed only on the HEAD of master"; exit 1; fi
lior.mor
  • 158
  • 1
  • 9
-3

With

build:
  only:
    - tags

you can run a job only when a tag was set (or pushed). If you have master in there, it runs on every commit on master. Without master it only runs after setting a tag.

Thomas Löffler
  • 5,922
  • 1
  • 14
  • 29
  • 1
    Thanx. I do not want to run it every time on master. Only when I tag it. And also, dont want to enable tag on another branches. – dorinand Mar 20 '20 at 07:47
  • If you don't want it run every time on master, you shouldn't add it to the `only` part. You can look at the docs here: https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic – Thomas Löffler Mar 20 '20 at 12:04
  • I am trying to run it only if its master branch and tag. That means, If I release tag 0.1.0, and than merge something to master, I do now want to run pipeline again with tag 0.1.0 everytime I merge something. I want to merge to master few bugfixes or features and than make new release 0.1.2. I am using this tag as docker image tag. – dorinand Mar 20 '20 at 12:09
  • 1
    With `tag` it runs only on tagging. Not when merging or doing other tings. – Thomas Löffler Mar 20 '20 at 12:22
  • I know, thats the porblem. I need to run it only on master when tagging. – dorinand Mar 20 '20 at 12:56
  • OP wants it to run ON MASTER only WITH TAG. Your example will run on branch commits as well. – Dev Null Dec 10 '20 at 07:35