.gitlab-ci.yaml Conditional Includes

I’d like to conditionally include a file based on some criteria. I’m creating a shared pipeline definition and I’d like to define things and run things only if the project the pipeline it’s running is has specific variables set or if a file exists in the directory. For example, I have language specific yaml files, I’d like to

include: 
   - local: "languages/docker.yaml"
     when: exists(Dockerfile)

Or something along those lines; is there a way to do what I’m looking for?

1 Like

FYI - was able to get around this by using conditional logic in the included files themselves and scoping variables to job level.

This would be useful… Or just “only” the same way as for stages.

@cschmidt0n I have exactly the same use case and I badly need to do the same , like I only want 1 specific include to be there based on parameter and I can still keep my pipelines DRY as much as possible.

Would move to see how you defined that in same include file as you described ?

Can you please provide some example for it. @cschmidt0n ?
I have a scenario where I just want to include the child yml only when some variable is met and only run a pipeline for that particular directory.

Below is my root .gitlab-ci.yml

include:
  - local: '/A/a.yml'
  - local: '/B/b.yml'
  - local: '/C/c.yml'
  - local: '/D/d.yml'

stages:
 - test
 - build
 - deploy

The stages for all the included pipelines are the same , the only difference is really in the set of variables and semantic-release they use. It would be really helpful to get to only run a single included pipeline and not others hence my ask for conditional include ?

I’d like this as well, was somewhat confused by the logic described for workflow templates – I thought thats what this was, e.g. you have one gitlab-ci.yml for lets say merge requests, and a seperate one for your default branch.

https://docs.gitlab.com/ee/ci/yaml/#workflowrules

It would be nice to have the some conditional logic like when: or the if: from workflow rules, e.g.

include:
  - local: .gitlab\MR-workflow.yml
    when:
      - merge_requests
1 Like

I too would very much like condtionals that use the same if criteria as rules do. I would really like them for include and also extends. Primarily so I can have slightly different builds based on branches and merge requests (eg, build without link-time optimization on some branches)

It doesn’t directly address the requirement, but you can kind of achieve the same outcome with dynamic child pipelines - https://docs.gitlab.com/ee/ci/parent_child_pipelines.html#dynamic-child-pipelines.

Just generate the pipeline you want depending on some variables. See e.g. https://gitlab.com/gitlab-org/project-templates/jsonnet, although you don’t have to use jsonnet - you can make your generated pipeline with whatever tools you like.

1 Like

I am looking to include a complete build pipeline only if that part of my repo has changes.
In my case that is, I would like to run the included terraform ci pipeline only if I have changes in a sub dir. Is that not possible at moment or I am I missing something? My case seems like a thing “everybody” would do?

So… I realised when posting that this was a year old. I did some further digging

Breadcrumbs:

  1. GitLab CI/CD include examples | GitLab
  2. Keyword reference for the `.gitlab-ci.yml` file | GitLab → says rules keyword changes is not supported"

Is there any other way?

Conditional includes with exists are now supported with GitLab 14.5. Related issue: Support conditional includes with exists (#341511) · Issues · GitLab.org / GitLab · GitLab

I’ve tried them in this project

with more screenshots on Twitter :slight_smile:

2 Likes