Ganu
Ganu

Reputation: 432

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

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

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

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?

Upvotes: 6

Views: 9718

Answers (1)

sytech
sytech

Reputation: 41061

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.

Upvotes: 5

Related Questions