Add job to pipeline only if specific file has changed doesnt work

Hi,
I am trying to add jobs to pipeline only when a specific file is changed in a commit to a feature branch. I am running a merge results pipeline with merge trains

But the rule on all different configs doesnt work:

eu-west-1--prod-plan:
  stage: tf_plan
  image: tf-builder-0.2
  environment:
    name: production
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - changes:
        - tfvars/**/eu-west-1-prod.*
        # - "tfvars/eu-west-1-prod.tfvars" 
        # - "**/eu-west-1-prod.tfvars"
  script:
    - echo....

The job will always be triggred regaurdless of if the file was changed or not.

Hi there,

It could be that an if statement is required. Try something like:

eu-west-1--prod-plan:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH  # change depending on when you want to run the job
      changes:
        - tfvars/**/eu-west-1-prod.*

@paula.kokic Thanks for the help - this fix did not help, for some reason every push to feature branch editing only specifc file, will trigger all the plan jobs in the pipeline.

Hi,

I would suggest reviewing your paths and making sure the wildcards are set correctly. I’d also have to play around a bit, as I’m not really a master in this. Reference: CI/CD YAML syntax reference | GitLab

@paula.kokic Thanks, I fixed the issue using compare_to between my feature branch and the main (default) branch:

rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH
      changes:
        compare_to: 'refs/heads/main'
        paths:
          - tfvars/eu-west-1-dev.tfvars
1 Like