Home:ALL Converter>GitLab CI/CD: Trigger pipeline only when a specific extension was added to a folder AND Merge Request

GitLab CI/CD: Trigger pipeline only when a specific extension was added to a folder AND Merge Request

Ask Time:2021-04-15T20:29:14         Author:french_dev

Json Formatter

On my gitlab repo I have to trigger pipeline only when a specific folder have changes AND when it's a Merge request (both condition). Especially on .zip file extension, i-e, add a new zip file in this folder, create a Merge request, then run the pipeline.

This is my initial pipeline yaml code:

trigger-ci-zip-file-only:
  stage: prebuild
  extends:
    - .prebuild
    - .preprod-tags
  variables:
    PROJECT_FOLDER: "my-specific-folder"
  before_script:
    - echo "Job currently run in $CI_JOB_STAGE"
    - touch ${CI_PROJECT_DIR}/$PROJECT_FOLDER/prebuild.env
    - cd $PROJECT_FOLDER
  only:
    refs:
      - merge_requests
    changes:
      - ${PROJECT_FOLDER}/*.zip
  artifacts:
    reports:
      dotenv: ${CI_PROJECT_DIR}/${PROJECT_FOLDER}/prebuild.env
  allow_failure: false

As you can see, my pipeline have to be triggered only when there is a change in a specific folder on zip files and only on MR. But in this state, the pipeline is always running when MR is creatd or when I push on an existing MR, even if there is no changes or adding in the specific folder.

I also tried to make some changes on the pipeline yaml code like this:

only:
  refs:
    - merge_requests
      changes:
      - ${PROJECT_FOLDER}/**/*.zip

But the pipeline always running.

Also I tried this:

trigger-ci-zip-file-only:
  stage: prebuild
  extends:
    - .prebuild
    - .preprod-tags
  variables:
    PROJECT_FOLDER: "my-specific-folder"
  before_script:
    - echo "Job currently run in $CI_JOB_STAGE"
    - touch ${CI_PROJECT_DIR}/$PROJECT_FOLDER/prebuild.env
    - cd $PROJECT_FOLDER
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - changes:
    - ${PROJECT_FOLDER}/**/*.zip
  when: always
  artifacts:
    reports:
      dotenv: ${CI_PROJECT_DIR}/${PROJECT_FOLDER}/prebuild.env
  allow_failure: false

But the pipeline always running too.

How to make sure the pipeline only run on Merge Request AND only when a .zip file was added to the specific folder ?

Author:french_dev,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67108485/gitlab-ci-cd-trigger-pipeline-only-when-a-specific-extension-was-added-to-a-fol
yy