Skip to content

Commit 2a54546

Browse files
committed
merge workdir into source input
Signed-off-by: CrazyMax <[email protected]>
1 parent 1cf124c commit 2a54546

7 files changed

Lines changed: 72 additions & 51 deletions

File tree

.github/workflows/ci.yml

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ jobs:
121121
exit 1
122122
fi
123123
124+
error-source:
125+
runs-on: ubuntu-latest
126+
steps:
127+
-
128+
name: Checkout
129+
uses: actions/checkout@v6
130+
-
131+
name: Build
132+
id: bake
133+
continue-on-error: true
134+
uses: ./
135+
with:
136+
source: ./does-not-exist
137+
-
138+
name: Check
139+
run: |
140+
if [ "${{ steps.bake.outcome }}" != "failure" ] || [ "${{ steps.bake.conclusion }}" != "success" ]; then
141+
echo "::error::Should have failed"
142+
exit 1
143+
fi
144+
124145
standalone:
125146
runs-on: ubuntu-latest
126147
steps:
@@ -190,8 +211,7 @@ jobs:
190211
name: Build
191212
uses: ./
192213
with:
193-
workdir: ./test/go
194-
source: .
214+
source: ./test/go
195215
targets: binary
196216
provenance: ${{ matrix.attrs }}
197217
set: |
@@ -232,8 +252,7 @@ jobs:
232252
name: Build
233253
uses: ./
234254
with:
235-
workdir: ./test/go
236-
source: .
255+
source: ./test/go
237256
targets: ${{ matrix.target }}
238257
sbom: true
239258
set: |
@@ -279,8 +298,7 @@ jobs:
279298
name: Build
280299
uses: ./
281300
with:
282-
workdir: ./test/go
283-
source: .
301+
source: ./test/go
284302
set: |
285303
*.platform=linux/amd64
286304
*.output=type=image,"name=localhost:5000/name/app:v1.0.0,localhost:5000/name/app:latest",push=true
@@ -309,8 +327,7 @@ jobs:
309327
name: Build and push
310328
uses: ./
311329
with:
312-
workdir: ./test/group
313-
source: .
330+
source: ./test/group
314331
push: true
315332
set: |
316333
t1.tags=localhost:5000/name/app:t1
@@ -472,8 +489,7 @@ jobs:
472489
name: Build and push
473490
uses: ./
474491
with:
475-
workdir: ./test/go
476-
source: .
492+
source: ./test/go
477493
set: |
478494
*.output=type=image,name=localhost:5000/name/app:latest,push=true
479495
*.output=type=docker,name=app:local
@@ -516,8 +532,7 @@ jobs:
516532
name: Build and push
517533
uses: ./
518534
with:
519-
workdir: ./test/go
520-
source: .
535+
source: ./test/go
521536
targets: image
522537
load: true
523538
push: true
@@ -704,8 +719,7 @@ jobs:
704719
name: Build
705720
uses: ./
706721
with:
707-
workdir: ./test
708-
source: .
722+
source: ./test
709723
files: |
710724
./lint.hcl
711725
@@ -726,8 +740,7 @@ jobs:
726740
name: Build
727741
uses: ./
728742
with:
729-
workdir: ./test
730-
source: .
743+
source: ./test
731744
files: |
732745
./lint.hcl
733746
env:
@@ -798,8 +811,7 @@ jobs:
798811
continue-on-error: true
799812
uses: ./
800813
with:
801-
workdir: ./test
802-
source: .
814+
source: ./test
803815
files: |
804816
./lint.hcl
805817
call: check
@@ -831,8 +843,7 @@ jobs:
831843
continue-on-error: true
832844
uses: ./
833845
with:
834-
workdir: ./test
835-
source: .
846+
source: ./test
836847
files: |
837848
./lint.hcl
838849
call: check
@@ -885,5 +896,4 @@ jobs:
885896
name: Build and push
886897
uses: ./
887898
with:
888-
workdir: ./test/attest
889-
source: .
899+
source: ./test/attest

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ 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 |
204-
| `source` | String | Context to build from. Can be either local (`.`) or a [remote bake definition](https://docs.docker.com/build/bake/remote-definition/) |
205203
| `allow` | List/CSV | Allow build to access specified resources (e.g., `network.host`) |
206204
| `call` | String | Set method for evaluating build (e.g., check) |
207205
| `files` | List/CSV | List of [bake definition files](https://docs.docker.com/build/customize/bake/file-definition/) |
@@ -212,6 +210,7 @@ The following inputs can be used as `step.with` keys
212210
| `push` | Bool | Push is a shorthand for `--set=*.output=type=registry` (default `false`) |
213211
| `sbom` | Bool/String | [SBOM](https://docs.docker.com/build/attestations/sbom/) is a shorthand for `--set=*.attest=type=sbom` |
214212
| `set` | List | List of [targets values to override](https://docs.docker.com/engine/reference/commandline/buildx_bake/#set) (e.g., `targetpattern.key=value`) |
213+
| `source` | String | Context to build from. Can be either local to specify the working directory or a [remote bake definition](https://docs.docker.com/build/bake/remote-definition/) |
215214
| `targets` | List/CSV | List of bake targets (`default` target used if empty) |
216215
| `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 }}`) |
217216

__tests__/context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ describe('getArgs', () => {
450450
provenance: inp.provenance,
451451
push: inp.push,
452452
sbom: inp.sbom,
453-
source: inp.source,
453+
source: inp.source.remoteRef,
454454
targets: inp.targets
455455
},
456456
{
457-
cwd: inp.workdir
457+
cwd: inp.source.workdir,
458458
}
459459
);
460460
const res = await context.getArgs(inp, definition, toolkit);

action.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ inputs:
1010
builder:
1111
description: "Builder instance"
1212
required: false
13-
workdir:
14-
description: "Working directory of bake execution"
15-
required: false
16-
default: '.'
17-
source:
18-
description: "Context to build from. Can be either local or a remote bake definition"
19-
required: false
2013
allow:
2114
description: "Allow build to access specified resources (e.g., network.host)"
2215
required: false
@@ -51,6 +44,9 @@ inputs:
5144
set:
5245
description: "List of targets values to override (eg. targetpattern.key=value)"
5346
required: false
47+
source:
48+
description: "Context to build from. Can be either local to specify the working directory or a remote bake definition"
49+
required: false
5450
targets:
5551
description: "List of bake targets"
5652
required: false

src/context.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs';
12
import * as core from '@actions/core';
23
import * as handlebars from 'handlebars';
34

@@ -10,10 +11,13 @@ import {Util} from '@docker/actions-toolkit/lib/util';
1011

1112
import {BakeDefinition} from '@docker/actions-toolkit/lib/types/buildx/bake';
1213

14+
export interface BakeContext {
15+
remoteRef?: string;
16+
workdir?: string;
17+
}
18+
1319
export interface Inputs {
1420
builder: string;
15-
workdir: string;
16-
source: string;
1721
allow: string[];
1822
call: string;
1923
files: string[];
@@ -24,15 +28,14 @@ export interface Inputs {
2428
push: boolean;
2529
sbom: string;
2630
set: string[];
31+
source: BakeContext;
2732
targets: string[];
2833
'github-token': string;
2934
}
3035

3136
export async function getInputs(): Promise<Inputs> {
3237
return {
3338
builder: core.getInput('builder'),
34-
workdir: core.getInput('workdir') || '.',
35-
source: getSourceInput('source'),
3639
allow: Util.getInputList('allow'),
3740
call: core.getInput('call'),
3841
files: Util.getInputList('files'),
@@ -43,6 +46,7 @@ export async function getInputs(): Promise<Inputs> {
4346
push: core.getBooleanInput('push'),
4447
sbom: core.getInput('sbom'),
4548
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
49+
source: getBakeContext(core.getInput('source')),
4650
targets: Util.getInputList('targets'),
4751
'github-token': core.getInput('github-token')
4852
};
@@ -59,8 +63,8 @@ export async function getArgs(inputs: Inputs, definition: BakeDefinition, toolki
5963

6064
async function getBakeArgs(inputs: Inputs, definition: BakeDefinition, toolkit: Toolkit): Promise<Array<string>> {
6165
const args: Array<string> = ['bake'];
62-
if (inputs.source) {
63-
args.push(inputs.source);
66+
if (inputs.source.remoteRef) {
67+
args.push(inputs.source.remoteRef);
6468
}
6569
if (await toolkit.buildx.versionSatisfies('>=0.17.0')) {
6670
if (await toolkit.buildx.versionSatisfies('>=0.18.0')) {
@@ -135,17 +139,26 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
135139
return args;
136140
}
137141

138-
function getSourceInput(name: string): string {
139-
let source = handlebars.compile(core.getInput(name))({
142+
function getBakeContext(sourceInput: string): BakeContext {
143+
let bakeContext = handlebars.compile(sourceInput)({
140144
defaultContext: Context.gitContext()
141145
});
142-
if (!source) {
143-
source = Context.gitContext();
146+
if (!bakeContext) {
147+
bakeContext = Context.gitContext();
148+
}
149+
if (Util.isValidRef(bakeContext)) {
150+
return {
151+
remoteRef: bakeContext
152+
};
144153
}
145-
if (source === '.') {
146-
source = '';
154+
try {
155+
fs.statSync(sourceInput).isDirectory();
156+
} catch {
157+
throw new Error(`Invalid source: ${sourceInput} does not exist or is not a directory.`);
147158
}
148-
return source;
159+
return {
160+
workdir: bakeContext
161+
};
149162
}
150163

151164
function noDefaultAttestations(): boolean {

src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ actionsToolkit.run(
105105
provenance: inputs.provenance,
106106
push: inputs.push,
107107
sbom: inputs.sbom,
108-
source: inputs.source,
108+
source: inputs.source.remoteRef,
109109
targets: inputs.targets,
110110
githubToken: gitAuthToken
111111
},
112112
{
113-
cwd: inputs.workdir
113+
cwd: inputs.source.workdir
114114
}
115115
);
116116
});
@@ -130,7 +130,7 @@ actionsToolkit.run(
130130

131131
await core.group(`Bake definition`, async () => {
132132
await Exec.getExecOutput(buildCmd.command, [...buildCmd.args, '--print'], {
133-
cwd: inputs.workdir,
133+
cwd: inputs.source.workdir,
134134
env: buildEnv,
135135
ignoreReturnCode: true
136136
}).then(res => {
@@ -142,7 +142,7 @@ actionsToolkit.run(
142142

143143
let err: Error | undefined;
144144
await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
145-
cwd: inputs.workdir,
145+
cwd: inputs.source.workdir,
146146
env: buildEnv,
147147
ignoreReturnCode: true
148148
}).then(res => {

src/state-helper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ export function setSummarySupported() {
4040

4141
export function setSummaryInputs(inputs: Inputs) {
4242
const res = {};
43+
if (inputs.source.remoteRef || inputs.source.workdir) {
44+
res['source'] = inputs.source.remoteRef || inputs.source.workdir;
45+
}
4346
for (const key of Object.keys(inputs)) {
44-
if (key === 'github-token') {
47+
if (key === 'source' || key === 'github-token') {
4548
continue;
4649
}
4750
const value: string | string[] | boolean = inputs[key];

0 commit comments

Comments
 (0)