forked from docker/setup-buildx-action
/
main.ts
180 lines (164 loc) · 6.31 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as uuid from 'uuid';
import * as auth from './auth';
import * as buildx from './buildx';
import * as context from './context';
import * as docker from './docker';
import * as stateHelper from './state-helper';
import * as util from './util';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
const inputs: context.Inputs = await context.getInputs();
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
// standalone if docker cli not available
const standalone = !(await docker.isAvailable());
stateHelper.setStandalone(standalone);
core.startGroup(`Docker info`);
if (standalone) {
core.info(`Docker info skipped in standalone mode`);
} else {
await exec.exec('docker', ['version'], {
failOnStdErr: false
});
await exec.exec('docker', ['info'], {
failOnStdErr: false
});
}
core.endGroup();
if (util.isValidUrl(inputs.version)) {
if (standalone) {
throw new Error(`Cannot build from source without the Docker CLI`);
}
core.startGroup(`Build and install buildx`);
await buildx.build(inputs.version, dockerConfigHome, standalone);
core.endGroup();
} else if (!(await buildx.isAvailable(standalone)) || inputs.version) {
core.startGroup(`Download and install buildx`);
await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone);
core.endGroup();
}
const buildxVersion = await buildx.getVersion(standalone);
await core.group(`Buildx version`, async () => {
const versionCmd = buildx.getCommand(['version'], standalone);
await exec.exec(versionCmd.commandLine, versionCmd.args, {
failOnStdErr: false
});
});
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
context.setOutput('name', builderName);
stateHelper.setBuilderName(builderName);
const credsdir = path.join(dockerConfigHome, 'buildx', 'creds', builderName);
fs.mkdirSync(credsdir, {recursive: true});
stateHelper.setCredsDir(credsdir);
if (inputs.driver !== 'docker') {
core.startGroup(`Creating a new builder instance`);
const authOpts = auth.setCredentials(credsdir, 0, inputs.driver, inputs.endpoint);
if (authOpts.length > 0) {
inputs.driverOpts = [...inputs.driverOpts, ...authOpts];
}
const createArgs: Array<string> = ['create', '--name', builderName, '--driver', inputs.driver];
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
createArgs.push('--driver-opt', driverOpt);
});
if (inputs.driver != 'remote' && inputs.buildkitdFlags) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
}
}
if (inputs.use) {
createArgs.push('--use');
}
if (inputs.endpoint) {
createArgs.push(inputs.endpoint);
}
if (inputs.driver != 'remote') {
if (inputs.config) {
createArgs.push('--config', await buildx.getConfigFile(inputs.config));
} else if (inputs.configInline) {
createArgs.push('--config', await buildx.getConfigInline(inputs.configInline));
}
}
const createCmd = buildx.getCommand(createArgs, standalone);
await exec.exec(createCmd.commandLine, createCmd.args);
core.endGroup();
core.startGroup(`Booting builder`);
const bootstrapArgs: Array<string> = ['inspect', '--bootstrap'];
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
bootstrapArgs.push('--builder', builderName);
}
const bootstrapCmd = buildx.getCommand(bootstrapArgs, standalone);
await exec.exec(bootstrapCmd.commandLine, bootstrapCmd.args);
core.endGroup();
}
if (inputs.install) {
if (standalone) {
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
}
core.startGroup(`Setting buildx as default builder`);
await exec.exec('docker', ['buildx', 'install']);
core.endGroup();
}
core.startGroup(`Inspect builder`);
const builder = await buildx.inspect(builderName, standalone);
core.info(JSON.stringify(builder, undefined, 2));
context.setOutput('driver', builder.driver);
context.setOutput('endpoint', builder.node_endpoint);
context.setOutput('status', builder.node_status);
context.setOutput('flags', builder.node_flags);
context.setOutput('platforms', builder.node_platforms);
core.endGroup();
if (!standalone && inputs.driver == 'docker-container') {
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
core.startGroup(`BuildKit version`);
core.info(await buildx.getBuildKitVersion(`buildx_buildkit_${builder.node_name}`));
core.endGroup();
}
if (core.isDebug() || builder.node_flags?.includes('--debug')) {
stateHelper.setDebug('true');
}
} catch (error) {
core.setFailed(error.message);
}
}
async function cleanup(): Promise<void> {
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
core.startGroup(`BuildKit container logs`);
await exec
.getExecOutput('docker', ['logs', `${stateHelper.containerName}`], {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
core.endGroup();
}
if (stateHelper.builderName.length > 0) {
core.startGroup(`Removing builder`);
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone));
await exec
.getExecOutput(rmCmd.commandLine, rmCmd.args, {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
core.endGroup();
}
if (stateHelper.credsDir.length > 0 && fs.existsSync(stateHelper.credsDir)) {
core.info(`Cleaning up credentials`);
fs.rmdirSync(stateHelper.credsDir, {recursive: true});
}
}
if (!stateHelper.IsPost) {
run();
} else {
cleanup();
}