Gitlab CI 同时在多个平台

Gitlab CI in multiple platforms simultaneously

我有一个 C++ 项目是为多个 OS (Linux, Windows, MacOS) 以及多个 CPU 架构(i386、x86_64、arm、Aarch64) 为此,我使用 Jenkins 在每个系统上并行获取源代码和 运行 构建脚本。这是一个简单的工作解决方案,因为我的构建脚本处理系统差异。

现在我正在研究 Gitlab CI/CD,它有很多吸引我的东西(能够将构建脚本作为存储库的一部分,与 git回购和票务系统,Docker 容器的自然使用,等等),但我找不到任何方法 运行 在多个 architectures/systems 中彼此并行的相同管道。

所以,假设我的构建脚本是:

build:
  stage: build
  script: 
    - uname -m > arch.txt
  artifacts:
    paths:
      - arch.txt

如何告诉 Gitlab 我想 运行 在多个 runners/Docker containers/systems 中同时进行这项工作?到目前为止,我阅读的所有文档都涉及 running multiple tests on one build, integrating multiple projects or deploying in different environments depending on branches。到目前为止,我读过的任何内容都没有尝试进行许多单独的构建、单独测试和打包它们并报告它们的独立结果。这在 Gitlab 上可行吗 CI/CD?

GitLab 使用 "runners" 执行 CI 个作业。 Runners 安装在任何你想 运行 一个 CI 工作的地方,所以如果你想 运行 在多个架构上,那么你需要在每个架构的系统上安装 运行ners .可以在此处找到 Runner 安装文档:

https://docs.gitlab.com/runner/install/index.html

对于基于 Linux 的作业,通常使用 Docker 来执行作业 - 这不会提供架构灵活性,但它确实允许您测试不同的风格和不同的软件使用容器化。对于其他架构,您可能需要自己安装 运行ners,或使用其他人共享的 运行ners。

在安装 运行ner 软件时,有一些关键步骤:

  • 你有机会 link 每个 运行 ner 到你的 GitLab 项目,这意味着它会出现在项目下的 运行ners 列表中 >设置 > CI/CD.

  • 您将有机会分配 "tags" 给 运行 人。标签可用于通过任意名称帮助识别一个 运行 人或一组 运行 人(例如,您可以添加 "Windows x86_64" 作为标签,或 "Windows" 和 "x86_64" 标签)。这些标签可用于 select 一个 运行ner.

一旦安装了 运行ners,您就可以开始编辑 .gitlab-ci.yml 文件了。

GitLab CI 文件被分解为 "stages"。每个阶段的作业可以 运行 并行。阶段名称在文件顶部定义。

stages:
  - build
  - deploy

可以使用 stage: 条目将每个 CI 作业附加到一个阶段:

build job:
  stage: build
  script:
    - echo "I am a build stage job"

在您的情况下,您需要为每个要构建的架构创建多个作业。将它们连接到同一阶段将允许它们并行 运行。

要控制每个作业 运行 的位置,您有两个主要机制:

  1. 标签 - 标签允许您将工作固定到 运行ner 标签。您可以使用构成 AND 列表的 tags: 条目指定多个标签(例如 win 标签和 x86_64 标签)。当那个工作 运行s GitLab 会找到一个 运行ner 有所有需要的标签,并且 运行 那里的工作。

  2. Image - 当 运行ning 在 Docker / Kubernetes 上时,您可以指定一个 docker 图像用于 运行ner。要使用 docker 图像,您首先需要指定一个 运行ner 可以 运行 docker 图像(例如 docker-in-docker或 kubernetes 运行ner),例如,它可能被标记为 dockerkubernetes。然后使用 image: 条目指定 docker 图像。

下面是同时显示标签和图片的示例:

build win x86_64:
  stage: build
  tags:
    - win
    - x86_64
  script:
    - echo "I am a build stage job for win x86_64"

build win 32:
  stage: build
  tags:
    - win
    - 32-bit
  script:
    - echo "I am a build stage job for win 32"

build debian:
  stage: build
  tags:
    - docker
  image: debian:stretch
  script:
    - echo "I am a build stage job for debian, running on docker using debian:stretch image"

目前不支持动态作业,或 运行在多个 运行 用户/架构上设置一个作业,因此这需要一些手动操作。从积极的方面来说,它使 GitLab CI 文件易于阅读,并且易于查看 CI 执行期间 运行 的内容。

查看最新的 GitLab 11.5 版(2018 年 11 月):

Parallel attribute for faster pipelines

The speed of pipelines is an important factor for any team, and running tests or other parallelizable tasks tends to take a big chunk of the time for any build.

Adding this new keyword gives teams the ability to simply parallelize tests, allowing everyone to accelerate their software delivery process.
To use this feature, simply set the parallel attribute to how many copies of the task you’d like to split it into, and GitLab will handle the work of automatically creating the appropriate number of jobs running your task.

by March 一样,这并不完全适合 OP(关于 运行 多个 architectures/systems 彼此并行的相同管道)。

documentation and issue

parallel allows you to configure how many instances of a job to run in parallel.
This value has to be greater than or equal to two (2) and less or equal than 50.

This creates N instances of the same job that run in parallel.
They’re named sequentially from job_name 1/N to job_name N/N.

test:
  script: rspec
  parallel: 5