Add support for optional includes in GitLab CI configurations
Summary
If Project A includes a CI File from Project B which has an conditional include, the include is not evaluated against Project A but Project B.
Although expectation is, that the include exists is evaluated against Project A - to allow more general porpuse and self managed includes without additional setup logic.
Proposal
- Add a new key to include
optional
toinclude
statements which changes the behaviour from:- Causing an error when the file doesn't exist.
- Doing nothing when the file doesn't exist. Specifically, not including the file in question.
include:
project: "x"
file: "y"
ref: "z"
optional: boolean
Steps to reproduce
Basic Setup:
- Project A .gitlab-ci.yml
# project A
includes:
- remote: ProjectB
file: include.yml
- Project B include.yml
includes:
- local: sub-include.yml
rules:
- exists:
- "Dockerfile"
- Project B sub-include.yml
# actually not relevant
job:
script: echo "test"
Reproduce 1#:
- create a Dockerfile within Project A:
- run the pipleine of Project A
- expectation is that
job
will run, but does not
Reproduce 2#:
- create a Dockerfile within Project B
- remove the Dockerfile from Project A
- run the pipeline of Project A
- expectation is that
job
will run, and it does, although there is no Dockerfile within the project which is triggering the CI pipeline
Example Project
Project with the includes: https://gitlab.com/lazy-lint/my-project
Project using the includes: https://gitlab.com/lazy-lint/testing/bigtestproject
- should run a HadoLint step and a Go step and a CSS step, based on the files, but does not do so
What is the current bug behavior?
Conditional includes for file existence are evaluated in the context of the project we are including the file from, if the condition is defined within that project.
What is the expected correct behavior?
Conditional includes for file existence should be evaluated against the files of the project which is including the files even if those are sub includes from a different project.
Relevant logs and/or screenshots
As you can see the jobs are created based on the file existence in the Project i am included the file from, instead of the project where the pipeline is started.
Output of checks
This bug happens on GitLab.com
Additional links
- see #341511 (comment 782819207) for the initial discussion