首页 > 解决方案 > GitLab 动态并行运行作业

问题描述

我有一个项目,它在遵循特定约定的文件夹中生成 go main 文件。问题是当有代码推送时,我需要在 Gitlab CI 管道中并行构建这些项目,而我无法硬编码它们,.gitlab-ci.yml因为它们是动态生成的。我需要并行构建这些 go 项目,并且如果所有单个项目构建成功,则需要通过构建阶段。有人可以让我知道在 Gitlab 中是否可行。

标签: gitlabgitlab-cigitlab-ci-runner

解决方案


现在也可以使用子管道功能。您可以使用 YTT 为子管道生成 YAML 配置文件 - 但只要您提供有效的 YAML 文件,任何生成方法都可以使用。

.gitlab-ci.yml

generate-config:
  stage: build
  script: ytt -f config-template.yml -f config-template-values.yml --data-value-yaml services="[${SERVICES}]" > generated-config.yml # with SERVICES=service1,service2
  artifacts:
    paths:
      - generated-config.yml

child-pipeline:
  stage: test
  trigger:
    include:
      - artifact: generated-config.yml
        job: generate-config

配置模板.yml

#@ load("@ytt:data", "data")
stages:
  - test
#@ for service in data.values.services:
#@yaml/text-templated-strings
(@= service @)-test:
  stage: test
  script:
    - #@ "test " + service
#@ end

配置模板值.yml

#@data/values
---
services: []

推荐阅读