Gitlab-CI:如何避免服务配置作业之间的重复?

时间:2018-03-09 07:23:46

标签: gitlab-ci

我在create table x (id integer generated by default as identity); insert into x (id) values (default); 中配置了以下作业:

.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: - ... 部分对于所有3个工作都是相同的。

是否可以避免这种重复?

3 个答案:

答案 0 :(得分:6)

您还可以使用Anchor YAML功能 - https://docs.gitlab.com/ee/ci/yaml/#anchors

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

job1:
  <<: *job_definition
  stage: test

当配置对所有作业都通用时,请使用全局服务。如果要避免仅在某些作业中重复,请使用YAML锚点。

答案 1 :(得分:1)

只需在作业外定义: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:
    - ...

答案 2 :(得分:0)

您可以使用在 GitLab 11.3 中引入的extends。它是使用YAML anchors的替代方法,并且更具灵活性和可读性。

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

job1:
  extends: .db_services
  stage: test
...

来源:Using extends in Gitlab CI

相关问题