How to inherit prefilled variables from a generic pipeline through include

Hello,

I have what I would call a generic pipeline in a specific project, and every other project that needs this pipeline to build their Docker image just include this pipeline to use it. This is something that I guess everyone pretty much do.

When including this pipeline, you need to set a few variables for the pipeline included to work. At the moment, these variables are defined in the project where the generic pipeline is included. The variables defined are of type dropdown list or free form values, here is how I include the generic pipeline and set the variables :

variables:
  DOCKER_IMAGE_NAME:
      value: ""
      description: "REQUIRED: Provide the name of the Docker image to build"
  USED_BY_CI:
      value: ""
      description: "OPTIONAL: Select the 'CICD' value if the image that is built will be used in CI/CD pipelines, otherwise leave it empty. This will be only appended when pushing into Gitlab registry"
      options:
        - "CICD"
        - ""
  DOCKERFILE_PATH:
      value: "Dockerfile"
      description: "REQUIRED: Select the correct Dockerfile to build the image from. By default the one named 'Dockerfile' will be used"
  CUSTOM_TAG_VALUE:
    value: ""
    description: "OPTIONAL: If the Dockerfile used to build the image is different from the default one, you can append here a specific tag that will be used in the name of the container built. NOTE: Do not use space between words! Use a '-' (dash)"
  REGISTRY_TARGET:
      value: "Gitlab"
      description: "REQUIRED: Select the container registry target, to where the built Docker image will be stored"
      options:
        - "ECR"
        - "Gitlab"
  ECR_REPOSITORY_NAME:
      value: "php"
      description: "ONLY IF ECR REGISTRY IS USED: If the target container registry is ECR, please specify the name of the repository where the image should be stored in ECR"
      options:
        - "php"

include:
  project: system/pipeline-generic
  ref: main
  file:
    - docker-image-builder-kaniko.yml

What I would like to do, is to have those variables defined at the generic pipeline level, so that every project can inherit them from the include keyword. This would help implement new features much easier, and not have to edit 20 projects to define a new variables needed by the generic pipeline to work.

It doesn’t seem at the moment possible to do that, as implementing those variables like written above in the generic pipeline, are not inherited through the include.
Does anyone has a solution to this? Is this even possible at the moment?

When I start a pipeline with the variables defined at the generic pipeline level, I do not have any variables shown :

But when the variables are defined in the project level it of course does work :

My Gitlab version is 16.1.2
If you need more information, feel free to ask, and thanks for the help!

Regards,

Hi @whyyouwannaknow

I cannot replicate your problem on GitLab.com. All global variables defined in my include are shown in Run pipeline page.

Shared pipeline file image-build.yml:

variables:
  OCI_HADOLINT_OUTPUT:
    description: "Hadolint output format"
    value: "gitlab_codeclimate"
  OCI_DOCKERFILE_PATH:
    description: "path to dockerfile"

Project pipeline file .gitlab-ci.yml:

include:
  - project: 'group/common-pipelines'
    ref: test
    file: 'image-build.yml'

Result:

Hello,

You have solved my issue!
Writting the include like so is inheriting variables correctly from the generic pipeline :

include:
  - project: system/pipeline-generic
    ref: main
    file: docker-image-builder-kaniko.yml

But writting the include like I was doing previously, does not work :

include:
  project: system/pipeline-generic
  ref: main
  file:
    - docker-image-builder-kaniko.yml

I guess we could consider this a “bug”, since both syntax are valide (I mean by that that it is not throwing an error saying that it not well written when starting the pipeline), but one does include the variables from the generic pipeline, and another one doesn’t.

Thanks for your help!

One more difference I see is that your include is not a list, but a hash. You can also try it like this:

include:
  - project: system/pipeline-generic
    ref: main
    file:
      - docker-image-builder-kaniko.yml

Defining include as hash is not really anywhere in documentation and it is strange it worked for you :slight_smile:

This indeed work too!
It definitely solved the issue, I will write it like your example provided from now on!

Thanks again!