Avoid path redundancy in Gitlab CI include

Viewed 1147

To improve the structure of my Gitlab CI file I include some specific files, like for example

include:
  - '/ci/config/linux.yml'
  - '/ci/config/windows.yml'
  # ... more includes

To avoid the error-prone redundancy of the path I thought to put it into a variable, like:

variables:
  CI_CONFIG_DIR: '/ci/config'

include:
  - '${CI_CONFIG_DIR}/linux.yml'    # ERROR: "Local file `${CI_CONFIG_DIR}/linux.yml` does not exist!"
  - '${CI_CONFIG_DIR}/windows.yml'
  # ... more includes

But this does not work. Gitlab CI claims that ${CI_CONFIG_DIR}/linux.yml does not exist, although the documentation says that variables in include paths are allowed, see https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#gitlab-ciyml-file.

What also didn't work was to include a file /ci/config/main.yml and from that include the specific configurations without paths:

# /ci/config/main.yml
include:
  - 'linux.yml'    # ERROR: "Local file `linux.yml` does not exist!"
  - 'windows.yml'
  # ... more includes

How can I make this work or is there an alternative to define the path in only one place without making it too complicated?

2 Answers

This does not seem to be implemented at the moment, and there is an open issue at the moment in the backlog.

Also, with the documentation saying that you could use variables within include sections, those are only for predefined variables.

See if GitLab 14.2 (August 2021) can help:

Use CI/CD variables in include statements in .gitlab-ci.yml

You can now use variables as part of include statements in .gitlab-ci.yml files.
These variables can be instance, group, or project CI/CD variables.

This improvement provides you with more flexibility to define pipelines.
You can copy the same .gitlab-ci.yml file to multiple projects and use variables to alter its behavior.
This allows for less duplication in the .gitlab-ci.yml file and reduces the need for complicated per-project configuration.

https://about.gitlab.com/images/14_2/pipeline_include_variables.png -- Use CI/CD variables in include statements in .gitlab-ci.yml

See Documentation and Issue.

Related