Gitlab Trigger CI/CD Pipeline only when PR/MR is approved

741 Views Asked by At

I have added the following rule in my .gitlab-ci.yml file to run the CI/CD pipeline only when PR is merged to the main but its running on every PR/MR request raised not after approval.

- if: $CI_MERGE_REQUEST_APPROVED && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"
   when: always

Can anyone point me if I am doing something wrong? I have protected main branch.

Thanks in advance.

1

There are 1 best solutions below

0
iamabhaykmr On

After research found that GitLab CI/CD does not natively provide a direct way to trigger a pipeline only when a Merge Request (MR) to the main branch is approved. Pipelines are generally triggered by code pushes, merge requests, and other events, but they don't have built-in features to directly trigger based on MR approval.

Just having this rule which will trigger pipeline when commit is made to main branch. Since after MR/PR approval gitlab creates a merge commit to main , it will trigger the pipeline after approval merge commit to main. Its working fine. This is what I wanted.

  rules:
    - if: $CI_COMMIT_BRANCH == "main"