|
| 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 | +}; |
0 commit comments