Rules changes between pushes of the same branch

If I add a rule to a job, can this rule compare to the last commit on the branch I pushed?

I built a relatively simple experiment repository to play around using few files and 2 jobs; the 1st one is supposed to always run, while the 2nd one only if the contents of 2 folders change.

  • I push a first commit which modifies said files, so I correctly see 2 jobs
  • I push a second one with no changes to those files and I still see the 2nd job

This seems to indicate that the pipeline compares by default to the default branch and not to the latest commit.

Is there a way to set this?

My test repo is public under my account it is called * test-job-rules*

I am using public gitlab with default shared runners, but I think my issue is quite a general one.

This is my current config;

default:
  image: mambaorg/micromamba # Docker image that is used by the CI/CD pipeline
  interruptible: true

# Switch from branch pipelines to merge request pipelines when a merge request is created
workflow:
  rules:
    # Ensures next rules do not block triggered pipelines
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
      when: never
    # always run for merge requests
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    # never run a branch pipeline if there is an open merge request for that branch
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    # the only allowed branch pipeline is that on the default branch
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

stages: # these define the sequential order of the jobs
  - test
  - build

first-job:
  stage: test
  script:
    - cat a.txt

second-job:
  stage: build
  script:
    - cd b && cat b.txt
    - cd ../c && cat c.txt
  # run only if there is a change or on master
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - changes:
        - b/**/*
        - c/**/*
1 Like

you can used rules:changes:paths and rules:changes:compare_to to compare main branch

  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - changes:
        paths:
          - b/**/*
          - c/**/*
        compare_to: main
1 Like

That’s the point: I do not want to compare to the main branch, but to the last commit on the branch in which I am working (which triggers a MR pipeline).

If I always compare to the main branch (which is what I see happening anyway) it doesn’t matter if the Nth commit within my MR doesn’t modify those specific files, because they will always be modified with respect to the main branch and the second job will always run.

What I am trying to avoid is running that second if I push any commit in my MR that doesn’t modify certain files.

1 Like

To be clearer:

  • in my main branch I have files A, B and C
  • 1st job always runs
  • 2nd job is supposed to run only if B or C are modified
  • now I make a new branch modifying e.g A & B
  • I push it and I trigger a MR pipeline
  • I should see both jobs run
  • now I push a new commit to my branch/MR in which I only modify A
  • I would like to not trigger the 2nd job
1 Like

Would compare_to: HEAD~ work?

It doesn’t seem like it. I tried @~1, HEAD^, HEAD~, none of that seems to do anything.

I am facing a very similar issue. Did you find the solution you were looking for?

Thank you in advance.