Skip to content

Commit 90a1e46

Browse files
authored
Merge pull request #169 from crazy-max/context-module
move args logic to context module and add tests
2 parents 6c48dad + 5a9fc40 commit 90a1e46

File tree

5 files changed

+145
-41
lines changed

5 files changed

+145
-41
lines changed

__tests__/context.test.ts

+91-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
1+
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
22
import * as fs from 'fs';
33
import * as os from 'os';
44
import * as path from 'path';
5+
import * as uuid from 'uuid';
56
import * as context from '../src/context';
67

78
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
@@ -13,6 +14,95 @@ jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
1314
return path.join(tmpdir, '.tmpname').split(path.sep).join(path.posix.sep);
1415
});
1516

17+
jest.mock('uuid');
18+
jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d');
19+
20+
describe('getCreateArgs', () => {
21+
beforeEach(() => {
22+
process.env = Object.keys(process.env).reduce((object, key) => {
23+
if (!key.startsWith('INPUT_')) {
24+
object[key] = process.env[key];
25+
}
26+
return object;
27+
}, {});
28+
});
29+
30+
// prettier-ignore
31+
test.each([
32+
[
33+
0,
34+
new Map<string, string>([
35+
['install', 'false'],
36+
['use', 'true'],
37+
]),
38+
[
39+
'create',
40+
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
41+
'--driver', 'docker-container',
42+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
43+
'--use'
44+
]
45+
],
46+
[
47+
1,
48+
new Map<string, string>([
49+
['driver', 'docker'],
50+
['install', 'false'],
51+
['use', 'true'],
52+
]),
53+
[
54+
'create',
55+
'--name', 'default',
56+
'--driver', 'docker',
57+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
58+
'--use'
59+
]
60+
],
61+
[
62+
2,
63+
new Map<string, string>([
64+
['install', 'false'],
65+
['use', 'false'],
66+
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
67+
]),
68+
[
69+
'create',
70+
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
71+
'--driver', 'docker-container',
72+
'--driver-opt', 'image=moby/buildkit:master',
73+
'--driver-opt', 'network=host',
74+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host'
75+
]
76+
],
77+
[
78+
3,
79+
new Map<string, string>([
80+
['driver', 'remote'],
81+
['endpoint', 'tls://foo:1234'],
82+
['install', 'false'],
83+
['use', 'true'],
84+
]),
85+
[
86+
'create',
87+
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
88+
'--driver', 'remote',
89+
'--use',
90+
'tls://foo:1234'
91+
]
92+
],
93+
])(
94+
'[%d] given %p as inputs, returns %p',
95+
async (num: number, inputs: Map<string, string>, expected: Array<string>) => {
96+
inputs.forEach((value: string, name: string) => {
97+
setInput(name, value);
98+
});
99+
const inp = await context.getInputs();
100+
const res = await context.getCreateArgs(inp, '0.9.0');
101+
expect(res).toEqual(expected);
102+
}
103+
);
104+
});
105+
16106
describe('getInputList', () => {
17107
it('handles single line correctly', async () => {
18108
await setInput('foo', 'bar');

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

+42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import fs from 'fs';
22
import * as os from 'os';
33
import path from 'path';
44
import * as tmp from 'tmp';
5+
import * as uuid from 'uuid';
6+
import * as buildx from './buildx';
57
import * as core from '@actions/core';
68
import {issueCommand} from '@actions/core/lib/command';
79

@@ -22,6 +24,7 @@ export function tmpNameSync(options?: tmp.TmpNameOptions): string {
2224

2325
export interface Inputs {
2426
version: string;
27+
name: string;
2528
driver: string;
2629
driverOpts: string[];
2730
buildkitdFlags: string;
@@ -35,6 +38,7 @@ export interface Inputs {
3538
export async function getInputs(): Promise<Inputs> {
3639
return {
3740
version: core.getInput('version'),
41+
name: getBuilderName(core.getInput('driver') || 'docker-container'),
3842
driver: core.getInput('driver') || 'docker-container',
3943
driverOpts: await getInputList('driver-opts', true),
4044
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
@@ -46,6 +50,44 @@ export async function getInputs(): Promise<Inputs> {
4650
};
4751
}
4852

53+
export function getBuilderName(driver: string): string {
54+
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
55+
}
56+
57+
export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
58+
const args: Array<string> = ['create', '--name', inputs.name, '--driver', inputs.driver];
59+
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
60+
await asyncForEach(inputs.driverOpts, async driverOpt => {
61+
args.push('--driver-opt', driverOpt);
62+
});
63+
if (inputs.driver != 'remote' && inputs.buildkitdFlags) {
64+
args.push('--buildkitd-flags', inputs.buildkitdFlags);
65+
}
66+
}
67+
if (inputs.use) {
68+
args.push('--use');
69+
}
70+
if (inputs.driver != 'remote') {
71+
if (inputs.config) {
72+
args.push('--config', await buildx.getConfigFile(inputs.config));
73+
} else if (inputs.configInline) {
74+
args.push('--config', await buildx.getConfigInline(inputs.configInline));
75+
}
76+
}
77+
if (inputs.endpoint) {
78+
args.push(inputs.endpoint);
79+
}
80+
return args;
81+
}
82+
83+
export async function getInspectArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
84+
const args: Array<string> = ['inspect', '--bootstrap'];
85+
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
86+
args.push('--builder', inputs.name);
87+
}
88+
return args;
89+
}
90+
4991
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
5092
const items = core.getInput(name);
5193
if (items == '') {

src/main.ts

+10-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fs from 'fs';
22
import * as os from 'os';
33
import * as path from 'path';
4-
import * as uuid from 'uuid';
54
import * as auth from './auth';
65
import * as buildx from './buildx';
76
import * as context from './context';
@@ -54,11 +53,10 @@ async function run(): Promise<void> {
5453
});
5554
});
5655

57-
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
58-
context.setOutput('name', builderName);
59-
stateHelper.setBuilderName(builderName);
56+
context.setOutput('name', inputs.name);
57+
stateHelper.setBuilderName(inputs.name);
6058

61-
const credsdir = path.join(dockerConfigHome, 'buildx', 'creds', builderName);
59+
const credsdir = path.join(dockerConfigHome, 'buildx', 'creds', inputs.name);
6260
fs.mkdirSync(credsdir, {recursive: true});
6361
stateHelper.setCredsDir(credsdir);
6462

@@ -68,42 +66,16 @@ async function run(): Promise<void> {
6866
if (authOpts.length > 0) {
6967
inputs.driverOpts = [...inputs.driverOpts, ...authOpts];
7068
}
71-
const createArgs: Array<string> = ['create', '--name', builderName, '--driver', inputs.driver];
72-
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
73-
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
74-
createArgs.push('--driver-opt', driverOpt);
75-
});
76-
if (inputs.driver != 'remote' && inputs.buildkitdFlags) {
77-
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
78-
}
79-
}
80-
if (inputs.use) {
81-
createArgs.push('--use');
82-
}
83-
if (inputs.endpoint) {
84-
createArgs.push(inputs.endpoint);
85-
}
86-
if (inputs.driver != 'remote') {
87-
if (inputs.config) {
88-
createArgs.push('--config', await buildx.getConfigFile(inputs.config));
89-
} else if (inputs.configInline) {
90-
createArgs.push('--config', await buildx.getConfigInline(inputs.configInline));
91-
}
92-
}
93-
const createCmd = buildx.getCommand(createArgs, standalone);
69+
const createCmd = buildx.getCommand(await context.getCreateArgs(inputs, buildxVersion), standalone);
9470
await exec.exec(createCmd.commandLine, createCmd.args);
9571
core.endGroup();
96-
97-
core.startGroup(`Booting builder`);
98-
const bootstrapArgs: Array<string> = ['inspect', '--bootstrap'];
99-
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
100-
bootstrapArgs.push('--builder', builderName);
101-
}
102-
const bootstrapCmd = buildx.getCommand(bootstrapArgs, standalone);
103-
await exec.exec(bootstrapCmd.commandLine, bootstrapCmd.args);
104-
core.endGroup();
10572
}
10673

74+
core.startGroup(`Booting builder`);
75+
const inspectCmd = buildx.getCommand(await context.getInspectArgs(inputs, buildxVersion), standalone);
76+
await exec.exec(inspectCmd.commandLine, inspectCmd.args);
77+
core.endGroup();
78+
10779
if (inputs.install) {
10880
if (standalone) {
10981
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
@@ -114,7 +86,7 @@ async function run(): Promise<void> {
11486
}
11587

11688
core.startGroup(`Inspect builder`);
117-
const builder = await buildx.inspect(builderName, standalone);
89+
const builder = await buildx.inspect(inputs.name, standalone);
11890
const firstNode = builder.nodes[0];
11991
core.info(JSON.stringify(builder, undefined, 2));
12092
context.setOutput('driver', builder.driver);

0 commit comments

Comments
 (0)