-
Notifications
You must be signed in to change notification settings - Fork 13
/
buildx.ts
201 lines (163 loc) · 5.22 KB
/
buildx.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as fs from 'fs';
import * as os from 'os';
import * as outputs from './outputs';
import * as path from 'path';
import * as semver from 'semver';
import * as state from './state';
import * as tc from '@actions/tool-cache';
import * as uuid from 'uuid';
import {debug, endGroup, info, startGroup, warning} from '@actions/core';
import {HttpClient} from '@actions/http-client';
import {exec, getExecOutput} from '@actions/exec';
export async function setup(
builderName: string,
builderImage: string
): Promise<void> {
if (!(await isAvailable())) {
await install();
}
const buildxVersion = await getVersion();
info(`📣 Buildx version: ${buildxVersion}`);
if (!builderName) {
builderName = `builder-${uuid.v4()}`;
state.setBuilder(builderName);
await createBuilder(builderName, builderImage);
await bootBuilder(builderName);
}
outputs.setBuilder(builderName);
await useBuilder(builderName);
}
export async function stop(builderName: string): Promise<void> {
if (builderName.length === 0) {
return;
}
startGroup(`🧹 Cleaning up builder`);
const res = await getExecOutput('docker', ['buildx', 'rm', builderName]);
if (res.stderr !== '' && res.exitCode) {
warning(res.stderr);
}
endGroup();
}
async function createBuilder(name: string, image: string): Promise<void> {
startGroup(`🔨 Creating a new builder instance`);
const context = 'builders';
await exec('docker', ['context', 'create', context]);
const args = [
'buildx',
'create',
'--name',
name,
'--driver',
'docker-container'
];
if (image) {
args.push('--driver-opt', `image=${image}`);
}
args.push(context);
await exec('docker', args);
endGroup();
}
async function bootBuilder(name: string): Promise<void> {
startGroup(`🏃 Booting builder`);
const args = ['buildx', 'inspect', '--bootstrap', '--builder', name];
await exec('docker', args);
endGroup();
}
async function useBuilder(name: string): Promise<void> {
startGroup(`Using builder`);
const args = ['buildx', 'use', name];
await exec('docker', args);
await ls();
endGroup();
}
async function getVersion(): Promise<string> {
const res = await getExecOutput('docker', ['buildx', 'version'], {
silent: true
});
if (res.stderr !== '' && res.exitCode) {
throw new Error(res.stderr);
}
return parseVersion(res.stdout);
}
export async function inspect(tag: string): Promise<void> {
startGroup(`📦 Pushed image`);
const res = await getExecOutput('docker', [
'buildx',
'imagetools',
'inspect',
tag
]);
if (res.stderr !== '' && res.exitCode) {
throw new Error(res.stderr);
}
endGroup();
}
async function ls(): Promise<void> {
const res = await getExecOutput('docker', ['buildx', 'ls']);
if (res.stderr !== '' && res.exitCode) {
throw new Error(res.stderr);
}
}
async function parseVersion(stdout: string): Promise<string> {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
}
const version = semver.clean(matches[1]);
if (!version) {
throw new Error(`Cannot clean buildx semver version`);
}
return version;
}
async function isAvailable(): Promise<boolean> {
const res = await getExecOutput('docker', ['buildx']);
return res.stderr === '' && !res.exitCode;
}
async function install(inputVersion = 'latest'): Promise<void> {
startGroup(`👉 Installing Buildx`);
const release: GithubRelease | null = await getRelease(inputVersion);
if (!release) {
throw new Error(`Cannot find buildx ${inputVersion} release`);
}
const version = release.tag_name.replace(/^(v)/, '');
let toolPath: string;
toolPath = tc.find('buildx', version);
if (!toolPath) {
const c = semver.clean(version) || '';
if (!semver.valid(c)) {
throw new Error(`Invalid Buildx version "${version}".`);
}
toolPath = await download(version);
}
const dockerConfigHome: string =
process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
debug(`Plugins dir is ${pluginsDir}`);
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, {recursive: true});
}
const binName: string =
os.platform() === 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const pluginPath: string = path.join(pluginsDir, binName);
debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, binName), pluginPath);
info('🔨 Fixing perms...');
fs.chmodSync(pluginPath, '0755');
endGroup();
}
async function download(version: string): Promise<string> {
const downloadUrl = `https://github.com/docker/buildx/releases/download/v${version}/buildx-v${version}.linux-amd64`;
info(`⬇️ Downloading ${downloadUrl}...`);
const downloadPath = await tc.downloadTool(downloadUrl);
debug(`Downloaded to ${downloadPath}`);
return tc.cacheFile(downloadPath, 'docker-buildx', 'buildx', version);
}
interface GithubRelease {
id: number;
tag_name: string;
}
async function getRelease(version: string): Promise<GithubRelease | null> {
const url = `https://github.com/docker/buildx/releases/${version}`;
const http = new HttpClient('setup-buildx');
return (await http.getJson<GithubRelease>(url)).result;
}