• 矩阵流水线

矩阵流水线

悟空平台已经集成了对矩阵工作流的支持.悟空平台为矩阵中的每个组合执行一个单独的工作流,允许您针对多个配置构建和测试单个提交.

矩阵示例定义:

matrix:
  GO_VERSION:
    - 1.4
    - 1.3
  REDIS_VERSION:
    - 2.6
    - 2.8
    - 3.0

示例仅包含特定组合的矩阵定义:

matrix:
  include:
    - GO_VERSION: 1.4
      REDIS_VERSION: 2.8
    - GO_VERSION: 1.5
      REDIS_VERSION: 2.8
    - GO_VERSION: 1.6
      REDIS_VERSION: 3.0

插值

矩阵变量在YAML中使用${VARIABLE}语法进行插值,插值发生在YAML解析之前.以下是矩阵参数插值之前的YAML文件示例:

matrix:
  GO_VERSION:
    - 1.4
    - 1.3
  DATABASE:
    - mysql:5.5
    - mysql:6.5
    - mariadb:10.1

pipeline:
  build:
    image: golang:${GO_VERSION}
    commands:
      - go get
      - go build
      - go test

services:
  database:
    image: ${DATABASE}

注入矩阵参数后的示例 YAML 文件:

pipeline:
  build:
-   image: golang:${GO_VERSION}
+   image: golang:1.4
    commands:
      - go get
      - go build
      - go test
+   environment:
+     - GO_VERSION=1.4
+     - DATABASE=mysql:5.5

services:
  database:
-   image: ${DATABASE}
+   image: mysql:5.5

案例

基于Docker镜像标签的矩阵流水线示例

matrix:
  TAG:
    - 1.7
    - 1.8
    - latest

pipeline:
  build:
    image: golang:${TAG}
    commands:
      - go build
      - go test

基于容器镜像的矩阵流水线示例

matrix:
  IMAGE:
    - golang:1.7
    - golang:1.8
    - golang:latest

pipeline:
  build:
    image: ${IMAGE}
    commands:
      - go build
      - go test

使用多个平台的示例矩阵管道

matrix:
  platform:
    - linux/amd64
    - linux/arm64

platform: ${platform}

pipeline:
  test:
    image: alpine
    commands:
      - echo "I am running on ${platform}"

  test-arm-only:
    image: alpine
    commands:
      - echo "I am running on ${platform}"
      - echo "Arm is cool!"
    when:
      platform: linux/arm*