Skip to content

Commit 78650a1

Browse files
committed
matrix subaction
Signed-off-by: CrazyMax <[email protected]>
1 parent 27749bc commit 78650a1

File tree

3 files changed

+162
-6
lines changed

3 files changed

+162
-6
lines changed

.github/workflows/ci-subaction.yml

+30-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ on:
2525
- 'test/**'
2626

2727
jobs:
28-
list-targets-group:
28+
list-targets:
2929
runs-on: ubuntu-latest
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
include:
34+
- testdir: group
35+
- testdir: group-matrix
36+
target: validate
3037
steps:
3138
-
3239
name: Checkout
@@ -36,25 +43,42 @@ jobs:
3643
id: gen
3744
uses: ./subaction/list-targets
3845
with:
39-
workdir: ./test/group
46+
workdir: ./test/${{ matrix.testdir }}
47+
target: ${{ matrix.target }}
4048
-
4149
name: Show matrix
4250
run: |
4351
echo matrix=${{ steps.gen.outputs.matrix }}
4452
45-
list-targets-group-matrix:
53+
matrix:
4654
runs-on: ubuntu-latest
55+
strategy:
56+
fail-fast: false
57+
matrix:
58+
include:
59+
- testdir: group
60+
- testdir: group-matrix
61+
target: validate
62+
- testdir: group-with-platform
63+
target: validate
64+
- testdir: group-with-platform
65+
target: validate
66+
fields: platforms
67+
- testdir: group-with-platform
68+
target: validate
69+
fields: platforms,dockerfile
4770
steps:
4871
-
4972
name: Checkout
5073
uses: actions/checkout@v4
5174
-
5275
name: Matrix gen
5376
id: gen
54-
uses: ./subaction/list-targets
77+
uses: ./subaction/matrix
5578
with:
56-
workdir: ./test/group-matrix
57-
target: validate
79+
workdir: ./test/${{ matrix.testdir }}
80+
target: ${{ matrix.target }}
81+
fields: ${{ matrix.fields }}
5882
-
5983
name: Show matrix
6084
run: |

subaction/matrix/action.yml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
2+
name: 'Matrix'
3+
description: 'Generate a matrix from a Bake definition to help distributing builds in your workflow'
4+
5+
inputs:
6+
workdir:
7+
description: Working directory
8+
default: '.'
9+
required: false
10+
files:
11+
description: List of Bake files
12+
required: false
13+
target:
14+
description: Bake target
15+
required: false
16+
fields:
17+
description: Comma separated list of extra fields to include in the matrix
18+
required: false
19+
20+
outputs:
21+
matrix:
22+
description: List of includes as matrix
23+
value: ${{ steps.generate.outputs.includes }}
24+
25+
runs:
26+
using: composite
27+
steps:
28+
-
29+
name: Generate
30+
id: generate
31+
uses: actions/github-script@v7
32+
with:
33+
script: |
34+
let def;
35+
const files = `${{ inputs.files }}` ? `${{ inputs.files }}`.split('\n') : [];
36+
const target = `${{ inputs.target }}`;
37+
const fields = `${{ inputs.fields }}` ? `${{ inputs.fields }}`.split(',') : [];
38+
39+
await core.group(`Parsing definition`, async () => {
40+
let args = ['buildx', 'bake'];
41+
for (const file of files) {
42+
args.push('--file', file);
43+
}
44+
if (target) {
45+
args.push(target);
46+
}
47+
args.push('--print');
48+
49+
const res = await exec.getExecOutput('docker', args, {
50+
ignoreReturnCode: true,
51+
silent: true,
52+
cwd: `${{ inputs.workdir }}`
53+
});
54+
if (res.stderr.length > 0 && res.exitCode != 0) {
55+
throw new Error(res.stderr);
56+
}
57+
def = JSON.parse(res.stdout.trim());
58+
core.info(JSON.stringify(def, null, 2));
59+
});
60+
61+
await core.group(`Generating matrix`, async () => {
62+
let includes = [];
63+
for (const targetName of Object.keys(def.target)) {
64+
const target = def.target[targetName];
65+
if (fields.length > 0) {
66+
let hasFields = false;
67+
fields.forEach(field => {
68+
if (target[field]) {
69+
hasFields = true;
70+
if (Array.isArray(target[field])) {
71+
target[field].forEach(value => {
72+
const include = { target: targetName };
73+
include[field] = value;
74+
includes.push(include);
75+
});
76+
} else {
77+
const include = { target: targetName };
78+
include[field] = target[field];
79+
includes.push(include);
80+
}
81+
}
82+
});
83+
if (!hasFields) {
84+
includes.push({
85+
target: targetName
86+
});
87+
}
88+
} else {
89+
includes.push({
90+
target: targetName
91+
});
92+
}
93+
}
94+
core.info(JSON.stringify(includes, null, 2));
95+
core.setOutput('includes', JSON.stringify(includes));
96+
});
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
group "validate" {
2+
targets = ["lint", "lint-gopls", "validate-vendor", "validate-docs"]
3+
}
4+
5+
target "lint" {
6+
dockerfile = "./hack/dockerfiles/lint.Dockerfile"
7+
output = ["type=cacheonly"]
8+
platforms = [
9+
"darwin/amd64",
10+
"darwin/arm64",
11+
"linux/amd64",
12+
"linux/arm64",
13+
"linux/s390x",
14+
"linux/ppc64le",
15+
"linux/riscv64",
16+
"windows/amd64",
17+
"windows/arm64"
18+
]
19+
}
20+
21+
target "lint-gopls" {
22+
inherits = ["lint"]
23+
target = "gopls-analyze"
24+
}
25+
26+
target "validate-vendor" {
27+
dockerfile = "./hack/dockerfiles/vendor.Dockerfile"
28+
target = "validate"
29+
output = ["type=cacheonly"]
30+
}
31+
32+
target "validate-docs" {
33+
dockerfile = "./hack/dockerfiles/docs.Dockerfile"
34+
target = "validate"
35+
output = ["type=cacheonly"]
36+
}

0 commit comments

Comments
 (0)