kiabso
kiabso

Reputation: 775

GitLab CI template job with different names

Is there a way in GitLab CI to use a generic job template and run multiple instances of that job with different names?

Below I have a generic job definition in build.yml that specifies that the job will run in stage build and it will run docker build with the given Dockerfile.

I want to be able to run several builds (in parallel, but I excluded that for now to keep the configuration as clean as possible) with different names that are based on this build template. Example in build_a.yml and build_b.yml where I set a specific variable for the job that specifies which Dockerfile to use.

And in the main file gitlab-ci.yml i want to include these specific jobs (build_a and build_b).

common.yml

image:
  name: docker:dind
services:
  - docker:dind
stages:
  - build

build.yml

build:
  stage: build
  script:
    - docker build -f $DOCKER_FILE

build_a.yml

include: 'build.yml'
build:
  variables:
    DOCKER_FILE: DockerfileA

build_b.yml

include: 'build.yml'
build:
  variables:
    DOCKER_FILE: DockerfileB

.gitlab-ci.yml

include:
  - 'common.yml'
  - 'build_a.yml'
  - 'build_b.yml'

The problem is that when I include these build jobs they have the same job name, and the first job (build_a) will be overwritten by the second job (build_b) in the resulting Yaml file.

Is there another way of doing this that I have missed in the documentation or other similar issues?

Upvotes: 1

Views: 2551

Answers (1)

kiabso
kiabso

Reputation: 775

A way to solve the above issue is to use the extends keyword to extend the job configuration in the specific build files.

The below update will create 3 build jobs (.build, build_a and build_b), and the runner will exclude the job named ".build", so that only build_a and build_b will be executed.

common.yml

image:
  name: docker:dind
services:
  - docker:dind
stages:
  - build

build.yml

.build:
  stage: build
  script:
    - docker build -f $DOCKER_FILE

build_a.yml

include: 'build.yml'
build_a:
  extends: .build
  variables:
    DOCKER_FILE: DockerfileA

build_b.yml

include: 'build.yml'
build_b:
  extends: .build
  variables:
    DOCKER_FILE: DockerfileB

.gitlab-ci.yml

include:
  - 'common.yml'
  - 'build_a.yml'
  - 'build_b.yml'

Upvotes: 2

Related Questions