0

Im trying some changes related to rules but not successful requirement: When a feature-A or feature-B is merged to develop , when merge req is approved the build stage should run. Thanks !

    build_development:
    stage: build
    # when: manual
    script:
    - | 
       echo "Intiating application docker build and push to ECR.."
  tags: 
    - docker
  # only:
  #   - merge_requests
  #   - develop
  # except:
  #   - prod
  # rules:
  #   - if: '$CI_MERGE_REQUEST_APPROVED == "true" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == /^feature-.*$/' 
  #     when: always
  rules:
    - if: '$CI_MERGE_REQUEST_APPROVED == "true" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^develop$/'
      when: always
dheeraj
  • 49
  • 1
  • 8

1 Answers1

0

There are two key elements to understand here:

  • rules: (as well as only:/except:) are evaluated at the time the pipeline is created
  • Merge request approvals do not trigger pipeline events

Therefore, if your merge request is not approved at the time the pipeline is created then any job requiring CI_MERGE_REQUEST_APPROVED to be true will not be included in the pipeline, even if the merge request is approved afterwards. Approving an MR will not change the existing pipeline and it will not trigger a new pipeline.

In order to get the effect you want, you will have to manually trigger a new pipeline from the MR. To do this, after approving the MR:

  1. Open the pipelines tab in the merge request
  2. Click the "run pipeline" button
sytech
  • 29,298
  • 3
  • 45
  • 86
  • How can I achieve the requirement below ? when a feature-A or feature-X is merged to develop (developer opens a merge req source branch : feature-A target branch : develop) . In my repository settings develop is a protected branch only maintainers can approve merge. When Merge is approved I want build to run automatically and build deploy manual using needs . Your help is greatly appreciated. – dheeraj Apr 20 '22 at 13:37
  • I have tried running the pipeline from MR -> pipelines. But problem is the build stage will be building an Docker image and MR approval from Dev Lead. So Im trying to avoid the manual for build. The running pipeline from MR is manual for this. – dheeraj Apr 20 '22 at 14:25
  • Unfortunately, the only way you _might_ be able avoid this manual interaction would be to develop a webhook receiver to receive [merge request events](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#merge-request-events) (specifically approval events) and then, on evaluation of the event, use the API to trigger the pipeline from the webhook receiver. Of course, this would require developing the code for such a webhook. @dheeraj – sytech Apr 20 '22 at 15:26
  • 1
    Im able to do this using below. rules: - if: '$CI_PIPELINE_SOURCE == "push" && $CI_BUILD_REF_NAME == "develop"' when: always and branch protection already in place https://stackoverflow.com/questions/70654510/how-to-run-pipeline-after-merge-request-approved-in-gitlab-ci – dheeraj Apr 20 '22 at 15:31