Problem with merge request pipelines

hi

i have this problem that whenever a merge request is accepted some pipelines start to run.

also we have the same pipeline for pushing a branch to gitlab and also creating a new merge request

this is my job :


build:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS clean install -Dmaven.test.skip=true -P prod
  artifacts:
    expire_in: 800 mins 0 sec
    paths:
      - app.jar
      - Dockerfile
      - docker
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - /node_modules 
    policy: pull-push
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "develop"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_PIPELINE_SOURCE != "push" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_REF_NAME == "master"' 
    - if: '$CI_PIPELINE_SOURCE != "push" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_REF_NAME == "develop"' 

i dont want the pipeline to be run after the merge request is accepted

this first rule always succeeds:

- if: '$CI_PIPELINE_SOURCE == "push" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "develop"'

Because on a push event, the CI variable $CI_MERGE_REQUEST_TARGET_BRANCH_NAME is not set, so that "" != "master" is true.

Instead, what I think you need is to not trigger the pipeline when there are CI_OPEN_MERGE_REQUESTS:

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

Feel free to have a look at this sample project, based on your job. You could also fork it and play around to see if it does what you need