Today I Learned (July 12, 2024)

How to prevent a GitLab CI/CD pipeline from running on tag events

According to the GitLab CI/CD documentation, a tag event in GitLab CI/CD is a push event (just like any other push of a commit on any branch), meaning that jobs will run for tag events unless $CI_PIPELINE_SOURCE = "push" is excluded in the rules section.

Now, in some scenarios—such as when we want to run both tag and release in the same pipeline—, a tag event should not trigger an additional pipeline run. Following this StackOverflow post, this is possible by excluding tag push events by checking if the CI_COMMIT_TAG variable has a non-empty value.

the_job:
    stage: the_stage
    script: 
        - echo "Doing my job."
        - ...
    rules:
        - if: '$CI_PIPELINE_SOURCE != "push" || $CI_COMMIT_TAG == null'
          when: never

:bulb: Note that testing for unset pipeline variables means checking for the null value. I.e. $CI_COMMIT_TAG == nullin this case.