Gitlab Workflow Config Prevent Duplicate Pipelines, Enable Branch, MR & Schedule Pipelines

215 Views Asked by At

The idea is to allow the following functions in the Gitlab Workflow config -

  • Branch pipelines
  • MR pipelines with MR labels
  • Schedule pipelines

And to run without creating duplicate pipelines (branch and MR) during the MR process.

The workflows are -

  1. Open a feature branch and push, this triggers two initial stages.
  2. If the initial stages pass, then an MR is created, and the more stages are introduced on top of the initial stages.

All of the above works in various combinations, but not all together.

The following workflow does not work for branch pipelines, all else is fine.

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_PIPELINE_SOURCE == "schedule"
    - if: $CI_COMMIT_REF_PROTECTED == "true"

Current versions

  • Gitlab v16.3.4-ee
  • Runner v16.3.0

Any ides or tips on the above would be much appreciated :~)

1

There are 1 best solutions below

0
Theo Sweeny On

A working Gitlab Workflow config can be seen here -

workflow: # Intent - Run for branch, merge request and schedule pipelines and prevent merge request duplicate pipelines
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule" # Run on schedule pipeline
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" # Run on merge request pipeline
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS # Do not run on both branch and merge request pipeline
      when: never
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000" # Do not run on merge request creation
      when: never
    - if: $CI_COMMIT_BRANCH # Run on branch pipeline

Each of the jobs extend rules like so

  dev:plan:
  stage: plan
  extends: [ .plan_rules ]

For plan_rules it was needed only for merge requests and not branch pipelines

So I tried

.plan_rules: # Intent - allow when merge request
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

But the above triggered a duplicate pipeline during the merge request pipeline.

By updating plan_rules to

.plan_rules: # Intent - allow plans when merge request
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_MERGE_REQUEST_ID != null

Duplicate pipelines no longer appear!