Push to gitlab triggers one or two pipelines

When I push changes to gitlab to an existing merge request, this triggers inconsistent numbers of pipelines.

When I push a change to a merge request, I expect gitlab to run one pipeline to see if the changes are ok, or if the pipeline fails. I am talking about the pipeline you see from the merge request:

However, sometimes two pipelines are triggered. Also, the source of the trigger in that pipeline changes.
Here is it how it can be reproduced. You have the following .gitlab-ci.yml file:

stages:
  - stage1
  - stage2


job1:
  stage: stage1
  script:
    - echo "Running default stage1, pipeline_source=$CI_PIPELINE_SOURCE"

job2:
  stage: stage2
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
    ####
  script:
    - echo "Running STAGE2!   pipeline_source=$CI_PIPELINE_SOURCE"

In the place ### you can put the following values:

  • You leave it out, you do not put anything there → 2 pipelines are started, and the one in the MR is a push pipeline
  • You add - when: never → 1 pipeline is started (push)
  • You add - when: always → 2 pipelines are started, and the one in the MR is a merge_request_event pipeline!
  • You add when: never or when: always (inside if rule) → 1 pipeline is started (push).

In addition, when you change the rule line to an inequality ($CI_PIPELINE_SOURCE != "push") in each case you will start 2 pipelines with the merge_request_event trigger EXCEPT if your rule is

    - if: $CI_PIPELINE_SOURCE != "push"
      when: never

Then you have again one single pipeline with a push trigger.

So here are two bugs as far as I can see:

  • Sometimes one pipeline is started (expected) and sometimes 2 pipelines.
  • In some cases the trigger source is push, but for no logical reason at all it also can be merge_request_event.

Is that problem known? Is there a bug report for these problems?

Hi @alex4200

This is likely to do with workflow:rules.

In my .gitlab-ci.yml files I have this:

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_ID
          when: never
        - when: always

Which means that for every MR I run one “branch” pipeline, and zero “MR” pipelines. You may of course choose to do the opposite!

No, it has nothing to do with it.

I do not have any workflow rules in my .gitlab-ci.yml` file.

So, if you have nothing at all in workflow:rules, by default you would get two pipelines for every push (an MR pipeline and a branch pipeline). When you say:

  1. You leave it out, you do not put anything there → 2 pipelines are started, and the one in the MR is a push pipeline
  2. You add - when: never → 1 pipeline is started ( push )
  3. You add - when: always → 2 pipelines are started, and the one in the MR is a merge_request_event pipeline!
  4. You add when: never or when: always (inside if rule) → 1 pipeline is started ( push ).

The two pipelines in cases 1. and 3. are one “branch” and one “MR” pipelines. The one pipeline in cases 2. and 4. are (I think) branch pipelines.

If you say something like:

stages:
  - stage1
  - stage2

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_ID
          when: never
        - when: always

job1:
  stage: stage1
  script:
    - echo "Running default stage1, pipeline_source=$CI_PIPELINE_SOURCE"

job2:
  stage: stage2
  script:
    - echo "Running STAGE2!   pipeline_source=$CI_PIPELINE_SOURCE"

then you should see one pipeline, with both jobs, which will output:

Running default stage1, pipeline_source=push

and

Running STAGE2!   pipeline_source=push

I guess I found the bug ticket in gitlab. I seems to be pipeline trigger twice when push to remote (#29605) · Issues · GitLab.org / GitLab · GitLab

1 Like

Interesting. This note looks useful.

Yes. It is a workaround. But not a fix.