Need help with CI/CD catalog and reuse of templates

Hi everyone …

I am testing the new CI/CD catalog feature and have currently the problem that I cannot reuse a template several times - each time with other dynamic values. I have started to play arround with the catalog only two weeks ago, so there is a great chance that I am just doing something wrong or that I have missunderstood the concept. So here we go …

My template looks like this:

//test-template.yml

spec:
  inputs:
    test-text:
      default: default-text
  stage:
    default: stage1

---

.test-template:
  stage: $[[ inputs.stage ]]
  image: alpine:latest
  script:
    - echo $[[ inputs.test-text]]


Within the .gitlab-ci.yml I have defined two local files where I use this template.

//.gitlab-ci.yml

stages:
  - stage1
  - stage2

include:
  - local: '/.gitlab/.test-components1.yml'
  - local: '/.gitlab/.test-components2.yml'


The /.gitlab/.test-components1.yml file looks like this:

include:
  - component: [PATH_TO_COMPONENT]/test-template@~latest
    inputs:
      stage: stage1
      test-text: test1

test-component1:
  extends: .test-template

  rules:
    - if: $CI_COMMIT_TAG


The other one /.gitlab/.test-components2.yml looks like this:

include:
  - component: [PATH_TO_COMPONENT]/test-template@~latest
    inputs:
      stage: stage2
      test-text: test2

test-component1:
  extends: .test-template

  rules:
    - if: $CI_COMMIT_TAG

When the pipeline is executed, both jobs are in stage stage2 (remember: the job test-component1 should be in stage1) and both jobs echos the text test2. It seems as if the second include of the template overwrites the first one with all its values. What am I doing wrong? Or is it a bug?

Thanks in advance …

You need to make the job name dynamic (you can’t have multiple jobs in a single pipeline with the same name).

A common way of achieving this is by adding a “job_name” input:

//test-template.yml

spec:
  inputs:
    test-text:
      default: default-text
    job_name:
      default: .test-template
    stage:
      default: stage1

---

$[[ inputs.job_name ]]:
  stage: $[[ inputs.stage ]]
  image: alpine:latest
  script:
    - echo $[[ inputs.test-text]]
2 Likes

Ah, that’s a good hint. I am going to test it within the next days and post a final feedback then. Many thanks :slight_smile:

Yip! The “job_name” input solves the issue. Many thanks again :slight_smile:

1 Like