跳到主要内容

矩阵工作流

Woodpecker 内置支持矩阵工作流。Woodpecker 为矩阵中的每个组合执行一个单独的工作流,允许您针对多个配置进行构建和测试。

示例矩阵定义:

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

插值

矩阵变量使用 ${VARIABLE} 语法在 YAML 中进行插值,然后再解析 YAML。这是插值矩阵参数之前的示例 YAML 文件:

matrix:
GO_VERSION:
- 1.4
- 1.3
DATABASE:
- mysql:8
- mysql:5
- mariadb:10.1

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

services:
- name: database
image: ${DATABASE}

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

 steps:
- name: build
- image: golang:${GO_VERSION}
+ image: golang:1.4
commands:
- go get
- go build
- go test
+ environment:
+ - GO_VERSION=1.4
+ - DATABASE=mysql:8

services:
- name: database
- image: ${DATABASE}
+ image: mysql:8

示例

基于 Docker 镜像标签的示例矩阵管道

matrix:
TAG:
- 1.7
- 1.8
- latest

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

基于容器镜像的示例矩阵管道

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

steps:
- name: build
image: ${IMAGE}
commands:
- go build
- go test

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

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

labels:
platform: ${platform}

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

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

如果您想在 Kubernetes 运行器上控制管道的架构,请参阅 Kubernetes 后端的 nodeSelector 文档