-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRunStep.ts
More file actions
73 lines (64 loc) · 3.79 KB
/
RunStep.ts
File metadata and controls
73 lines (64 loc) · 3.79 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type DockerBuildRequest as AcrDockerBuildRequest } from "@azure/arm-containerregistry";
import { AzExtFsExtra, GenericTreeItem, activityFailContext, activityFailIcon } from "@microsoft/vscode-azext-utils";
import * as retry from 'p-retry';
import * as path from 'path';
import { type Progress } from "vscode";
import { ext } from "../../../../extensionVariables";
import { ExecuteActivityOutputStepBase, type ExecuteActivityOutput } from "../../../../utils/activity/ExecuteActivityOutputStepBase";
import { createActivityChildContext } from "../../../../utils/activity/activityUtils";
import { localize } from "../../../../utils/localize";
import { type BuildImageInAzureImageSourceContext } from "./BuildImageInAzureImageSourceContext";
export class RunStep extends ExecuteActivityOutputStepBase<BuildImageInAzureImageSourceContext> {
public priority: number = 440;
protected async executeCore(context: BuildImageInAzureImageSourceContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
// Need to keep the additional try wrapper here to execute finally, then we can catch any error that percolates up and display its output
try {
const runRequest: AcrDockerBuildRequest = {
type: 'DockerBuildRequest',
imageNames: [context.imageName],
isPushEnabled: true,
sourceLocation: context.uploadedSourceLocation,
platform: { os: context.os },
dockerFilePath: path.basename(context.dockerfilePath) /* Assume the dockerfile is always in the root of the source */
};
const retries = 3;
await retry(
async (currentAttempt: number): Promise<void> => {
const message: string = currentAttempt === 1 ?
localize('buildingImage', 'Building image...') :
localize('buildingImageAttempt', 'Building image (Attempt {0}/{1})...', currentAttempt, retries + 1);
progress.report({ message: message });
ext.outputChannel.appendLog(message);
runRequest.sourceLocation = 'a'
context.run = await context.client.registries.beginScheduleRunAndWait(context.resourceGroupName, context.registryName, runRequest);
},
{ retries, minTimeout: 2 * 1000 }
);
} finally {
if (await AzExtFsExtra.pathExists(context.tarFilePath)) {
await AzExtFsExtra.deleteResource(context.tarFilePath);
}
}
}
public shouldExecute(context: BuildImageInAzureImageSourceContext): boolean {
return !context.run;
}
protected createSuccessOutput(): ExecuteActivityOutput {
// Skip here, success will be output by the build image step
return {};
}
protected createFailOutput(context: BuildImageInAzureImageSourceContext): ExecuteActivityOutput {
return {
item: new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['runStep', activityFailContext]),
label: localize('runLabel', 'Build image "{0}" in registry "{1}"', context.imageName, context.registryName),
iconPath: activityFailIcon
}),
message: localize('runFail', 'Failed to build image "{0}" in registry "{1}".', context.imageName, context.registryName)
};
}
}