Problem passing variable from .gitlab-ci.yml to external template project using include

Hello,

I am quite new to gitlab-ci, especially when it comes to advanced pipelines, but here is what has been puzzling me:

Setup:

I am building a ci template project and for now it is really simple:

include:
  - local: 'dotnet/docker_image/docker_app.yml'
    rules:
      - if: '$IS_DOTNET_DOCKER_APP == "true"'
        when: always
  - local: 'base.yml'

The dotnet/docker_image/docker_app.yml file is to be included when the $IS_DOTNET_DOCKER_APP variable is true. This variable is declared in the test project defined bellow.

The base.yml file is just for debugging, and looks like this:

stages:
  - checks

base:
  stage: checks
  script:
    - |
      echo "This is the base job. IS_DOTNET_DOCKER_APP = ${IS_DOTNET_DOCKER_APP}"

I have a test project that uses it (and this is the one that starts the pipeline), and its .gitlab-ci.yml looks like this:

variables:
  IS_DOTNET_DOCKER_APP: "true"
  DOTNET_SDK_VERSION: "7.0"
  DOCKER_IMAGE_NAME: "e4portal-api"

include:
  - project: 'e4-network/cicd-templates'
    ref: 'base_CICD'
    file: '/.gitlab-ci.yml'

The problem:

The IS_DOTNET_DOCKER_APP variable is passed to the template project (it is echoed correctly).
I get the output from the base.yml base job:

"This is the base job. IS_DOTNET_DOCKER_APP = true

But it is not read by the rules/if condition. (The file ‘dotnet/docker_image/docker_app.yml’ does not get included in the pipeline)

I have tried adding the variable directly above the template project’s .gitlab-ci.yml file, same thing. It is like the variable is undeclared.

However, if I add the variable directly when I manually run the pipeline…:

…then it works!

I also tested removing the rules completely, the file then gets included as expected.

It puzzles me. The variable is passed from the test project to the template project, then is accessible in scripts, but it it is not accessible to the condition check for the file include statement.

I want the variable to be set in the test project’s .gitlab-ci.yml file.

I am using Gitlab.com.

If you want more details, feel free to ask and I will reply.

Thank you very much for your help. I am sure it is a simple problem that I cannot see.

I found this quote on the gitlab documentation:

You cannot use variables defined in jobs, or in a global variables section which defines the default variables for all jobs. Includes are evaluated before jobs, so these variables cannot be used with include.

That might be the problem.

Is there a way to use variables in a yml file to pass to an included template?

Thanks for your help.

1 Like

any final solution for testing ci components or projects and pass variables-parameters?