Search code examples
gitlabcontinuous-integrationgitlab-cipipeline

Gitlab CI Pipeline Runs When it Shouldnt


I'm trying to implement a gitlab ci pipeline that will only be triggered if files in a specific directory or its subdirectories are changed.

stages:
#  - pre
  - triggers

.schema_trigger: &trigger_config
  trigger:
    include: pipeline/zone.gitlab-ci.yml
    strategy: depend

#pre-stage:
#  image: centos:7
#  stage: pre
#  script:
#    - echo "This job ensures that when no migrations are added the pipeline is still successfull"

demo:
  stage: triggers
  variables:
    SCHEMA: demo
  <<: *trigger_config
  only:
    refs:
      - merge_requests
    changes:
      - schemas/demo/**/*.{sql,env}

Problem is that when i create a new merge request without changes in the schemas/demo dir the pipeline still runs. I've tried some configurations without success, including adding the only:refs clause with the only effect of creating a detached pipeline.

And this issue only presents itself on the first pipeline for the branch, all commits after are evaluated correctly and don't run.

How would i be able to achieve the goal of having the pipeline only run when files in the directory are changed?


Solution

  • To get the effect you want, you should use rules: instead of only: in order to combine the rule with changes: Something like:

    demo:
      # ...
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
          changes:
            - schemas/demo/**/*.{sql,env}
        # ...
    

    Also note that if no pipeline for the merge request runs, the branch pipeline will show up in the merge request UI instead. You can consider disabling branch pipelines altogether or making sure at least one other job is included on pipelines for merge requests.