gitlabgitlab-cicicdgitlab-api

GitLab API merge request seems to initiate a CICD pipeline that it should not


In a particular project I have defined a cicd pipeline with this rule:

include:
  - '.some_other.yml'
  - project: 'another/project'
    file: '/.gitlab-ci.yml'
    ref: 'main'
variables:
  bla: "blabla"
  

.package_pipeline: &package_pipeline
  stage: build
  timeout: 1 hours
  script:
    - ssh bla bla

  artifacts:
    paths:
      - "./artefacts/*"

  tags:
    - mytag

  only:
    - web

package_pipeline_job1:
  <<: *package_pipeline
  needs:
    - job: do_packing
      artifacts: true
    - job: package_sss
      artifacts: true
  variables:
    SRC_URL: "bla"

Now whenever I raise a MR on the gitlab website, no pipeline gets initiated as I would expect as the above should only be triggered when we press "CI/CD->pipelines->run pipeline"

However, now I have started to raise MR through the GitLab API by following:

curl -v --request POST --header "PRIVATE-TOKEN: MyToken" "https://gitlab.XX.com/api/v4/projects/ID/merge_requests" --data "source_branch=branch_1&target_branch=branch_2&title=Hi MR"

When doing so the pipeline from above seem to be triggered, which I cannot understand why, nor do I know how to prevent it.


Solution

  • As by the CI/CD YAML reference for the only keyword only: web will catch

    Run pipeline in the GitLab UI, from the project’s Build > Pipelines section
    

    Since you are using the "normal" API, this is considered to be the same action as manually triggering it through the UI. If you want to have a distinctive API my recommendation would be to use pipeline triggers.

    One additional remark: for new pipelines, I would recommend the rules keyword like

    rules:
        - if: $CI_PIPELINE_SOURCE == "web"
    

    since only has been deprecated for a while now.