10

Goal

The release pipeline should start a deployment for specific branches.
This should not happen (skip the job), if there are only documentation changes. (*.md files)

The problem

If you change multiple files, but only one file ends in .md, the build job is still skipped. The job does not run for any of the files.

https://docs.gitlab.com/ee/ci/jobs/job_control.html#onlychanges--exceptchanges-examples

So, is it even possible to specifcy a rule as mentioned above?

What I tried so far (an excerpt)

So, if "*.md" doesn't work, is it possible to revert it?
"**/!(*.md)" # Every file except *.md

This does not execute anything

  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      changes:
      - "**/!(*.md)" # Every file except *.md

This executes always

  rules:
    - if: $CI_COMMIT_BRANCH == "main"
    - changes:
      - "**/!(*.md)"

Question

Do I have to use custom variables to solve this problem or is there a simpler way?

kapsiR
  • 2,720
  • 28
  • 36
  • Hi, might not be elegant but you might be interested in something like [this](https://stackoverflow.com/questions/52908263/how-to-list-the-modified-files). It uses Gitlab variables that hold the SHA refs so you can run git command to detect change yourself in your shell script. You can abort the script on any desired conditions. – asinkxcoswt Dec 07 '21 at 22:19
  • I often find that running the pipeline with EVERY change is much more cost-effective than adding a lot of extra config to the pipeline and spending time getting it to work. My 2c. – Valentin Despa Dec 09 '21 at 10:18
  • 1
    @ValentinDespa You are absolutely right, but I think this is an often needed (simple) use case... – kapsiR Dec 09 '21 at 15:58

3 Answers3

7

After contacting the GitLab Support, I can document here, that there is no way of doing that currently.

A new issue has been created for this specific use case:
https://gitlab.com/gitlab-org/gitlab/-/issues/348141

kapsiR
  • 2,720
  • 28
  • 36
4

Below is the more elegant solution and is documented on the GitLab docs.

So there are two approaches.

  1. To push a commit without triggering a pipeline, add [ci skip] or [skip ci], using any capitalization, to your commit message.

  2. Alternatively, if you are using Git 2.10 or later, use the ci.skip Git push option. The ci.skip push option does not skip merge request pipelines.

    git push --push-option=ci.skip for GL version 2.18 and later, even the short version

    git push -o ci.skip

Reference:

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Sanjay Bharwani
  • 3,317
  • 34
  • 31
  • Thanks for this hint, but if anyone can skip it we would have to enable `Skipped pipelines are considered successful`. It's a requirement to have a successful pipeline before anything gets merged. (Only *.md would be an exception) – kapsiR Apr 14 '22 at 10:49
  • @kapsiR Cool, I didn't know this. So may be already configured. I needed this option when was working on some throwaway code, but wanted to share with team as well, so created a to be thrown branch and didn't want it to go through huge pipeline and block resources. – Sanjay Bharwani Apr 14 '22 at 13:07
  • 1
    Please edit this answer (edit queue is full so I cannot edit). It should be [skip ci] or [ci skip] - doesn't work without square brackets – Kappacake Nov 09 '22 at 08:48
4

A colleague of mine explored the globbing syntax used for making these rules of exclusion, and discovered that you can provide a list of conditions, that are evaluated in an AND-style of conditional logic. Consider the following:

.other_files_rule: &other_files_rule
  # Check if any files (not MD) changed
  # Glob syntax that checks for changes in all files except files that end with .md extension.
  # (Glob syntax tester: https://toools.cloud/miscellaneous/glob-tester)
  - changes:
      - "**/{.*,!(*.md)}"
    # If any non-MD files changed, always run the pipeline.
    when: always

# What happens if I updated CHANGELOG.md and some python file?
.md_only_rule: &md_only_rule
  # Check if any MD files changed
  # Glob syntax that checks for changes in files ending with .md extension.
  # (Glob syntax tester: https://toools.cloud/miscellaneous/glob-tester)
  - changes:
      - "**/*.md"
    # If any MD files changed, don't run the pipeline.
    when: manual
    # Allow failure must be true, else manual pipelines can never be successful without running the manual jobs.
    allow_failure: true

You need both rules, one to exclude MD files, and one to handle MD files.

Louis Parkin
  • 800
  • 7
  • 15