Run job when CHANGELOG.md is not changed

I try to run the step in CI/CD pipeline once the CHANGELOG.md file is not modified. This means that developers have not documented the changes being made in code. My intention is to fail the pipeline once it happens.

I wrote the following code but it doesn’t work:

.pre-verify:
  stage: verify
  tags:
    - ubuntu-1804
    - shell
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - "CHANGELOG.md"
      when: never
  script:
    - echo "CHANGELOG.md is not updated."
    - exit 1

Your .pre-verify is a hidden job, so unless there’s more to your CI config, that job won’t run.

This works for me:

stages:
    - verify

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_IID
          when: always
        - when: never

pre-verify:
    stage: verify
    tags:
        - ubuntu-1804
        - shell
    rules:
        - if: $CI_MERGE_REQUEST_IID
          changes:
              - CHANGELOG.md
          when: never
          allow_failure: false
        - when: always
    script:
        - echo "CHANGELOG.md is not updated."
        - exit 1

Can you explain this please:

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_IID
          when: always
        - when: never

When you push to a branch which has an associated MR, by default GitLab will start a pipeline for the branch AND a pipeline for the MR. The workflow keyword allows you to control when pipelines are created.

You probably don’t want that when: never at the end, most likely you want to think about things like tags, etc. first, but that set of rules says “create a pipeline for MRs, don’t create pipelines for branches or anything else”.

Thank you

1 Like