Skip to content

Commit 8b7abd5

Browse files
tpavloviCopilotivanduplenskikh
authored
Guarantee pipeline failure for AzurePowerShellV5 (#21377)
* Guarantee pipeline failure for AzurePowerShellV5 Added cleanupExitCode added logs added try-catch removed logs added logs changed condition Uncommented the fix for the missing import error Update error messages for AzurePowerShell task Update the task version Guarantee pipeline failure for AzurePowerShellV5 Uncommented the fix for the missing import error Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Bump AzurePowerShell task version to 5.267.0 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Ivan Duplenskikh <115665590+ivanduplenskikh@users.noreply.github.com>
1 parent 20637a6 commit 8b7abd5

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true }
22

3-
. "$PSScriptRoot/Utility.ps1"
4-
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"
3+
Set-StrictMode -Version Latest
4+
$ErrorActionPreference = 'Stop'
5+
try {
6+
. "$PSScriptRoot/Utility.ps1"
7+
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"
58

6-
Update-PSModulePathForHostedAgentLinux
7-
Disconnect-AzureAndClearContext -restrictContext 'True'
9+
Update-PSModulePathForHostedAgentLinux
10+
Disconnect-AzureAndClearContext -restrictContext 'True'
811

9-
if ($env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID) {
10-
$env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID = ""
11-
$env:AZURESUBSCRIPTION_CLIENT_ID = ""
12-
$env:AZURESUBSCRIPTION_TENANT_ID = ""
12+
if ($env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID) {
13+
$env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID = ""
14+
$env:AZURESUBSCRIPTION_CLIENT_ID = ""
15+
$env:AZURESUBSCRIPTION_TENANT_ID = ""
16+
}
17+
}
18+
catch {
19+
Write-Host "##vso[task.logissue type=error]$($_.Exception.Message)"
20+
exit 1
1321
}

Tasks/AzurePowerShellV5/azurepowershell.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ async function run() {
180180
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
181181
}
182182
finally {
183+
let cleanupExitCode = 0;
183184
try {
184185
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
185186
.arg('-NoLogo')
@@ -197,10 +198,16 @@ async function run() {
197198
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
198199
ignoreReturnCode: true
199200
};
200-
await powershell.exec(options);
201+
cleanupExitCode = await powershell.exec(options);
202+
tl.debug(`Cleanup exit code: ${cleanupExitCode}`);
201203
}
202204
catch (err) {
203205
tl.debug("Az-clearContext not completed due to an error");
206+
tl.setResult(tl.TaskResult.Failed, `Cleanup failed with error message: ${err.message}`);
207+
}
208+
209+
if (cleanupExitCode !== 0) {
210+
tl.setResult(tl.TaskResult.Failed, `Cleanup failed with exit code: ${cleanupExitCode}`);
204211
}
205212
}
206213
}

Tasks/AzurePowerShellV5/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 266,
20+
"Minor": 267,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "Added support for Az Module and cross platform agents.",

Tasks/AzurePowerShellV5/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 266,
20+
"Minor": 267,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "ms-resource:loc.releaseNotes",

0 commit comments

Comments
 (0)