Skip to content

Commit 9c52192

Browse files
authored
Merge pull request #416 from crazy-max/refactor-git-context
refactor: use new gitContext for bake source resolution
2 parents b48ae06 + b3f5862 commit 9c52192

File tree

8 files changed

+239
-74
lines changed

8 files changed

+239
-74
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,31 @@ jobs:
445445
-
446446
name: Build
447447
uses: ./
448+
with:
449+
files: |
450+
./test/config.hcl
451+
452+
git-context-query:
453+
runs-on: ubuntu-latest
454+
steps:
455+
-
456+
name: Checkout
457+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
458+
-
459+
name: Set up Docker Buildx
460+
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
461+
with:
462+
version: v0.33.0
463+
driver-opts: |
464+
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
465+
-
466+
name: Build
467+
uses: ./
468+
with:
469+
files: |
470+
./test/config.hcl
471+
env:
472+
BUILDX_SEND_GIT_QUERY_AS_INPUT: true
448473

449474
git-context-and-local:
450475
runs-on: ubuntu-latest
@@ -468,6 +493,7 @@ jobs:
468493
uses: ./
469494
with:
470495
files: |
496+
./test/config.hcl
471497
cwd://${{ steps.meta.outputs.bake-file }}
472498
473499
multi-output:

__tests__/context.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as os from 'os';
44
import * as path from 'path';
55

66
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
7+
import {Build} from '@docker/actions-toolkit/lib/buildx/build.js';
78
import {Builder} from '@docker/actions-toolkit/lib/buildx/builder.js';
89
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
910
import {Docker} from '@docker/actions-toolkit/lib/docker/docker.js';
@@ -39,6 +40,55 @@ vi.spyOn(Bake.prototype, 'getDefinition').mockImplementation(async (): Promise<B
3940
return <BakeDefinition>JSON.parse(fs.readFileSync(path.join(fixturesDir, 'bake-def.json'), {encoding: 'utf-8'}).trim());
4041
});
4142

43+
describe('getInputs', () => {
44+
const originalEnv = process.env;
45+
46+
beforeEach(() => {
47+
process.env = Object.keys(process.env).reduce((object, key) => {
48+
if (!key.startsWith('INPUT_')) {
49+
object[key] = process.env[key];
50+
}
51+
return object;
52+
}, {});
53+
});
54+
55+
afterEach(() => {
56+
process.env = originalEnv;
57+
});
58+
59+
function setRequiredBooleanInputs(): void {
60+
setInput('no-cache', 'false');
61+
setInput('pull', 'false');
62+
setInput('load', 'false');
63+
setInput('push', 'false');
64+
}
65+
66+
test('uses Build git context when source input is empty', async () => {
67+
const gitContext = 'https://github.com/docker/bake-action.git?ref=refs/heads/master&checksum=0123456789abcdef';
68+
const gitContextSpy = vi.spyOn(Build.prototype, 'gitContext').mockResolvedValue(gitContext);
69+
setRequiredBooleanInputs();
70+
const inputs = await context.getInputs();
71+
expect(inputs.source).toEqual({
72+
remoteRef: gitContext
73+
});
74+
expect(gitContextSpy).toHaveBeenCalledTimes(1);
75+
gitContextSpy.mockRestore();
76+
});
77+
78+
test('renders defaultContext source templates from Build git context', async () => {
79+
const gitContext = 'https://github.com/docker/bake-action.git#refs/heads/master';
80+
const gitContextSpy = vi.spyOn(Build.prototype, 'gitContext').mockResolvedValue(gitContext);
81+
setRequiredBooleanInputs();
82+
setInput('source', '{{defaultContext}}:subdir');
83+
const inputs = await context.getInputs();
84+
expect(inputs.source).toEqual({
85+
remoteRef: `${gitContext}:subdir`
86+
});
87+
expect(gitContextSpy).toHaveBeenCalledTimes(1);
88+
gitContextSpy.mockRestore();
89+
});
90+
});
91+
4292
describe('getArgs', () => {
4393
const originalEnv = process.env;
4494
beforeEach(() => {
@@ -343,6 +393,54 @@ describe('getArgs', () => {
343393
['BUILDX_NO_DEFAULT_ATTESTATIONS', '1']
344394
])
345395
],
396+
[
397+
15,
398+
'0.29.0',
399+
new Map<string, string>([
400+
['load', 'false'],
401+
['no-cache', 'false'],
402+
['push', 'false'],
403+
['pull', 'false'],
404+
['files', './foo.hcl'],
405+
]),
406+
[
407+
'bake',
408+
'https://github.com/docker/bake-action.git?ref=refs/heads/master',
409+
'--allow', 'fs=*',
410+
'--file', './foo.hcl',
411+
'--metadata-file', metadataJson,
412+
'--set', `lint.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
413+
'--set', `validate-docs.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
414+
'--set', `validate-vendor.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`
415+
],
416+
new Map<string, string>([
417+
['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true']
418+
])
419+
],
420+
[
421+
16,
422+
'0.28.0',
423+
new Map<string, string>([
424+
['load', 'false'],
425+
['no-cache', 'false'],
426+
['push', 'false'],
427+
['pull', 'false'],
428+
['files', './foo.hcl'],
429+
]),
430+
[
431+
'bake',
432+
'https://github.com/docker/bake-action.git#refs/heads/master',
433+
'--allow', 'fs=*',
434+
'--file', './foo.hcl',
435+
'--metadata-file', metadataJson,
436+
'--set', `lint.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
437+
'--set', `validate-docs.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
438+
'--set', `validate-vendor.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`
439+
],
440+
new Map<string, string>([
441+
['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true']
442+
])
443+
],
346444
])(
347445
'[%d] given %o with %o as inputs, returns %o',
348446
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>, envs: Map<string, string> | undefined) => {

dist/index.js

Lines changed: 28 additions & 15 deletions
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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"packageManager": "yarn@4.9.2",
2525
"dependencies": {
2626
"@actions/core": "^3.0.0",
27-
"@docker/actions-toolkit": "^0.79.0",
27+
"@docker/actions-toolkit": "^0.87.0",
2828
"handlebars": "^4.7.9"
2929
},
3030
"devDependencies": {

src/context.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as handlebars from 'handlebars';
44

55
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
66
import {Build} from '@docker/actions-toolkit/lib/buildx/build.js';
7-
import {Context} from '@docker/actions-toolkit/lib/context.js';
87
import {GitHub} from '@docker/actions-toolkit/lib/github/github.js';
98
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
109
import {Util} from '@docker/actions-toolkit/lib/util.js';
@@ -46,7 +45,7 @@ export async function getInputs(): Promise<Inputs> {
4645
push: core.getBooleanInput('push'),
4746
sbom: core.getInput('sbom'),
4847
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
49-
source: getBakeContext(core.getInput('source')),
48+
source: await getBakeContext(core.getInput('source')),
5049
targets: Util.getInputList('targets'),
5150
'github-token': core.getInput('github-token')
5251
};
@@ -139,12 +138,13 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
139138
return args;
140139
}
141140

142-
function getBakeContext(sourceInput: string): BakeContext {
141+
async function getBakeContext(sourceInput: string): Promise<BakeContext> {
142+
const defaultContext = await new Build().gitContext();
143143
let bakeContext = handlebars.compile(sourceInput)({
144-
defaultContext: Context.gitContext()
144+
defaultContext: defaultContext
145145
});
146146
if (!bakeContext) {
147-
bakeContext = Context.gitContext();
147+
bakeContext = defaultContext;
148148
}
149149
if (Util.isValidRef(bakeContext)) {
150150
return {

test/config.hcl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ group "release" {
66
targets = ["db", "app-plus"]
77
}
88

9+
# Special target: https://github.com/docker/metadata-action#bake-definition
10+
target "docker-metadata-action" {
11+
tags = [
12+
"localhost:5000/name/app:latest",
13+
"localhost:5000/name/app:1.0.0"
14+
]
15+
}
16+
917
target "db" {
1018
context = "./test"
1119
tags = ["docker.io/tonistiigi/db"]
1220
}
1321

1422
target "app" {
23+
inherits = ["docker-metadata-action"]
1524
context = "./test"
1625
dockerfile = "Dockerfile"
1726
args = {
1827
name = "foo"
1928
}
20-
tags = [
21-
"localhost:5000/name/app:latest",
22-
"localhost:5000/name/app:1.0.0"
23-
]
2429
}
2530

2631
target "cross" {

0 commit comments

Comments
 (0)