Gitlab ci run job on master with release tag only

Viewed 6738

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?

4 Answers

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

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

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.

Related