Skip to content

Commit 23f8808

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

File tree

3 files changed

+164
-14
lines changed

3 files changed

+164
-14
lines changed

.github/workflows/ci-subaction.yml

+30-14
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,26 +43,35 @@ jobs:
3643
id: gen
3744
uses: ./subaction/list-targets
3845
with:
39-
workdir: ./test/group
40-
-
41-
name: Show matrix
42-
run: |
43-
echo matrix=${{ steps.gen.outputs.matrix }}
46+
workdir: ./test/${{ matrix.testdir }}
47+
target: ${{ matrix.target }}
4448

45-
list-targets-group-matrix:
49+
matrix:
4650
runs-on: ubuntu-latest
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
include:
55+
- testdir: group
56+
- testdir: group-matrix
57+
target: validate
58+
- testdir: group-with-platform
59+
target: validate
60+
- testdir: group-with-platform
61+
target: validate
62+
fields: platforms
63+
- testdir: group-with-platform
64+
target: validate
65+
fields: platforms,dockerfile
4766
steps:
4867
-
4968
name: Checkout
5069
uses: actions/checkout@v4
5170
-
5271
name: Matrix gen
5372
id: gen
54-
uses: ./subaction/list-targets
73+
uses: ./subaction/matrix
5574
with:
56-
workdir: ./test/group-matrix
57-
target: validate
58-
-
59-
name: Show matrix
60-
run: |
61-
echo matrix=${{ steps.gen.outputs.matrix }}
75+
workdir: ./test/${{ matrix.testdir }}
76+
target: ${{ matrix.target }}
77+
fields: ${{ matrix.fields }}

subaction/matrix/action.yml

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
} else {
82+
core.warning(`Field "${field}" not found in target ${targetName}`);
83+
}
84+
});
85+
if (!hasFields) {
86+
includes.push({
87+
target: targetName
88+
});
89+
}
90+
} else {
91+
includes.push({
92+
target: targetName
93+
});
94+
}
95+
}
96+
core.info(JSON.stringify(includes, null, 2));
97+
core.setOutput('includes', JSON.stringify(includes));
98+
});
+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)