Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab-CI: how to avoid services config duplication between jobs?

Tags:

gitlab-ci

I have the following jobs configuration in .gitlab-ci.yml:

job1:
  stage: test
  services:
    - name: mariadb
      alias: mysql
      entrypoint: [""]
      command: [...]

  script:
    - ...

job2:
  stage: test
  services:
    - name: mariadb
      alias: mysql
      entrypoint: [""]
      command: [...]

  script:
    - ...

job3:
  stage: test
  services:
    - name: mariadb
      alias: mysql
      entrypoint: [""]
      command: [...]

  script:
    - ...

services portion is the same for all 3 jobs.

Is it possible to avoid this duplication?

like image 697
Roman Puchkovskiy Avatar asked Dec 13 '22 17:12

Roman Puchkovskiy


2 Answers

You can also utilize Anchors YAML feature - https://docs.gitlab.com/ee/ci/yaml/#anchors .

.job_template: &job_definition
  services:
    - name: mariadb
      alias: mysql
      entrypoint: [""]
      command: [...]

job1:
  <<: *job_definition
  stage: test

When the config is common for all jobs, use global service. When you want to avoid duplication among only some jobs, use YAML anchors.

like image 174
Tomáš Záluský Avatar answered Jan 13 '23 13:01

Tomáš Záluský


Just define it outside the jobs: https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#define-image-and-services-from-gitlab-ci-yml

services:
    - name: mariadb
      alias: mysql
      entrypoint: [""]
      command: [...]

job1:
  stage: test
  script:
    - ...

job2:
  stage: test
  script:
    - ...

job3:
  stage: test
  script:
    - ...
like image 29
Markus Avatar answered Jan 13 '23 13:01

Markus