'Don't launch a job if the only changes are on a folder

I want to disable some jobs if the only changes that happen in last git commit are in a docs folder. The idea is to be able to push new documentation on master (in a docs folder) without launching jobs responsible for pushing to production.

What I tried:

prod:
 script:
  - whatever
 rules:
  - changes:
     - docs/*
    when: never

It does not work because I want to be able to launch this job if there are changes in docs AND other files.

I also tried with a regular expression (probably wrong) but it does not seem to be taken in charge by gitlab. Something like that:

prod:
 script:
  - whatever
 rules:
  - changes:
     - (docs/*)!*
    when: never

I also tried a suboptimal solution, with a CI variable $CI_COMMIT_MESSAGE:

prod:
 script:
  - whatever
 rules:
  - if: $CI_COMMIT_MESSAGE == "doconly"
    when: never

and it does not work, except if the variable is set manually. Or this:

prod:
 script:
  - whatever
 except:
  variables:
    - if: $CI_COMMIT_MESSAGE == "doconly"

I also tried with a bash script that would set up a variable but variables can't be passed between jobs. Here the bash script:

LAST_COMMIT=$(git log --pretty=format:'%H' -n 1)
FILES=$(git diff-tree --no-commit-id --name-only -r ${LAST_COMMIT})
doconly=true

while read -r line && $doconly; do
  if  [[ ! "$line" =~ docs/* ]] ; then
    doconly=false
    exit 1
  fi
done <<< "$FILES"

exit 0 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source