Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrix subaction #204

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 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:
include:
- testdir: group
- testdir: group-matrix
target: validate
steps:
-
name: Checkout
Expand All @@ -36,26 +43,35 @@ jobs:
id: gen
uses: ./subaction/list-targets
with:
workdir: ./test/group
-
name: Show matrix
run: |
echo matrix=${{ steps.gen.outputs.matrix }}
workdir: ./test/${{ matrix.testdir }}
target: ${{ matrix.target }}

list-targets-group-matrix:
matrix:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- testdir: group
- testdir: group-matrix
target: validate
- testdir: group-with-platform
target: validate
- testdir: group-with-platform
target: validate
fields: platforms
- testdir: group-with-platform
target: validate
fields: platforms,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
-
name: Show matrix
run: |
echo matrix=${{ steps.gen.outputs.matrix }}
workdir: ./test/${{ matrix.testdir }}
target: ${{ matrix.target }}
fields: ${{ matrix.fields }}
83 changes: 83 additions & 0 deletions subaction/matrix/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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 () => {
const result = [];
for (const targetName of Object.keys(def.target)) {
const target = def.target[targetName];
const entry = { target: targetName };
Object.keys(target).forEach(field => {
if (fields.includes(field)) {
const value = target[field];
if (Array.isArray(value)) {
value.forEach((v: string) => {
entry[field] = v
result.push({ ...entry });
});
} else {
entry[field] = value;
result.push({ ...entry });
}
}
});
}
core.info(JSON.stringify(result, null, 2));
core.setOutput('includes', JSON.stringify(result));
});
36 changes: 36 additions & 0 deletions test/group-with-platform/docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
group "validate" {
targets = ["lint", "lint-gopls", "validate-vendor", "validate-docs"]
}

target "lint" {
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" {
dockerfile = "./hack/dockerfiles/vendor.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}

target "validate-docs" {
dockerfile = "./hack/dockerfiles/docs.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}
Loading