Skip to content

Merge pipeline not created until an additional job with simple rules is defined

Summary

Gitlab is apparently unable to detect that a job requires a merge pipeline. The problem solve itself when using lint stage instead of .pre

Steps to reproduce

The following pipeline won't trigger a merge pipeline

variables:
  PRE_COMMIT_HOME: /pre-commit/
  MAIN_BRANCH: main

stages:
- .pre
- lint
- deploy
- .post

pre-commit:
  stage: .pre
  image: python:3.9
  cache:
    - key: pre-commit-environment
      paths:
        - $PRE_COMMIT_HOME
  script:
    - pip install pre-commit
    - pre-commit run -a
  rules:
    - if: '(
        ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $MAIN_BRANCH) ||
        ($CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $MAIN_BRANCH)
        )'

but this one will because of the added job

variables:
  PRE_COMMIT_HOME: /pre-commit/
  MAIN_BRANCH: main

stages:
- .pre
- lint
- deploy
- .post

publish_local:
  stage: deploy
  script:
    - pip install build twine
    - python -m build
    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $MAIN_BRANCH'

pre-commit:
  stage: .pre
  image: python:3.9
  cache:
    - key: pre-commit-environment
      paths:
        - $PRE_COMMIT_HOME
  script:
    - pip install pre-commit
    - pre-commit run -a
  rules:
    - if: '(
        ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $MAIN_BRANCH) ||
        ($CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $MAIN_BRANCH)
        )'

By parsing the additional job, gitlab will detect the pipeline has a merge pipeline, define the CI_MERGE_REQUEST_TARGET_BRANCH_NAME which will cause my pre-commit job to finally run.

Nb: The reason for defining my rule this way is to avoid having two simultaneous pipeline running the same job (push + merge pipeline). This was pointed out by checkcov tool.

What is the current bug behavior?

Gitlab does not detect a pipeline as a merge pipeline

What is the expected correct behavior?

Gitlab should be able to detect the pipeline has a merge pipeline

Relevant logs and/or screenshots

none

Output of checks

none

Results of GitLab environment info

none

Possible fixes

none

Edited by David Gallay