Gitlab CI automatical run pipeline on merge request

Viewed 3379

I have below pipeline. After creating merge request, created Detached merge request pipeline with failed status (app1 - no stages/jobs). In scope of below pipeline need to run pipeline when merge request is created and after merging changes main. Flow described in here Gitlab CI Child pipeline Below pipeline does not work.


workflow:
    rules:
        - if: '$CI_PIPELINE_SOURCE == "schedule"'
        - if: '$CI_PIPELINE_SOURCE == "web"'
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        - if: '$CI_PIPELINE_SOURCE == "push"'

stages:
    - child-pipelines

app1:
    stage: child-pipelines
    variables:
        COMPONENT NAME: 'app1'
    trigger:
        include:
        - local: .ci/.gitlab-ci.yml
        strategy: depend
    rules:

        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
          changes:
            - test1/**/*
          variables:
            DEPLOY_RELEASE: '11111'

        - if : '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
          changes:
            - test1/**/*
          variables:
            DEPLOY_RELEASE: '222222'
app2:
    stage: child-pipelines
    variables:
        COMPONENT NAME: 'app1'
    trigger:
        include:
        - local: .ci/.gitlab-ci.yml
        strategy: depend
    rules:

        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
          changes:
            - test2/**/*
          variables:
            DEPLOY_RELEASE: '11111'

        - if : '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
          changes:
            - test2/**/*
          variables:
            DEPLOY_RELEASE: '222222'


1 Answers

If you want to run a pipeline when merge request was created and after merge to main branch, take a look this example:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
    
job1:
  script:
    - echo "This job runs in merge request and also after merge to main branch"

According to Gitlab documentation.

If you want to run a rule for the entire pipeline:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  
job1:
  script:
    - echo "This job runs in merge request pipelines"
    
job2:
  script:
    - echo "This job also runs in merge request pipelines"

If you want to run a rule in certain job:

job1:
  script:
    - echo "This job runs in merge request pipelines"
  only:
    - merge_requests
Related