Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab: `changes` to files does not re-run if a previous stage failed

Imagine a simple .gitlab-ci.yml file with the following

stages:
  - test
  - build

test_job:
  stage: test
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
  script:
    - exit 1

build_job:
  stage: build
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
      changes:
        - web-app/**/*
  script:
    - echo "Building..."

The first time this pipeline is run, you will see both jobs, however the build_job will not be run because the test_job failed (exit 1).

Correcting the exit 1 (to exit 0, for example) and re-running, you will now only see the test_job because to gitlab, the web-app files havent changed, yet they havent successfully run previously.

So how do you ensure the build_job is run to success??

like image 735
Christian Avatar asked Feb 03 '26 22:02

Christian


1 Answers

In the scenario you described, your second commit contains only 1 change to your .gitlab-ci.yml file, therefore your changes: rule correctly causes the build_job to be excluded from the pipeline as you have configured the pipeline.

For branch pipelines, GitLab will not consider previous commits when evaluating the changes: rules.

However, with pipelines for merge requests, all changes in the merge request are tested. Which sounds like the behavior you are expecting.

If your goal is for efficient pipelines tracking your features branches, your best course(s) of action might be something like this:

  1. Always run the build job on branch pipelines
  2. Only apply the changes: rule for pipelines for merge requests
  3. Optionally, exclude branch pipelines when an MR is open
  4. Optionally, adopt a workflow whereby which you open (draft) merge requests when creating your feature branches

An optimized CI configuration that does not skip build job on branches may look like this:

build_job:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:  # in the case of merge requests, we can be more efficient
        - web-app/**/*
    - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
      when: never  # skip branch pipelines when MR pipelines exist
    - if: '$CI_COMMIT_BRANCH' # but in branch pipelines, build every time
like image 180
sytech Avatar answered Feb 06 '26 11:02

sytech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!