Gitlab CI/CD merge request - CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and Target branch name empty
Asked Answered
H

1

6

I want the Gitlab pipeline to be initiated only when there is a merge request. Not on commits to any branch.

only:
 refs:
  - merge_requests
changes:
  - "**/*.json"   except:
  - $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "master"
  - $CI_COMMIT_REF_NAME == "master"

But when I create a merge request there are two pipeline request initiated. One for

  • Detached merge request pipeline : this is initiated when the merge request is created (not yet approved). I want this pipeline run to be blocked. But this run has all MR variables populated.

FYI I have some rules to set variable values and read this is causing this issue. I tried the below as mentioned here:(https://gitlab.com/gitlab-org/gitlab/-/issues/201845) to block the Detached pipeline and it works. But now I have no way to access the MR variables.

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_OPEN_MERGE_REQUESTS
      when: never
  • Merged branch pipeline: this is initiated after the merge is completed/approved. I want only this pipeline to be initiated. But on this branch all the merge request variables - CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and CI_MERGE_REQUEST_TARGET_BRANCH_NAME are all EMPTY.

How to achieve the following:

Block pipeline run for Detached branch request and get the MR variables for Merged branch pipeline run?

Update 1: I enabled merged result pipeline on settings and removed the MERGE_REQUEST_IID rule. Now the merge RESULT pipeline is running instead of Detached but the merged result pipeline is executing before the merge is approved? Sorry this is confusing. Is there anyway to initiate a pipeline ONLY after the merge is approved and get access to MR variables in that pipeline run?

Hydrosphere answered 20/2, 2022 at 2:19 Comment(0)
T
5

As mentioned in the predefined variables documentation, merge request variables only exist for the merge request pipelines running for the MR, when configured.

A pipeline running on the merged branch is just a normal branch pipeline, thus will not have these variables. You cannot use these variables in the merged branch pipeline.

Thwack answered 20/2, 2022 at 2:27 Comment(6)
Is "Detached merge request pipeline" the only pipeline run that has access to MR variables?Hydrosphere
@Hydrosphere yes, that or other merge request pipelines, including merged result pipelines (as with merge trains).Thwack
How to block the Detached merge request pipeline and have only the merged RESULT pipeline running?Hydrosphere
@Hydrosphere my understanding is when you enable merged result pipelines, that is the default behavior, except in the two circumstances described in the documentation. In those two exceptional cases, it will fallback to a detached merge request pipeline.Thwack
I just enabled merged result pipeline on settings. Why is merged result pipeline executing before the merge is approved? Sorry this is confusing. Is there anyway to initiate a pipeline ONLY after the merge is approved and get access to MR variables in that pipeline run?Hydrosphere
@Hydrosphere you can make a job or workflow rule to only run when $CI_MERGE_REQUEST_APPROVED is true if you only want the pipeline to run after it has been approved (but not merged). You, however, cannot get these variables for any pipeline created as a consequence of merging the pipeline; that is always just a normal branch pipeline and does not have access to the MR variables from the MR (as stated in the answer).Thwack

© 2022 - 2024 — McMap. All rights reserved.