Skip to content

Commit

Permalink
matrix subaction
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 30, 2024
1 parent 27749bc commit b273ba0
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 6 deletions.
36 changes: 30 additions & 6 deletions .github/workflows/ci-subaction.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ on:
- 'test/**'

jobs:
list-targets-group:
list-targets:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
includes:
- testdir: group
- testdir: group-matrix
target: validate
steps:
-
name: Checkout
Expand All @@ -36,25 +43,42 @@ jobs:
id: gen
uses: ./subaction/list-targets
with:
workdir: ./test/group
workdir: ./test/${{ matrix.testdir }}
target: ${{ matrix.target }}
-
name: Show matrix
run: |
echo matrix=${{ steps.gen.outputs.matrix }}
list-targets-group-matrix:
matrix:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
includes:
- testdir: group
- testdir: group-matrix
target: validate
- testdir: group-with-platform
target: validate
- testdir: group-with-platform
target: validate
fields: platform
- testdir: group-with-platform
target: validate
fields: platform,dockerfile
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Matrix gen
id: gen
uses: ./subaction/list-targets
uses: ./subaction/matrix
with:
workdir: ./test/group-matrix
target: validate
workdir: ./test/${{ matrix.testdir }}
target: ${{ matrix.target }}
fields: ${{ matrix.fields }}
-
name: Show matrix
run: |
Expand Down
96 changes: 96 additions & 0 deletions subaction/matrix/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
name: 'Matrix'
description: 'Generate a matrix from a Bake definition to help distributing builds in your workflow'

inputs:
workdir:
description: Working directory
default: '.'
required: false
files:
description: List of Bake files
required: false
target:
description: Bake target
required: false
fields:
description: Comma separated list of extra fields to include in the matrix
required: false

outputs:
matrix:
description: List of includes as matrix
value: ${{ steps.generate.outputs.includes }}

runs:
using: composite
steps:
-
name: Generate
id: generate
uses: actions/github-script@v7
with:
script: |
let def;
const files = `${{ inputs.files }}` ? `${{ inputs.files }}`.split('\n') : [];
const target = `${{ inputs.target }}`;
const fields = `${{ inputs.fields }}` ? `${{ inputs.fields }}`.split(',') : [];
await core.group(`Parsing definition`, async () => {
let args = ['buildx', 'bake'];
for (const file of files) {
args.push('--file', file);
}
if (target) {
args.push(target);
}
args.push('--print');
const res = await exec.getExecOutput('docker', args, {
ignoreReturnCode: true,
silent: true,
cwd: `${{ inputs.workdir }}`
});
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
def = JSON.parse(res.stdout.trim());
core.info(JSON.stringify(def, null, 2));
});
await core.group(`Generating matrix`, async () => {
let includes = [];
for (const targetName of Object.keys(def.target)) {
const target = def.target[targetName];
if (fields.length > 0) {
let hasFields = false;
fields.forEach(field => {
if (target[field]) {
hasFields = true;
if (Array.isArray(target[field])) {
target[field].forEach(value => {
const include = { target: targetName };
include[field] = value;
includes.push(include);
});
} else {
const include = { target: targetName };
include[field] = target[field];
includes.push(include);
}
}
});
if (!hasFields) {
includes.push({
target: targetName
});
}
} else {
includes.push({
target: targetName
});
}
}
core.info(JSON.stringify(includes, null, 2));
core.setOutput('includes', JSON.stringify(includes));
});
43 changes: 43 additions & 0 deletions test/group-with-platform/docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
group "validate" {
targets = ["lint", "lint-gopls", "validate-vendor", "validate-docs"]
}

target "lint" {
inherits = ["_common"]
dockerfile = "./hack/dockerfiles/lint.Dockerfile"
output = ["type=cacheonly"]
platforms = [
"darwin/amd64",
"darwin/arm64",
"linux/amd64",
"linux/arm64",
"linux/s390x",
"linux/ppc64le",
"linux/riscv64",
"windows/amd64",
"windows/arm64"
]
}

target "lint-gopls" {
inherits = ["lint"]
target = "gopls-analyze"
}

target "validate-vendor" {
inherits = ["_common"]
dockerfile = "./hack/dockerfiles/vendor.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}

target "validate-docs" {
inherits = ["_common"]
args = {
FORMATS = DOCS_FORMATS
BUILDX_EXPERIMENTAL = 1 // enables experimental cmds/flags for docs generation
}
dockerfile = "./hack/dockerfiles/docs.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}

0 comments on commit b273ba0

Please sign in to comment.