Skip to content

Commit 5efdf4d

Browse files
authored
Merge pull request #338 from crazy-max/call-input
call input to set method for evaluating build
2 parents 1a42865 + 7a78e75 commit 5efdf4d

7 files changed

Lines changed: 142 additions & 22 deletions

File tree

.github/workflows/ci.yml

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ jobs:
116116
-
117117
name: Check
118118
run: |
119-
echo "${{ toJson(steps.bake) }}"
120119
if [ "${{ steps.bake.outcome }}" != "failure" ] || [ "${{ steps.bake.conclusion }}" != "success" ]; then
121120
echo "::error::Should have failed"
122121
exit 1
@@ -779,3 +778,92 @@ jobs:
779778
./test/config.hcl
780779
env:
781780
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
781+
782+
call-check:
783+
runs-on: ubuntu-latest
784+
steps:
785+
-
786+
name: Checkout
787+
uses: actions/checkout@v5
788+
-
789+
name: Set up Docker Buildx
790+
uses: docker/setup-buildx-action@v3
791+
with:
792+
version: ${{ inputs.buildx-version || env.BUILDX_VERSION }}
793+
driver-opts: |
794+
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
795+
-
796+
name: Build
797+
id: bake
798+
continue-on-error: true
799+
uses: ./
800+
with:
801+
workdir: ./test
802+
source: .
803+
files: |
804+
./lint.hcl
805+
call: check
806+
targets: lint
807+
-
808+
name: Check
809+
run: |
810+
if [ "${{ steps.bake.outcome }}" != "failure" ] || [ "${{ steps.bake.conclusion }}" != "success" ]; then
811+
echo "::error::Should have failed"
812+
exit 1
813+
fi
814+
815+
call-check-multi:
816+
runs-on: ubuntu-latest
817+
steps:
818+
-
819+
name: Checkout
820+
uses: actions/checkout@v5
821+
-
822+
name: Set up Docker Buildx
823+
uses: docker/setup-buildx-action@v3
824+
with:
825+
version: ${{ inputs.buildx-version || env.BUILDX_VERSION }}
826+
driver-opts: |
827+
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
828+
-
829+
name: Build
830+
id: bake
831+
continue-on-error: true
832+
uses: ./
833+
with:
834+
workdir: ./test
835+
source: .
836+
files: |
837+
./lint.hcl
838+
call: check
839+
-
840+
name: Check
841+
run: |
842+
if [ "${{ steps.bake.outcome }}" != "failure" ] || [ "${{ steps.bake.conclusion }}" != "success" ]; then
843+
echo "::error::Should have failed"
844+
exit 1
845+
fi
846+
847+
call-check-nowarning:
848+
runs-on: ubuntu-latest
849+
steps:
850+
-
851+
name: Checkout
852+
uses: actions/checkout@v5
853+
-
854+
name: Set up Docker Buildx
855+
uses: docker/setup-buildx-action@v3
856+
with:
857+
version: ${{ inputs.buildx-version || env.BUILDX_VERSION }}
858+
driver-opts: |
859+
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
860+
-
861+
name: Build
862+
id: bake
863+
continue-on-error: true
864+
uses: ./
865+
with:
866+
source: .
867+
files: |
868+
./test/config.hcl
869+
call: check

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,19 @@ The following inputs can be used as `step.with` keys
200200
| Name | Type | Description |
201201
|----------------|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
202202
| `builder` | String | Builder instance (see [setup-buildx](https://github.com/docker/setup-buildx-action) action) |
203+
| `workdir` | String | Working directory of execution |
203204
| `source` | String | Context to build from. Can be either local (`.`) or a [remote bake definition](https://docs.docker.com/build/bake/remote-definition/) |
204205
| `allow` | List/CSV | Allow build to access specified resources (e.g., `network.host`) |
206+
| `call` | String | Set method for evaluating build (e.g., check) |
205207
| `files` | List/CSV | List of [bake definition files](https://docs.docker.com/build/customize/bake/file-definition/) |
206-
| `workdir` | String | Working directory of execution |
207-
| `targets` | List/CSV | List of bake targets (`default` target used if empty) |
208208
| `no-cache` | Bool | Do not use cache when building the image (default `false`) |
209209
| `pull` | Bool | Always attempt to pull a newer version of the image (default `false`) |
210210
| `load` | Bool | Load is a shorthand for `--set=*.output=type=docker` (default `false`) |
211211
| `provenance` | Bool/String | [Provenance](https://docs.docker.com/build/attestations/slsa-provenance/) is a shorthand for `--set=*.attest=type=provenance` |
212212
| `push` | Bool | Push is a shorthand for `--set=*.output=type=registry` (default `false`) |
213213
| `sbom` | Bool/String | [SBOM](https://docs.docker.com/build/attestations/sbom/) is a shorthand for `--set=*.attest=type=sbom` |
214214
| `set` | List | List of [targets values to override](https://docs.docker.com/engine/reference/commandline/buildx_bake/#set) (e.g., `targetpattern.key=value`) |
215+
| `targets` | List/CSV | List of bake targets (`default` target used if empty) |
215216
| `github-token` | String | API token used to authenticate to a Git repository for [remote definitions](https://docs.docker.com/build/bake/remote-definition/) (default `${{ github.token }}`) |
216217

217218
### outputs

action.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ inputs:
1010
builder:
1111
description: "Builder instance"
1212
required: false
13+
workdir:
14+
description: "Working directory of bake execution"
15+
required: false
16+
default: '.'
1317
source:
1418
description: "Context to build from. Can be either local or a remote bake definition"
1519
required: false
1620
allow:
1721
description: "Allow build to access specified resources (e.g., network.host)"
1822
required: false
23+
call:
24+
description: "Set method for evaluating build (e.g., check)"
25+
required: false
1926
files:
2027
description: "List of bake definition files"
2128
required: false
22-
workdir:
23-
description: "Working directory of bake execution"
24-
required: false
25-
default: '.'
26-
targets:
27-
description: "List of bake targets"
28-
required: false
2929
no-cache:
3030
description: "Do not use cache when building the image"
3131
required: false
@@ -51,6 +51,9 @@ inputs:
5151
set:
5252
description: "List of targets values to override (eg. targetpattern.key=value)"
5353
required: false
54+
targets:
55+
description: "List of bake targets"
56+
required: false
5457
github-token:
5558
description: "API token used to authenticate to a Git repository for remote definitions"
5659
default: ${{ github.token }}

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,39 @@ import {Util} from '@docker/actions-toolkit/lib/util';
1111
import {BakeDefinition} from '@docker/actions-toolkit/lib/types/buildx/bake';
1212

1313
export interface Inputs {
14-
allow: string[];
1514
builder: string;
16-
files: string[];
1715
workdir: string;
18-
targets: string[];
16+
source: string;
17+
allow: string[];
18+
call: string;
19+
files: string[];
1920
'no-cache': boolean;
2021
pull: boolean;
2122
load: boolean;
2223
provenance: string;
2324
push: boolean;
2425
sbom: string;
2526
set: string[];
26-
source: string;
27+
targets: string[];
2728
'github-token': string;
2829
}
2930

3031
export async function getInputs(): Promise<Inputs> {
3132
return {
32-
allow: Util.getInputList('allow'),
3333
builder: core.getInput('builder'),
34-
files: Util.getInputList('files'),
3534
workdir: core.getInput('workdir') || '.',
36-
targets: Util.getInputList('targets'),
35+
source: getSourceInput('source'),
36+
allow: Util.getInputList('allow'),
37+
call: core.getInput('call'),
38+
files: Util.getInputList('files'),
3739
'no-cache': core.getBooleanInput('no-cache'),
3840
pull: core.getBooleanInput('pull'),
3941
load: core.getBooleanInput('load'),
4042
provenance: Build.getProvenanceInput('provenance'),
4143
push: core.getBooleanInput('push'),
4244
sbom: core.getInput('sbom'),
4345
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
44-
source: getSourceInput('source'),
46+
targets: Util.getInputList('targets'),
4547
'github-token': core.getInput('github-token')
4648
};
4749
}
@@ -69,6 +71,12 @@ async function getBakeArgs(inputs: Inputs, definition: BakeDefinition, toolkit:
6971
args.push('--allow', allow);
7072
});
7173
}
74+
if (inputs.call) {
75+
if (!(await toolkit.buildx.versionSatisfies('>=0.16.0'))) {
76+
throw new Error(`Buildx >= 0.16.0 is required to use the call flag.`);
77+
}
78+
args.push('--call', inputs.call);
79+
}
7280
await Util.asyncForEach(inputs.files, async file => {
7381
args.push('--file', file);
7482
});

src/main.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,26 @@ actionsToolkit.run(
146146
env: buildEnv,
147147
ignoreReturnCode: true
148148
}).then(res => {
149-
if (res.stderr.length > 0 && res.exitCode != 0) {
150-
err = Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
149+
if (res.exitCode != 0) {
150+
if (inputs.call && inputs.call === 'check' && res.stdout.length > 0) {
151+
// checks warnings are printed to stdout: https://github.com/docker/buildx/pull/2647
152+
// with bake we can have multiple targets being checked so we need to
153+
// count the total number of warnings
154+
const totalWarnings = [...res.stdout.matchAll(/^Check complete, (\d+) warnings? (?:has|have) been found!/gm)].reduce((sum, m) => sum + parseInt(m[1], 10), 0);
155+
if (totalWarnings > 0) {
156+
// https://github.com/docker/buildx/blob/1e50e8ddabe108f009b9925e13a321d7c8f99f26/commands/build.go#L797-L803
157+
if (totalWarnings === 1) {
158+
err = Error(`Check complete, ${totalWarnings} warning has been found!`);
159+
} else {
160+
err = Error(`Check complete, ${totalWarnings} warnings have been found!`);
161+
}
162+
} else {
163+
// if there are no warnings found, return the first line of stdout
164+
err = Error(res.stdout.split('\n')[0]?.trim());
165+
}
166+
} else if (res.stderr.length > 0) {
167+
err = Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
168+
}
151169
}
152170
});
153171

@@ -191,6 +209,8 @@ actionsToolkit.run(
191209
await core.group(`Check build summary support`, async () => {
192210
if (!buildSummaryEnabled()) {
193211
core.info('Build summary disabled');
212+
} else if (inputs.call && inputs.call !== 'build') {
213+
core.info(`Build summary skipped for ${inputs.call} subrequest`);
194214
} else if (GitHub.isGHES) {
195215
core.info('Build summary is not yet supported on GHES');
196216
} else if (!(await toolkit.buildx.versionSatisfies('>=0.13.0'))) {

0 commit comments

Comments
 (0)