Skip to content

Commit 7ce8772

Browse files
committed
Replace github-script with separate package
1 parent 180c0b5 commit 7ce8772

12 files changed

Lines changed: 272 additions & 87 deletions

File tree

dist/licenses.txt

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import eslintPluginPrettier from 'eslint-plugin-prettier';
88

99
export default defineConfig([
1010
{
11-
ignores: ['.yarn/**/*', 'coverage/**/*', 'dist/**/*']
11+
ignores: ['.yarn/**/*', 'coverage/**/*', 'dist/**/*', 'subaction/matrix/dist/**/*']
1212
},
1313
js.configs.recommended,
1414
...tseslint.configs['flat/recommended'],

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"test": "vitest run",
1111
"license": "generate-license-file --input package.json --output dist/licenses.txt --overwrite --ci --no-spinner --eol lf"
1212
},
13+
"workspaces": [
14+
"subaction/matrix"
15+
],
1316
"repository": {
1417
"type": "git",
1518
"url": "git+https://github.com/docker/bake-action.git"

subaction/matrix/action.yml

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,7 @@ inputs:
2020
outputs:
2121
matrix:
2222
description: Matrix configuration
23-
value: ${{ steps.generate.outputs.includes }}
2423

2524
runs:
26-
using: composite
27-
steps:
28-
-
29-
name: Generate
30-
id: generate
31-
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
32-
env:
33-
INPUT_WORKDIR: ${{ inputs.workdir }}
34-
INPUT_FILES: ${{ inputs.files }}
35-
INPUT_TARGET: ${{ inputs.target }}
36-
INPUT_FIELDS: ${{ inputs.fields }}
37-
with:
38-
script: |
39-
function getInputList(name) {
40-
return core.getInput(name) ? core.getInput(name).split(/[\r?\n,]+/).filter(x => x !== '') : [];
41-
}
42-
43-
const workdir = core.getInput('workdir');
44-
const files = getInputList('files');
45-
const target = core.getInput('target');
46-
const fields = getInputList('fields');
47-
48-
let def = {};
49-
await core.group(`Parsing definition`, async () => {
50-
let args = ['buildx', 'bake'];
51-
for (const file of files) {
52-
args.push('--file', file);
53-
}
54-
if (target) {
55-
args.push(target);
56-
}
57-
args.push('--print');
58-
const res = await exec.getExecOutput('docker', args, {
59-
ignoreReturnCode: true,
60-
silent: true,
61-
cwd: workdir
62-
});
63-
if (res.stderr.length > 0 && res.exitCode != 0) {
64-
throw new Error(res.stderr);
65-
}
66-
def = JSON.parse(res.stdout.trim());
67-
core.info(JSON.stringify(def, null, 2));
68-
});
69-
70-
await core.group(`Generating matrix`, async () => {
71-
const result = [];
72-
for (const targetName of Object.keys(def.target)) {
73-
const target = def.target[targetName];
74-
const entry = { target: targetName };
75-
if (fields.length === 0) {
76-
result.push({ ...entry });
77-
continue;
78-
}
79-
let fieldFound = false;
80-
Object.keys(target).forEach(field => {
81-
if (fields.includes(field)) {
82-
fieldFound = true;
83-
const value = target[field];
84-
if (Array.isArray(value)) {
85-
value.forEach((v) => {
86-
entry[field] = v;
87-
result.push({ ...entry });
88-
});
89-
} else {
90-
entry[field] = value;
91-
result.push({ ...entry });
92-
}
93-
}
94-
});
95-
if (!fieldFound) {
96-
result.push({ ...entry });
97-
}
98-
}
99-
core.info(JSON.stringify(result, null, 2));
100-
core.setOutput('includes', JSON.stringify(result));
101-
});
25+
using: 'node24'
26+
main: 'dist/index.cjs'

subaction/matrix/dist/index.cjs

Lines changed: 114 additions & 0 deletions
Large diffs are not rendered by default.

subaction/matrix/dist/index.cjs.map

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

subaction/matrix/dist/licenses.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This file was generated with the generate-license-file npm package!
2+
https://www.npmjs.com/package/generate-license-file
3+
4+
This file was generated with the generate-license-file npm package!
5+
https://www.npmjs.com/package/generate-license-file

subaction/matrix/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "docker-buildx-bake-matrix",
3+
"description": "GitHub Action to generate a multi-dimension GitHub matrix from bake definition files",
4+
"type": "module",
5+
"main": "src/main.ts",
6+
"scripts": {
7+
"build": "esbuild src/main.ts --bundle --platform=node --target=node24 --format=cjs --outfile=dist/index.cjs --sourcemap --minify && yarn run license",
8+
"license": "generate-license-file --input package.json --output dist/licenses.txt --overwrite --ci --no-spinner --eol lf"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/docker/bake-action.git",
13+
"directory": "subaction/matrix"
14+
},
15+
"author": "Docker Inc.",
16+
"license": "Apache-2.0",
17+
"dependencies": {
18+
"@actions/core": "catalog:",
19+
"@actions/exec": "^3.0.0",
20+
"@docker/actions-toolkit": "catalog:"
21+
},
22+
"devDependencies": {
23+
"esbuild": "catalog:",
24+
"generate-license-file": "catalog:"
25+
}
26+
}

subaction/matrix/src/main.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import * as core from '@actions/core';
2+
import * as exec from '@actions/exec';
3+
import * as actionsToolkit from '@docker/actions-toolkit';
4+
5+
import {BakeDefinition, Target} from '@docker/actions-toolkit/lib/types/buildx/bake.js';
6+
7+
actionsToolkit.run(
8+
// main
9+
async () => {
10+
const workdir = core.getInput('workdir');
11+
const files = getInputList('files');
12+
const target = core.getInput('target');
13+
const fields = getInputList('fields');
14+
15+
let def: BakeDefinition;
16+
await core.group(`Parsing definition`, async () => {
17+
const args = ['buildx', 'bake'];
18+
for (const file of files) {
19+
args.push('--file', file);
20+
}
21+
if (target) {
22+
args.push(target);
23+
}
24+
args.push('--print');
25+
const res = await exec.getExecOutput('docker', args, {
26+
ignoreReturnCode: true,
27+
silent: true,
28+
cwd: workdir
29+
});
30+
if (res.stderr.length > 0 && res.exitCode != 0) {
31+
throw new Error(res.stderr);
32+
}
33+
def = JSON.parse(res.stdout.trim());
34+
core.info(JSON.stringify(def, null, 2));
35+
});
36+
37+
await core.group(`Generating matrix`, async () => {
38+
const result: Array<MatrixConfigEntry> = [];
39+
for (const targetName of Object.keys(def.target)) {
40+
const target = def.target[targetName];
41+
const entry: MatrixConfigEntry = {target: targetName};
42+
if (fields.length === 0) {
43+
result.push({...entry});
44+
continue;
45+
}
46+
let fieldFound = false;
47+
Object.keys(target).forEach(field => {
48+
if (fields.includes(field)) {
49+
fieldFound = true;
50+
const value = target[field];
51+
if (Array.isArray(value)) {
52+
value.forEach(v => {
53+
entry[field] = v;
54+
result.push({...entry});
55+
});
56+
} else {
57+
entry[field] = value;
58+
result.push({...entry});
59+
}
60+
}
61+
});
62+
if (!fieldFound) {
63+
result.push({...entry});
64+
}
65+
}
66+
core.info(JSON.stringify(result, null, 2));
67+
core.setOutput('matrix', JSON.stringify(result));
68+
});
69+
}
70+
);
71+
72+
function getInputList(name: string) {
73+
return core.getInput(name)
74+
? core
75+
.getInput(name)
76+
.split(/[\r?\n,]+/)
77+
.filter(x => x !== '')
78+
: [];
79+
}
80+
81+
type Optional<Type> = {
82+
[Property in keyof Type]?: Type[Property];
83+
};
84+
85+
type ArrayToSingleEntry<Type> = {
86+
[Property in keyof Type]: Type[Property] extends Array<infer Entry> ? Entry : Type[Property];
87+
};
88+
89+
type MatrixConfigEntry = Optional<ArrayToSingleEntry<Target>> & {
90+
target: string;
91+
};

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"useUnknownInCatchVariables": false,
1313
},
1414
"include": [
15-
"src/**/*.ts"
15+
"src/**/*.ts",
16+
"subaction/matrix/src/**/*.ts"
1617
]
1718
}

0 commit comments

Comments
 (0)