-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBuildImageStep.ts
More file actions
99 lines (86 loc) · 5.4 KB
/
BuildImageStep.ts
File metadata and controls
99 lines (86 loc) · 5.4 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { sendRequestWithTimeout, type AzExtPipelineResponse } from "@microsoft/vscode-azext-azureutils";
import { GenericParentTreeItem, GenericTreeItem, activityFailContext, activityFailIcon, activitySuccessContext, activitySuccessIcon, nonNullProp, nonNullValue, nonNullValueAndProp, type AzExtTreeItem } from "@microsoft/vscode-azext-utils";
import { ThemeColor, ThemeIcon, window, type MessageItem } from "vscode";
import { acrDomain } from "../../../../constants";
import { ExecuteActivityOutputStepBase, type ExecuteActivityOutput } from "../../../../utils/activity/ExecuteActivityOutputStepBase";
import { createActivityChildContext } from "../../../../utils/activity/activityUtils";
import { localize } from "../../../../utils/localize";
import { openAcrBuildLogs, type AcrBuildResults } from "../../openAcrBuildLogs";
import { type BuildImageInAzureImageSourceContext } from "./BuildImageInAzureImageSourceContext";
import { buildImageInAzure } from "./buildImageInAzure";
export class BuildImageStep extends ExecuteActivityOutputStepBase<BuildImageInAzureImageSourceContext> {
public priority: number = 450;
protected acrBuildError: AcrBuildResults;
protected async executeCore(context: BuildImageInAzureImageSourceContext): Promise<void> {
context.registryDomain = acrDomain;
const run = await buildImageInAzure(context);
const outputImages = run?.outputImages;
context.telemetry.properties.outputImagesCount = outputImages?.length?.toString();
if (outputImages) {
const image = outputImages[0];
context.image = `${image.registry}/${image.repository}:${image.tag}`;
} else {
const logSasUrl = (await context.client.runs.getLogSasUrl(context.resourceGroupName, context.registryName, nonNullValue(context.run.runId))).logLink;
const response: AzExtPipelineResponse = await sendRequestWithTimeout(context, { method: 'GET', url: nonNullValue(logSasUrl) }, 2500, undefined);
const content: string = nonNullProp(response, 'bodyAsText');
this.acrBuildError = {
name: nonNullValueAndProp(context.run, 'name'),
runId: nonNullValueAndProp(context.run, 'id'),
content
};
const viewLogsButton: MessageItem = { title: localize('viewLogs', 'View Logs') };
const errorMessage = localize('noImagesBuilt', 'Failed to build image. View logs for more details.');
void window.showErrorMessage(errorMessage, viewLogsButton).then(async result => {
if (result === viewLogsButton) {
await openAcrBuildLogs(context, this.acrBuildError);
}
});
context.errorHandling.suppressDisplay = true;
throw new Error(errorMessage);
}
}
public shouldExecute(context: BuildImageInAzureImageSourceContext): boolean {
return !context.image;
}
protected createSuccessOutput(context: BuildImageInAzureImageSourceContext): ExecuteActivityOutput {
return {
item: new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['buildImageStepSuccessItem', activitySuccessContext]),
label: localize('buildImageLabel', 'Build image "{0}" in registry "{1}"', context.imageName, context.registryName),
iconPath: activitySuccessIcon
}),
message: [
localize('buildImageSuccess', 'Finished building image "{0}" in registry "{1}".', context.imageName, context.registryName),
localize('useImage', 'Using image "{0}".', context.image)
]
};
}
protected createFailOutput(context: BuildImageInAzureImageSourceContext): ExecuteActivityOutput {
let loadMoreChildrenImpl: (() => Promise<AzExtTreeItem[]>) | undefined;
if (this.acrBuildError) {
loadMoreChildrenImpl = () => {
const buildImageLogsItem = new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['logsLinkItem']),
label: localize('buildImageLogs', 'Click to view build image logs'),
iconPath: new ThemeIcon('note', new ThemeColor('terminal.ansiWhite')),
commandId: 'containerApps.openAcrBuildLogs',
});
buildImageLogsItem.commandArgs = [this.acrBuildError];
return Promise.resolve([buildImageLogsItem]);
};
}
return {
item: new GenericParentTreeItem(undefined, {
contextValue: createActivityChildContext(['buildImageStepFailItem', activityFailContext]),
label: localize('buildImageLabel', 'Build image "{0}" in registry "{1}"', context.imageName, context.registryName),
iconPath: activityFailIcon,
loadMoreChildrenImpl: loadMoreChildrenImpl ?? (() => Promise.resolve([]))
}),
message: localize('buildImageFail', 'Failed to build image "{0}" in registry "{1}".', context.imageName, context.registryName)
};
}
}