Distinguish merge of Merge Request and normal push in CI job not possible
Summary
When running a GitLab CI job you cannot easily determine whether the job is triggered by a push or by a merge of a Merge Request. Whereas for GitLab a direct push to a branch and a merge of an MR to a branch is much different (protected branches) it seems this is only something that can be distinguished internally (in GitLab) but not in the scope of the CI job.
Steps to reproduce
Define a job that only runs on master/main branch like this:
rules: - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
You would expect that this job only runs for pushes to master but in fact it also runs when you merge a Merge Request.
All MR related variables are not set if the MR is merged. E.g., CI_MERGE_REQUEST_IID is empty.
What is the current bug behavior?
Job is triggered for merge of MR's
What is the expected correct behavior?
Job should not be triggered for merge of MR's
Current Workaround
The only workaround I found is:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_MESSAGE !~ /Merge branch/ && $CI_COMMIT_MESSAGE !~ /See merge request/'
which of course is unsafe because any direct push could have such a commit message
Possible fixes
Provide additional value for CI_PIPELINE_SOURCE to distinguish from merge of MR
or
provide other variable value to do so
or
hint to other options to distinguish in a safe manner