yamlgitlab-ci

How to run a Gitlab-CI job only if a tag is created on the main branch


I have a job to create a release when a tag is created but I also need a rule to only allow it to create a release if the tag was created for the main branch. I'm not sure what I need to do for this?

I've tried this rule however this fails in the pipeline because this command stops it from running for some reason.

  rules:
  - if: $CI_COMMIT_TAG && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:

Solution

  • Thanks to Teemu for their answer which led me in the right direction for sure. The best result I've gotten is with this snippet.

    Script:
    - git log -1 --pretty='%D' $CI_COMMIT_TAG > tag_info.txt
    - | 
        if ! grep -q 'origin/master' tag_info.txt; then
          exit 1
        fi
    

    This sends the information from the tag I just created to a text file and grep checks if the text file contains the branch I want, if it doesn't the pipeline fails otherwise I can carry on with my release.