refactor: use new gitContext for bake source resolution
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
26
.github/workflows/ci.yml
vendored
26
.github/workflows/ci.yml
vendored
@@ -445,6 +445,31 @@ jobs:
|
|||||||
-
|
-
|
||||||
name: Build
|
name: Build
|
||||||
uses: ./
|
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:
|
git-context-and-local:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -468,6 +493,7 @@ jobs:
|
|||||||
uses: ./
|
uses: ./
|
||||||
with:
|
with:
|
||||||
files: |
|
files: |
|
||||||
|
./test/config.hcl
|
||||||
cwd://${{ steps.meta.outputs.bake-file }}
|
cwd://${{ steps.meta.outputs.bake-file }}
|
||||||
|
|
||||||
multi-output:
|
multi-output:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import * as os from 'os';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
|
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 {Builder} from '@docker/actions-toolkit/lib/buildx/builder.js';
|
||||||
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
|
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
|
||||||
import {Docker} from '@docker/actions-toolkit/lib/docker/docker.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());
|
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', () => {
|
describe('getArgs', () => {
|
||||||
const originalEnv = process.env;
|
const originalEnv = process.env;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -343,6 +393,54 @@ describe('getArgs', () => {
|
|||||||
['BUILDX_NO_DEFAULT_ATTESTATIONS', '1']
|
['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',
|
'[%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) => {
|
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>, envs: Map<string, string> | undefined) => {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import * as handlebars from 'handlebars';
|
|||||||
|
|
||||||
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
|
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake.js';
|
||||||
import {Build} from '@docker/actions-toolkit/lib/buildx/build.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 {GitHub} from '@docker/actions-toolkit/lib/github/github.js';
|
||||||
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
|
||||||
import {Util} from '@docker/actions-toolkit/lib/util.js';
|
import {Util} from '@docker/actions-toolkit/lib/util.js';
|
||||||
@@ -46,7 +45,7 @@ export async function getInputs(): Promise<Inputs> {
|
|||||||
push: core.getBooleanInput('push'),
|
push: core.getBooleanInput('push'),
|
||||||
sbom: core.getInput('sbom'),
|
sbom: core.getInput('sbom'),
|
||||||
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
|
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
|
||||||
source: getBakeContext(core.getInput('source')),
|
source: await getBakeContext(core.getInput('source')),
|
||||||
targets: Util.getInputList('targets'),
|
targets: Util.getInputList('targets'),
|
||||||
'github-token': core.getInput('github-token')
|
'github-token': core.getInput('github-token')
|
||||||
};
|
};
|
||||||
@@ -139,12 +138,13 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
|
|||||||
return args;
|
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)({
|
let bakeContext = handlebars.compile(sourceInput)({
|
||||||
defaultContext: Context.gitContext()
|
defaultContext: defaultContext
|
||||||
});
|
});
|
||||||
if (!bakeContext) {
|
if (!bakeContext) {
|
||||||
bakeContext = Context.gitContext();
|
bakeContext = defaultContext;
|
||||||
}
|
}
|
||||||
if (Util.isValidRef(bakeContext)) {
|
if (Util.isValidRef(bakeContext)) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -6,21 +6,26 @@ group "release" {
|
|||||||
targets = ["db", "app-plus"]
|
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" {
|
target "db" {
|
||||||
context = "./test"
|
context = "./test"
|
||||||
tags = ["docker.io/tonistiigi/db"]
|
tags = ["docker.io/tonistiigi/db"]
|
||||||
}
|
}
|
||||||
|
|
||||||
target "app" {
|
target "app" {
|
||||||
|
inherits = ["docker-metadata-action"]
|
||||||
context = "./test"
|
context = "./test"
|
||||||
dockerfile = "Dockerfile"
|
dockerfile = "Dockerfile"
|
||||||
args = {
|
args = {
|
||||||
name = "foo"
|
name = "foo"
|
||||||
}
|
}
|
||||||
tags = [
|
|
||||||
"localhost:5000/name/app:latest",
|
|
||||||
"localhost:5000/name/app:1.0.0"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
target "cross" {
|
target "cross" {
|
||||||
|
|||||||
Reference in New Issue
Block a user