From 561e713afbde307c955d481b97f29b8825ff64d3 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 1 Apr 2026 12:47:22 +0200 Subject: [PATCH] refactor: use new gitContext for bake source resolution Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/ci.yml | 26 +++++++++++ __tests__/context.test.ts | 98 +++++++++++++++++++++++++++++++++++++++ src/context.ts | 10 ++-- test/config.hcl | 13 ++++-- 4 files changed, 138 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 027442e..96c269f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index f1e6cba..31e4a9e 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -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 (): PromiseJSON.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([ + ['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([ + ['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true'] + ]) + ], + [ + 16, + '0.28.0', + new Map([ + ['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([ + ['BUILDX_SEND_GIT_QUERY_AS_INPUT', 'true'] + ]) + ], ])( '[%d] given %o with %o as inputs, returns %o', async (num: number, buildxVersion: string, inputs: Map, expected: Array, envs: Map | undefined) => { diff --git a/src/context.ts b/src/context.ts index 6df7826..d695376 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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 { 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> { return args; } -function getBakeContext(sourceInput: string): BakeContext { +async function getBakeContext(sourceInput: string): Promise { + 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 { diff --git a/test/config.hcl b/test/config.hcl index 6e48771..1d23558 100644 --- a/test/config.hcl +++ b/test/config.hcl @@ -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" {