-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.ts
More file actions
286 lines (267 loc) · 9.21 KB
/
main.ts
File metadata and controls
286 lines (267 loc) · 9.21 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import * as task from 'azure-pipelines-task-lib/task';
import {TaskResult} from 'azure-pipelines-task-lib/task';
import path from 'path';
import {populateDefaults} from '../../../common/src/envvars';
import {
devcontainer,
DevContainerCliBuildArgs,
DevContainerCliExecArgs,
DevContainerCliUpArgs,
MAJOR_VERSION_FALLBACK
} from '../../../common/src/dev-container-cli';
import {isDockerBuildXInstalled, pushImage} from './docker';
import {isSkopeoInstalled, copyImage} from './skopeo';
import {exec} from './exec';
export async function runMain(): Promise<void> {
try {
task.setTaskVariable('hasRunMain', 'true');
const buildXInstalled = await isDockerBuildXInstalled();
if (!buildXInstalled) {
console.log(
'### WARNING: docker buildx not available: add a step to set up with docker/setup-buildx-action - see https://github.com/devcontainers/ci/blob/main/docs/azure-devops-task.md',
);
return;
}
const specifiedDevContainerCliVersion =
task.getInput('cliVersion') ?? MAJOR_VERSION_FALLBACK;
const devContainerCliInstalled = await devcontainer.isCliInstalled(
exec,
specifiedDevContainerCliVersion,
);
if (!devContainerCliInstalled) {
console.log('Installing @devcontainers/cli...');
const success = await devcontainer.installCli(
exec,
specifiedDevContainerCliVersion,
);
if (!success) {
task.setResult(
task.TaskResult.Failed,
'@devcontainers/cli install failed!',
);
return;
}
}
const checkoutPath = task.getInput('checkoutPath') ?? '';
const imageName = task.getInput('imageName');
const imageTag = task.getInput('imageTag');
const platform = task.getInput('platform');
const subFolder = task.getInput('subFolder') ?? '.';
const relativeConfigFile = task.getInput('configFile');
const runCommand = task.getInput('runCmd');
const envs = task.getInput('env')?.split('\n') ?? [];
const inheritEnv = (task.getInput('inheritEnv') ?? 'false') === 'true';
const inputEnvsWithDefaults = populateDefaults(envs, inheritEnv);
const cacheFrom = task.getInput('cacheFrom')?.split('\n') ?? [];
const noCache = (task.getInput('noCache') ?? 'false') === 'true';
const cacheTo = task.getInput('cacheTo')?.split('\n') ?? [];
const skipContainerUserIdUpdate =
(task.getInput('skipContainerUserIdUpdate') ?? 'false') === 'true';
if (platform) {
const skopeoInstalled = await isSkopeoInstalled();
if (!skopeoInstalled) {
console.log(
'skopeo not available and is required for multi-platform builds - make sure it is installed on your build agent',
);
return;
}
}
const buildxOutput = platform ? 'type=oci,dest=/tmp/output.tar' : undefined;
const log = (message: string): void => console.log(message);
const workspaceFolder = path.resolve(checkoutPath, subFolder);
const configFile =
relativeConfigFile && path.resolve(checkoutPath, relativeConfigFile);
const resolvedImageTag = imageTag ?? 'latest';
const imageTagArray = resolvedImageTag.split(/\s*,\s*/);
const fullImageNameArray: string[] = [];
for (const tag of imageTagArray) {
fullImageNameArray.push(`${imageName}:${tag}`);
}
if (imageName) {
if (fullImageNameArray.length === 1) {
if (!noCache && !cacheFrom.includes(fullImageNameArray[0])) {
// If the cacheFrom options don't include the fullImageName, add it here
// This ensures that when building a PR where the image specified in the action
// isn't included in devcontainer.json (or docker-compose.yml), the action still
// resolves a previous image for the tag as a layer cache (if pushed to a registry)
cacheFrom.splice(0, 0, fullImageNameArray[0]);
}
} else {
// Don't automatically add --cache-from if multiple image tags are specified
console.log(
'Not adding --cache-from automatically since multiple image tags were supplied',
);
}
} else {
console.log(
'!! imageTag specified without specifying imageName - ignoring imageTag',
);
}
const buildArgs: DevContainerCliBuildArgs = {
workspaceFolder,
configFile,
imageName: fullImageNameArray,
platform,
additionalCacheFroms: cacheFrom,
output: buildxOutput,
noCache,
cacheTo,
};
console.log('\n\n');
console.log('***');
console.log('*** Building the dev container');
console.log('***');
const buildResult = await devcontainer.build(buildArgs, log);
if (buildResult.outcome !== 'success') {
console.log(
`### ERROR: Dev container build failed: ${buildResult.message} (exit code: ${buildResult.code})\n${buildResult.description}`,
);
task.setResult(TaskResult.Failed, buildResult.message);
}
if (buildResult.outcome !== 'success') {
return;
}
if (runCommand) {
console.log('\n\n');
console.log('***');
console.log('*** Starting the dev container');
console.log('***');
const upArgs: DevContainerCliUpArgs = {
workspaceFolder,
configFile,
additionalCacheFroms: cacheFrom,
skipContainerUserIdUpdate,
env: inputEnvsWithDefaults,
};
const upResult = await devcontainer.up(upArgs, log);
if (upResult.outcome !== 'success') {
console.log(
`### ERROR: Dev container up failed: ${upResult.message} (exit code: ${upResult.code})\n${upResult.description}`,
);
task.setResult(TaskResult.Failed, upResult.message);
}
if (upResult.outcome !== 'success') {
return;
}
console.log('\n\n');
console.log('***');
console.log('*** Running command in the dev container');
console.log('***');
const execArgs: DevContainerCliExecArgs = {
workspaceFolder,
configFile,
command: ['bash', '-c', runCommand],
env: inputEnvsWithDefaults,
};
let execLogString = '';
const execLog = (message: string): void => {
console.log(message);
if (!message.includes('@devcontainers/cli')) {
execLogString += message;
}
};
const execResult = await devcontainer.exec(execArgs, execLog);
if (execResult !== 0) {
console.log(
`### ERROR: Dev container exec failed (exit code: ${execResult})`,
);
task.setResult(
TaskResult.Failed,
`Dev container exec failed (exit code: ${execResult})`,
);
return;
}
if (execLogString.length >= 25000) {
execLogString = execLogString.substring(0, 24963);
execLogString += 'TRUNCATED TO 25K CHAR MAX OUTPUT SIZE';
}
console.log(`##vso[task.setvariable variable=runCmdOutput]${execLog}`);
} else {
console.log('No runCmd set - skipping starting/running container');
}
// TODO - should we stop the container?
} catch (err) {
task.setResult(task.TaskResult.Failed, err.message);
}
}
export async function runPost(): Promise<void> {
const pushOption = task.getInput('push');
const imageName = task.getInput('imageName');
const pushOnFailedBuild =
(task.getInput('pushOnFailedBuild') ?? 'false') === 'true';
// default to 'never' if not set and no imageName
if (pushOption === 'never' || (!pushOption && !imageName)) {
console.log(`Image push skipped because 'push' is set to '${pushOption}'`);
return;
}
// default to 'filter' if not set and imageName is set
if (pushOption === 'filter' || (!pushOption && imageName)) {
// https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
const agentJobStatus = process.env.AGENT_JOBSTATUS;
switch (agentJobStatus) {
case 'Succeeded':
case 'SucceededWithIssues':
// continue
break;
default:
if (!pushOnFailedBuild) {
console.log(
`Image push skipped because Agent JobStatus is '${agentJobStatus}'`,
);
return;
}
}
const buildReasonsForPush: string[] =
task.getInput('buildReasonsForPush')?.split('\n') ?? [];
const sourceBranchFilterForPush: string[] =
task.getInput('sourceBranchFilterForPush')?.split('\n') ?? [];
// check build reason is allowed
const buildReason = process.env.BUILD_REASON;
if (
buildReasonsForPush.length !== 0 && // empty filter allows all
!buildReasonsForPush.some(s => s === buildReason)
) {
console.log(
`Image push skipped because buildReason (${buildReason}) is not in buildReasonsForPush`,
);
return;
}
// check branch is allowed
const sourceBranch = process.env.BUILD_SOURCEBRANCH;
if (
sourceBranchFilterForPush.length !== 0 && // empty filter allows all
!sourceBranchFilterForPush.some(s => s === sourceBranch)
) {
console.log(
`Image push skipped because source branch (${sourceBranch}) is not in sourceBranchFilterForPush`,
);
return;
}
}
if (!imageName) {
if (pushOption) {
// pushOption was set (and not to "never") - give an error that imageName is required
task.setResult(
task.TaskResult.Failed,
`imageName input is required to push images (push: ${pushOption})`,
);
}
return;
}
const imageTag = task.getInput('imageTag') ?? 'latest';
const imageTagArray = imageTag.split(/\s*,\s*/);
const platform = task.getInput('platform');
if (platform) {
for (const tag of imageTagArray) {
console.log(`Copying multiplatform image '${imageName}:${tag}'...`);
const imageSource = `oci-archive:/tmp/output.tar:${tag}`;
const imageDest = `docker://${imageName}:${tag}`;
await copyImage(true, imageSource, imageDest);
}
} else {
for (const tag of imageTagArray) {
console.log(`Pushing image '${imageName}:${tag}'...`);
await pushImage(imageName, tag);
}
}
}