Skip to content

Provide example how to use rules:exists with not

Problem to solve

When rules rules, and specifically rules:exists we can make a job run if, and only if a file exists. However sometimes the opposite is desired, to run a job, if, and only if a file does NOT exist.

The documentation does not mention how to do this at all. If this where not possible, the documentation also does not state that this is not possible. With only/except and changes this was possible by combining 'changes' with either only or except or using regex on the 'changes' (this last part is also not clear, only that one can glob).

Further details

As a work-around, I have to do the following, which is ugly

not-exists:
job:
  rules:
    - exists:
      - "file"
  script:
    - echo "Not supported when file exists"
    - exit 1

and unless I stage the 'real' job that shouldn't run if a file exists, the job will still run, but at least the pipeline gets aborted.

Proposal

Add a keyword not_exists that will take an array. If all files do not exist, return true.

job:
  rules:
    - not_exists: # Array of files will be treated as AND operator
      - "file1"
      - "file2"
      - "file3"

  script:
    - echo "Not supported when file exists"
    - exit 1

Who can address the issue

Anybody familiar with CI/CD and 'rules'

Other links/references

Edited by Kasia Misirli