Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include .gitlab-ci.yml multiple times with different configuration each time

Tags:

gitlab-ci

We have a GitLab CI/CD .gitlab-ci.yaml file that builds software packages. This .gitlab-ci.yaml has a variable that determines which operating system release the package should be built for. We would like to use the include keyword in other GitLab projects to include this .gitlab-ci.yaml to allow us to build packages. We want to build this package for multiple operating system releases. However, we cannot as GitLab does not allow us to include the same file twice. Is there another way to do this?

To see this more concretely, assume the .gitlab-ci.yaml file we want to include in other projects is this:

# common/gitlab-templates/.gitlab-ci.yaml
variables:
  OS_RELEASE: 10.0

build-package:
  script: echo "building for $OS_RELEASE"

In another GitLab project we would like to do something like this:

# Build for version 8.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 8.0

# Build for version 9.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 9.0

# Build for version 10.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 10.0

However, the above is not valid .gitlab-ci.yaml syntax.

How do we get around this?

like image 357
rlandster Avatar asked Feb 24 '26 14:02

rlandster


2 Answers

You can include the same file multiple times, with different inputs. However, if multiple jobs with the same name are added to one pipeline, each additional job overwrites the previous job with the same name. You must ensure the configuration prevents duplicate job names.

For example, including the same configuration multiple times with different inputs:

include:
  - local: path/to/my-super-linter.yml
    inputs:
      type: docs
      lint-path: "doc/"
  - local: path/to/my-super-linter.yml
    inputs:
      type: yaml
      lint-path: "data/yaml/"

The configuration in path/to/my-super-linter.yml ensures the job has a unique name each time it is included:

spec:
  inputs:
    type:
    lint-path:
---
"run-$[[ inputs.type ]]-lint":
script: ./lint --$[[ inputs.type ]] --path=$[[ inputs.lint-path ]]

Check Define inputs for configuration added with include

like image 121
Camilo Soto Avatar answered Feb 27 '26 08:02

Camilo Soto


Maybe you can use extends ?

    # common/gitlab-templates/.gitlab-ci.yaml
    variables:
      OS_RELEASE: change_me

    .build-package:
      script: echo "building for $OS_RELEASE"

In another GitLab project we would like to do something like this:

    # Build for version 8.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 8.0

    # Build for version 9.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 9.0

    # Build for version 10.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 10.0
like image 23
Oceania Water Avatar answered Feb 27 '26 07:02

Oceania Water



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!