I would like to create a pipeline that is only run if both of the following conditions are met:
- A tag refers to the given commit
- The commit exists on any protected branch (i.e. master)
- Optional: The job should be run whenever a tagged unprotected branch is merge (with a merge request) into a protected branch or if a tag is added to a protected branch.
I've tried:
publish:
stage: publish
script:
- echo "Publish!"
rules:
# Only publish if tag given and commit is present on a protected branch
- if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
Which does not work as either the $CI_COMMIT_TAG is set or the $CI_COMMIT_REF_PROTECTED is set to true.
I am aware of the similar Questions: Gitlab ci run job on master with release tag only and How to run a gitlab-ci.yml job only on a tagged branch?.
Also I know there is/was a wide discussion in the issues from gitlab, with some solution (or something close to this) like this.
The general problem seems to be that it is not possible in gitlab to determine reliable if a commit if on a given branch as the information (git history) for this is not given.
This question is to keep track of a proper solution within gitlab CI for this common use case.