Skip to content

Commit

Permalink
Do not set --iidfile flag if local and tar exporters are used
Browse files Browse the repository at this point in the history
More tests for context module

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
CrazyMax committed Oct 19, 2020
1 parent d90dfad commit 08566ae
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 265 deletions.
16 changes: 15 additions & 1 deletion __tests__/buildx.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import fs from 'fs';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as buildx from '../src/buildx';
import * as exec from '@actions/exec';
import * as context from '../src/context';

const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';

jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});

jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
return path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
});

describe('getImageID', () => {
it('matches', async () => {
const imageIDFile = await buildx.getImageIDFile();
Expand Down
121 changes: 121 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,125 @@
import * as fs from 'fs';
import * as path from 'path';
import * as context from '../src/context';
import * as buildx from '../src/buildx';

jest.spyOn(context, 'defaultContext').mockImplementation((): string => {
return 'https://github.com/docker/build-push-action.git#test-jest';
});

jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});

jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
return path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
});

describe('getArgs', () => {
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
if (!key.startsWith('INPUT_')) {
object[key] = process.env[key];
}
return object;
}, {});
});

// prettier-ignore
test.each([
[
'0.4.2',
new Map<string, string>([
// noop
]),
[
'buildx',
'build',
'--iidfile',
'/tmp/.docker-build-push-jest/iidfile',
'--file',
'Dockerfile',
'https://github.com/docker/build-push-action.git#test-jest'
]
],
[
'0.4.2',
new Map<string, string>([
['context', '.'],
['outputs', 'type=local,dest=./release-out']
]),
[
'buildx',
'build',
'--output', 'type=local,dest=./release-out',
'--file', 'Dockerfile',
'.'
]
],
[
'0.4.1',
new Map<string, string>([
['context', '.']
]),
[
'buildx',
'build',
'--file', 'Dockerfile',
'.'
]
],
[
'0.4.2',
new Map<string, string>([
['context', '.'],
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno0123456789'],
]),
[
'buildx',
'build',
'--iidfile',
'/tmp/.docker-build-push-jest/iidfile',
'--secret',
'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest',
'--file', 'Dockerfile',
'.'
]
],
[
'0.4.2',
new Map<string, string>([
['github-token', 'abcdefghijklmno0123456789']
]),
[
'buildx',
'build',
'--iidfile',
'/tmp/.docker-build-push-jest/iidfile',
'--secret',
'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest',
'--file', 'Dockerfile',
'https://github.com/docker/build-push-action.git#test-jest'
]
]
])(
'given %p with %p as inputs, returns %p',
async (buildxVersion: string, inputs: Map<string, any>, expected: Array<string>) => {
await inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
const defContext = context.defaultContext();
const inp = await context.getInputs(defContext);
console.log(inp);
const res = await context.getArgs(inp, defContext, buildxVersion);
console.log(res);
expect(res).toEqual(expected);
}
);
});

describe('getInputList', () => {
it('handles single line correctly', async () => {
Expand Down
505 changes: 261 additions & 244 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
clearMocks: true,
clearMocks: false,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
Expand Down
7 changes: 3 additions & 4 deletions src/buildx.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
import * as semver from 'semver';
import * as context from './context';
import * as exec from './exec';

export async function getImageIDFile(): Promise<string> {
return path.join(context.tmpDir, 'iidfile');
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
}

export async function getImageID(): Promise<string | undefined> {
Expand All @@ -19,8 +18,8 @@ export async function getImageID(): Promise<string | undefined> {

export async function getSecret(kvp: string): Promise<string> {
const [key, value] = kvp.split('=');
const secretFile = tmp.tmpNameSync({
tmpdir: context.tmpDir
const secretFile = context.tmpNameSync({
tmpdir: context.tmpDir()
});
await fs.writeFileSync(secretFile, value);
return `id=${key},src=${secretFile}`;
Expand Down
39 changes: 27 additions & 12 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as tmp from 'tmp';
import * as buildx from './buildx';
import * as core from '@actions/core';
import * as github from '@actions/github';

export const tmpDir: string = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-'));
const defaultContext: string = `https://github.com/${github.context.repo.owner}/${
github.context.repo.repo
}.git#${github.context.ref.replace(/^refs\//, '')}`;

export interface Inputs {
context: string;
file: string;
Expand All @@ -32,7 +28,21 @@ export interface Inputs {
githubToken: string;
}

export async function getInputs(): Promise<Inputs> {
export function defaultContext(): string {
return `https://github.com/${github.context.repo.owner}/${
github.context.repo.repo
}.git#${github.context?.ref?.replace(/^refs\//, '')}`;
}

export function tmpDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep);
}

export function tmpNameSync(options?: tmp.TmpNameOptions): string {
return tmp.tmpNameSync(options);
}

export async function getInputs(defaultContext: string): Promise<Inputs> {
return {
context: core.getInput('context') || defaultContext,
file: core.getInput('file') || 'Dockerfile',
Expand All @@ -55,15 +65,15 @@ export async function getInputs(): Promise<Inputs> {
};
}

export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx'];
args.push.apply(args, await getBuildArgs(inputs, buildxVersion));
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
args.push.apply(args, await getCommonArgs(inputs));
args.push(inputs.context);
return args;
}

async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['build'];
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
Expand All @@ -83,12 +93,17 @@ async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Arra
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
}
if (inputs.platforms.length == 0 || semver.satisfies(buildxVersion, '>=0.4.2')) {
args.push('--iidfile', await buildx.getImageIDFile());
}
let isLocalOrTarExporter: boolean = false;
await asyncForEach(inputs.outputs, async output => {
if (output.startsWith('type=local') || output.startsWith('type=tar')) {
isLocalOrTarExporter = true;
}
args.push('--output', output);
});
// TODO: Remove platforms length cond when buildx >0.4.2 available on runner (docker/buildx#351)
if (inputs.platforms.length == 0 && !isLocalOrTarExporter && semver.satisfies(buildxVersion, '>=0.4.2')) {
args.push('--iidfile', await buildx.getImageIDFile());
}
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
Expand Down
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ async function run(): Promise<void> {
core.setFailed(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
return;
}
stateHelper.setTmpDir(context.tmpDir);
stateHelper.setTmpDir(context.tmpDir());

const buildxVersion = await buildx.getVersion();
core.info(`📣 Buildx version: ${buildxVersion}`);

let inputs: context.Inputs = await context.getInputs();
const defContext = context.defaultContext();
let inputs: context.Inputs = await context.getInputs(defContext);

core.info(`🏃 Starting build...`);
const args: string[] = await context.getArgs(inputs, buildxVersion);
const args: string[] = await context.getArgs(inputs, defContext, buildxVersion);
await exec.exec('docker', args);

const imageID = await buildx.getImageID();
Expand Down

0 comments on commit 08566ae

Please sign in to comment.