Using `rules:exists` to check if multiple files exists *at the same time*

,

How can I use rules:exists to enable a job only if two files exist at the same time?

I have a job which is only valid if a tree contains both setup.py and sonar-project.properties. I have tried the following (all without success):

Gitlab Version: 13.2.2

First Try

rules:
  - exists:
      - setup.py
      - sonar-project.properties

Problem: Job is also added if only one of the two files exists

Second Try

rules:
  - exists:
      - setup.py
  - exists:
      - sonar-project.properties
  - when: never

Problem: Job is also added if only one of the two files exists

Third Try

rules:
  - exists:
      - "setup.py && sonar-project.properties"

Problem: Does not work at all (it was a desperate try :wink: )

2 Likes

Just for clarification:

  • do you try to create a pipeline template which is to be included by several projects? Then I would define it as template and let the including project extend the job when these files exist.
  • or does this condition exist in some branches of one project but not in others? Then maybe abuse something like the branch name and match that one with pattern.

Unfortunately ANDing “exists” or “changes” is not possible at the moment.

Regarding “changes” keep in mind that jobs might not be triggered if you push multiple commits to a branch and only the first one changed the file actually (I use forced pushed for some usecases).

Any idea if this going to be supported?

Documentation also does not explain if the default behaviour is OR or AND.

1 Like

This looks like a design misfortune in GitLab:

Common sense makes me believe that the cases of existence/change of the file are not covered fully, and while in the case of change check the most common case is covered (any change in the files from the list causes job execution), in case of file existence is the least common scenario is covered. If I’m checking the existence of files to trigger the job, I’m expecting that all of them are there. The cleanest solution in my opinion should be 2 options:
exists_any: [list] and exists_all: [list]
changed_any: [list] and changed_all: [list]

BTW, the option to check all files existing is highly missing.

1 Like

I agree about calling this a “design misfortune”. To me, the cleanest solution would probably be to not try to handle cases like this with new predicate keywords like exists_any or exists_all (what would you do if you wanted to run only if (X AND Y) OR Z exists?), but by creating predicate functions that could function with arbitrary boolean combinations, like so (presuming the existence of a new exists() predicate function):

rules:
  - exists(setup.py) && exists(sonar-project.properties)

Another way to achieve this without reinventing the wheel would be to allow bash expressions:

rules:
  bash: "[[ -f setup.py && -f sonar-project.properties ]]"

or assume the [[ context implicitly:

rules:
  bash: -f setup.py && -f sonar-project.properties

Anyway, FWIW I’ve also found myself confused on several occasions by how the rules rules work, opening up a new frontier for this would be great.

1 Like