Skip to content

Commit

Permalink
matrix targets subaction
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Nov 9, 2023
1 parent 063b84c commit 0058d5f
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci-subaction.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: ci-subaction

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
workflow_dispatch:
schedule:
- cron: '0 10 * * *'
push:
branches:
- 'master'
- 'releases/v*'
tags:
- 'v*'
paths:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'
- 'test/**'
pull_request:
paths:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'
- 'test/**'

jobs:
matrix-targets-group:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Matrix gen
id: gen
uses: ./subaction/matrix-targets
with:
workdir: ./test/group
-
name: Show matrix
run: |
echo matrix=${{ steps.gen.outputs.matrix }}
matrix-targets-group-matrix:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Matrix gen
id: gen
uses: ./subaction/matrix-targets
with:
workdir: ./test/group-matrix
target: validate
-
name: Show matrix
run: |
echo matrix=${{ steps.gen.outputs.matrix }}
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ on:
- 'releases/v*'
tags:
- 'v*'
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'
pull_request:
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'

env:
BUILDX_VERSION: latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ on:
branches:
- 'master'
- 'releases/v*'
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'
pull_request:
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'

jobs:
test:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ on:
branches:
- 'master'
- 'releases/v*'
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'
pull_request:
paths-ignore:
- '.github/workflows/ci-subaction.yml'
- 'subaction/**'

jobs:
prepare:
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ as a high-level build command.
___

* [Usage](#usage)
* [Subactions](#subactions)
* [`matrix-targets`](#matrix-targets)
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
Expand Down Expand Up @@ -52,6 +54,66 @@ jobs:
push: true
```
## Subactions
### `matrix-targets`

This subaction generates a matrix of Bake targets that can be used in a [GitHub matrix](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix),
so you can distribute your builds across multiple runners.

```hcl
# docker-bake.hcl
group "validate" {
targets = ["lint", "doctoc"]
}
target "lint" {
target = "lint"
}
target "doctoc" {
target = "doctoc"
}
```

```yaml
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
targets: ${{ steps.targets.outputs.matrix }}
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Targets matrix
id: targets
uses: docker/bake-action/subaction/matrix-targets@v4
with:
target: validate
validate:
runs-on: ubuntu-latest
needs:
- prepare
strategy:
fail-fast: false
matrix:
target: ${{ fromJson(needs.prepare.outputs.targets) }}
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Validate
uses: docker/bake-action@v4
with:
targets: ${{ matrix.target }}
```

See also https://docs.docker.com/build/ci/github-actions/multi-platform/#with-bake

## Customizing

### inputs
Expand Down
59 changes: 59 additions & 0 deletions subaction/matrix-targets/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
name: 'Bake matrix targets'
description: 'Generate a matrix of targets to distribute builds in your workflow'

inputs:
workdir:
description: Working directory
default: '.'
required: false
files:
description: Comma separated list of Bake files
required: false
target:
description: Bake target
default: 'default'
required: false

outputs:
matrix:
description: Matrix of targets
value: ${{ steps.generate.outputs.matrix }}

runs:
using: composite
steps:
-
name: Generate matrix
id: generate
uses: actions/github-script@v6
with:
script: |
let def;
const files = `${{ inputs.files }}` ? `${{ inputs.files }}`.split(',') : [];
const target = `${{ inputs.target }}` ? `${{ inputs.target }}` : 'default';
await core.group(`Validating definition`, async () => {
let args = ['buildx', 'bake'];
for (const file of files) {
args.push('--file', file);
}
args.push(target, '--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(`Set matrix`, async () => {
const matrix = Object.keys(def.target);
core.info(`matrix: ${JSON.stringify(matrix)}`);
core.setOutput('matrix', JSON.stringify(matrix));
});
31 changes: 31 additions & 0 deletions test/group-matrix/docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
group "validate" {
targets = ["lint", "validate-vendor", "validate-doctoc"]
}

target "lint" {
name = "lint-${buildtags.name}"
dockerfile = "./hack/dockerfiles/lint.Dockerfile"
target = buildtags.target
output = ["type=cacheonly"]
matrix = {
buildtags = [
{ name = "default", tags = "", target = "golangci-lint" },
{ name = "labs", tags = "dfrunsecurity dfparents", target = "golangci-lint" },
{ name = "nydus", tags = "nydus", target = "golangci-lint" },
{ name = "yaml", tags = "", target = "yamllint" },
{ name = "proto", tags = "", target = "protolint" },
]
}
}

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

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

0 comments on commit 0058d5f

Please sign in to comment.