Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -468,6 +493,7 @@ jobs:
uses: ./
with:
files: |
./test/config.hcl
cwd://${{ steps.meta.outputs.bake-file }}

multi-output:
Expand Down
98 changes: 98 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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) => {
Expand Down
43 changes: 28 additions & 15 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"packageManager": "yarn@4.9.2",
"dependencies": {
"@actions/core": "^3.0.0",
"@docker/actions-toolkit": "^0.79.0",
"@docker/actions-toolkit": "^0.87.0",
"handlebars": "^4.7.9"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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')
};
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions test/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading
Loading