refactor: use new gitContext for bake source resolution

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2026-04-01 12:47:22 +02:00
parent cc339485f5
commit 561e713afb
4 changed files with 138 additions and 9 deletions
+26
View File
@@ -445,6 +445,31 @@ jobs:
-
name: Build
uses: ./
with:
files: |
./test/config.hcl
git-context-query:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
with:
version: v0.33.0
driver-opts: |
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
-
name: Build
uses: ./
with:
files: |
./test/config.hcl
env:
BUILDX_SEND_GIT_QUERY_AS_INPUT: true
git-context-and-local:
runs-on: ubuntu-latest
@@ -468,6 +493,7 @@ jobs:
uses: ./
with:
files: |
./test/config.hcl
cwd://${{ steps.meta.outputs.bake-file }}
multi-output:
+98
View File
@@ -4,6 +4,7 @@ import * as os from 'os';
import * as path from 'path';
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
import {Build} from '@docker/actions-toolkit/lib/buildx/build.js';
import {Builder} from '@docker/actions-toolkit/lib/buildx/builder.js';
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker.js';
@@ -39,6 +40,55 @@ vi.spyOn(Bake.prototype, 'getDefinition').mockImplementation(async (): Promise<B
return <BakeDefinition>JSON.parse(fs.readFileSync(path.join(fixturesDir, 'bake-def.json'), {encoding: 'utf-8'}).trim());
});
describe('getInputs', () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
if (!key.startsWith('INPUT_')) {
object[key] = process.env[key];
}
return object;
}, {});
});
afterEach(() => {
process.env = originalEnv;
});
function setRequiredBooleanInputs(): void {
setInput('no-cache', 'false');
setInput('pull', 'false');
setInput('load', 'false');
setInput('push', 'false');
}
test('uses Build git context when source input is empty', async () => {
const gitContext = 'https://github.com/docker/bake-action.git?ref=refs/heads/master&checksum=0123456789abcdef';
const gitContextSpy = vi.spyOn(Build.prototype, 'gitContext').mockResolvedValue(gitContext);
setRequiredBooleanInputs();
const inputs = await context.getInputs();
expect(inputs.source).toEqual({
remoteRef: gitContext
});
expect(gitContextSpy).toHaveBeenCalledTimes(1);
gitContextSpy.mockRestore();
});
test('renders defaultContext source templates from Build git context', async () => {
const gitContext = 'https://github.com/docker/bake-action.git#refs/heads/master';
const gitContextSpy = vi.spyOn(Build.prototype, 'gitContext').mockResolvedValue(gitContext);
setRequiredBooleanInputs();
setInput('source', '{{defaultContext}}:subdir');
const inputs = await context.getInputs();
expect(inputs.source).toEqual({
remoteRef: `${gitContext}:subdir`
});
expect(gitContextSpy).toHaveBeenCalledTimes(1);
gitContextSpy.mockRestore();
});
});
describe('getArgs', () => {
const originalEnv = process.env;
beforeEach(() => {
@@ -343,6 +393,54 @@ describe('getArgs', () => {
['BUILDX_NO_DEFAULT_ATTESTATIONS', '1']
])
],
[
15,
'0.29.0',
new Map<string, string>([
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
['pull', 'false'],
['files', './foo.hcl'],
]),
[
'bake',
'https://github.com/docker/bake-action.git?ref=refs/heads/master',
'--allow', 'fs=*',
'--file', './foo.hcl',
'--metadata-file', metadataJson,
'--set', `lint.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
'--set', `validate-docs.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
'--set', `validate-vendor.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`
],
new Map<string, string>([
['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true']
])
],
[
16,
'0.28.0',
new Map<string, string>([
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
['pull', 'false'],
['files', './foo.hcl'],
]),
[
'bake',
'https://github.com/docker/bake-action.git#refs/heads/master',
'--allow', 'fs=*',
'--file', './foo.hcl',
'--metadata-file', metadataJson,
'--set', `lint.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
'--set', `validate-docs.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`,
'--set', `validate-vendor.attest=type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/bake-action/actions/runs/123456789/attempts/1`
],
new Map<string, string>([
['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true']
])
],
])(
'[%d] given %o with %o as inputs, returns %o',
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>, envs: Map<string, string> | undefined) => {
+5 -5
View File
@@ -4,7 +4,6 @@ import * as handlebars from 'handlebars';
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
import {Build} from '@docker/actions-toolkit/lib/buildx/build.js';
import {Context} from '@docker/actions-toolkit/lib/context.js';
import {GitHub} from '@docker/actions-toolkit/lib/github/github.js';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
import {Util} from '@docker/actions-toolkit/lib/util.js';
@@ -46,7 +45,7 @@ export async function getInputs(): Promise<Inputs> {
push: core.getBooleanInput('push'),
sbom: core.getInput('sbom'),
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
source: getBakeContext(core.getInput('source')),
source: await getBakeContext(core.getInput('source')),
targets: Util.getInputList('targets'),
'github-token': core.getInput('github-token')
};
@@ -139,12 +138,13 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
return args;
}
function getBakeContext(sourceInput: string): BakeContext {
async function getBakeContext(sourceInput: string): Promise<BakeContext> {
const defaultContext = await new Build().gitContext();
let bakeContext = handlebars.compile(sourceInput)({
defaultContext: Context.gitContext()
defaultContext: defaultContext
});
if (!bakeContext) {
bakeContext = Context.gitContext();
bakeContext = defaultContext;
}
if (Util.isValidRef(bakeContext)) {
return {
+9 -4
View File
@@ -6,21 +6,26 @@ group "release" {
targets = ["db", "app-plus"]
}
# Special target: https://github.com/docker/metadata-action#bake-definition
target "docker-metadata-action" {
tags = [
"localhost:5000/name/app:latest",
"localhost:5000/name/app:1.0.0"
]
}
target "db" {
context = "./test"
tags = ["docker.io/tonistiigi/db"]
}
target "app" {
inherits = ["docker-metadata-action"]
context = "./test"
dockerfile = "Dockerfile"
args = {
name = "foo"
}
tags = [
"localhost:5000/name/app:latest",
"localhost:5000/name/app:1.0.0"
]
}
target "cross" {