From 1f20926c0f90b46b2095b5540c65ad8275cb7059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 00:09:43 +0200 Subject: [PATCH 1/8] Guarantee pipeline failure for AzurePowerShellV5 --- Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 | 2 +- Tasks/AzurePowerShellV5/RemoveAzContext.ps1 | 2 +- Tasks/AzurePowerShellV5/azurepowershell.ts | 3 +++ Tasks/AzurePowerShellV5/task.json | 2 +- Tasks/AzurePowerShellV5/task.loc.json | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 b/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 index 7138fd25a42..b301ff34986 100644 --- a/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 +++ b/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 @@ -1 +1 @@ -Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } +#Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } diff --git a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 index 66070ba29d8..1827d70c940 100644 --- a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 +++ b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 @@ -1,4 +1,4 @@ -Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } +#Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } . "$PSScriptRoot/Utility.ps1" . "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1" diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 4702e5b05d6..72f7fc1f1ea 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -201,6 +201,9 @@ async function run() { } catch (err) { tl.debug("Az-clearContext not completed due to an error"); + //if (tl.getPipelineFeature('SetTaskResultFailed')) { + tl.setResult(tl.TaskResult.Failed, err.message); + //} } } } diff --git a/Tasks/AzurePowerShellV5/task.json b/Tasks/AzurePowerShellV5/task.json index 0a06a8de2d7..00352eaf986 100644 --- a/Tasks/AzurePowerShellV5/task.json +++ b/Tasks/AzurePowerShellV5/task.json @@ -18,7 +18,7 @@ "version": { "Major": 5, "Minor": 264, - "Patch": 0 + "Patch": 1 }, "releaseNotes": "Added support for Az Module and cross platform agents.", "groups": [ diff --git a/Tasks/AzurePowerShellV5/task.loc.json b/Tasks/AzurePowerShellV5/task.loc.json index 09b7061433b..0935b2d45c2 100644 --- a/Tasks/AzurePowerShellV5/task.loc.json +++ b/Tasks/AzurePowerShellV5/task.loc.json @@ -18,7 +18,7 @@ "version": { "Major": 5, "Minor": 264, - "Patch": 0 + "Patch": 1 }, "releaseNotes": "ms-resource:loc.releaseNotes", "groups": [ From 551bc88bdf3ebc9b4bc56a6c22da12308df40146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 00:28:56 +0200 Subject: [PATCH 2/8] Added cleanupExitCode --- Tasks/AzurePowerShellV5/azurepowershell.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 72f7fc1f1ea..796193917d9 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -197,7 +197,10 @@ async function run() { outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR. ignoreReturnCode: true }; - await powershell.exec(options); + let cleanupExitCode = await powershell.exec(options); + if (cleanupExitCode !== 0) { + tl.setResult(tl.TaskResult.Failed, 'Cleanup failed'); + } } catch (err) { tl.debug("Az-clearContext not completed due to an error"); From c398977613081ac4240d9ce710c009026b0ec036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 00:41:10 +0200 Subject: [PATCH 3/8] added logs --- Tasks/AzurePowerShellV5/azurepowershell.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 796193917d9..423d816160b 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -198,6 +198,7 @@ async function run() { ignoreReturnCode: true }; let cleanupExitCode = await powershell.exec(options); + tl.debug(`Cleanup (RemoveAzContext.ps1) exit code: ${cleanupExitCode}`); if (cleanupExitCode !== 0) { tl.setResult(tl.TaskResult.Failed, 'Cleanup failed'); } From 0e447b46e83d2b7b58d8544141c5524ee64f0539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 01:01:45 +0200 Subject: [PATCH 4/8] added try-catch --- Tasks/AzurePowerShellV5/RemoveAzContext.ps1 | 24 ++++++++++++++------- Tasks/AzurePowerShellV5/azurepowershell.ts | 5 +---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 index 1827d70c940..b6f32ccfad7 100644 --- a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 +++ b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 @@ -1,13 +1,21 @@ #Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } -. "$PSScriptRoot/Utility.ps1" -. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1" +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' +try { + . "$PSScriptRoot/Utility.ps1" + . "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1" -Update-PSModulePathForHostedAgentLinux -Disconnect-AzureAndClearContext -restrictContext 'True' + Update-PSModulePathForHostedAgentLinux + Disconnect-AzureAndClearContext -restrictContext 'True' -if ($env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID) { - $env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID = "" - $env:AZURESUBSCRIPTION_CLIENT_ID = "" - $env:AZURESUBSCRIPTION_TENANT_ID = "" + if ($env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID) { + $env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID = "" + $env:AZURESUBSCRIPTION_CLIENT_ID = "" + $env:AZURESUBSCRIPTION_TENANT_ID = "" + } +} +catch { + Write-Host "##vso[task.logissue type=error]$($_.Exception.Message)" + exit 1 } \ No newline at end of file diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 423d816160b..d78713914f0 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -197,11 +197,8 @@ async function run() { outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR. ignoreReturnCode: true }; - let cleanupExitCode = await powershell.exec(options); + await powershell.exec(options); tl.debug(`Cleanup (RemoveAzContext.ps1) exit code: ${cleanupExitCode}`); - if (cleanupExitCode !== 0) { - tl.setResult(tl.TaskResult.Failed, 'Cleanup failed'); - } } catch (err) { tl.debug("Az-clearContext not completed due to an error"); From 459dd967d33d538b4c13c00e5d86941faef98be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 01:15:08 +0200 Subject: [PATCH 5/8] removed logs --- Tasks/AzurePowerShellV5/azurepowershell.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index d78713914f0..72f7fc1f1ea 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -198,7 +198,6 @@ async function run() { ignoreReturnCode: true }; await powershell.exec(options); - tl.debug(`Cleanup (RemoveAzContext.ps1) exit code: ${cleanupExitCode}`); } catch (err) { tl.debug("Az-clearContext not completed due to an error"); From a60c59b50bb97c0fa5787832ee40650d5833bc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 01:25:24 +0200 Subject: [PATCH 6/8] added logs --- Tasks/AzurePowerShellV5/azurepowershell.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 72f7fc1f1ea..6fdbeab492c 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -180,6 +180,7 @@ async function run() { tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed'); } finally { + let cleanupExitCode = 0; try { const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true)) .arg('-NoLogo') @@ -197,13 +198,16 @@ async function run() { outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR. ignoreReturnCode: true }; - await powershell.exec(options); + cleanupExitCode = await powershell.exec(options); + tl.debug(`Cleanup (RemoveAzContext.ps1) exit code: ${cleanupExitCode}`); } catch (err) { tl.debug("Az-clearContext not completed due to an error"); - //if (tl.getPipelineFeature('SetTaskResultFailed')) { - tl.setResult(tl.TaskResult.Failed, err.message); - //} + tl.setResult(tl.TaskResult.Failed, err.message); + } + // If cleanup failed and the main logic did not already fail, set task result to failed + if (cleanupExitCode !== 0 && tl.getTaskResult() !== tl.TaskResult.Failed) { + tl.setResult(tl.TaskResult.Failed, `Cleanup (RemoveAzContext.ps1) failed with exit code ${cleanupExitCode}`); } } } From 4ddd51d8789c960124b55e806c7319d7e02e6bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 01:47:27 +0200 Subject: [PATCH 7/8] changed condition --- Tasks/AzurePowerShellV5/azurepowershell.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 6fdbeab492c..767e90cd7e0 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -205,8 +205,7 @@ async function run() { tl.debug("Az-clearContext not completed due to an error"); tl.setResult(tl.TaskResult.Failed, err.message); } - // If cleanup failed and the main logic did not already fail, set task result to failed - if (cleanupExitCode !== 0 && tl.getTaskResult() !== tl.TaskResult.Failed) { + if (cleanupExitCode !== 0) { tl.setResult(tl.TaskResult.Failed, `Cleanup (RemoveAzContext.ps1) failed with exit code ${cleanupExitCode}`); } } From 63332c3d146c07366ca57babf4bb48eae0d3cc8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodora=20Pavlovi=C4=87?= Date: Mon, 13 Oct 2025 14:35:39 +0200 Subject: [PATCH 8/8] Uncommented the fix for the missing import error --- Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 | 2 +- Tasks/AzurePowerShellV5/RemoveAzContext.ps1 | 2 +- Tasks/AzurePowerShellV5/azurepowershell.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 b/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 index b301ff34986..7138fd25a42 100644 --- a/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 +++ b/Tasks/AzurePowerShellV5/ImportVstsTaskSdk.ps1 @@ -1 +1 @@ -#Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } +Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } diff --git a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 index b6f32ccfad7..9ff379016fc 100644 --- a/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 +++ b/Tasks/AzurePowerShellV5/RemoveAzContext.ps1 @@ -1,4 +1,4 @@ -#Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } +Import-Module "$PSScriptRoot\ps_modules\VstsTaskSdk" -ArgumentList @{ NonInteractive = $true } Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' diff --git a/Tasks/AzurePowerShellV5/azurepowershell.ts b/Tasks/AzurePowerShellV5/azurepowershell.ts index 767e90cd7e0..aa9e7d558c5 100644 --- a/Tasks/AzurePowerShellV5/azurepowershell.ts +++ b/Tasks/AzurePowerShellV5/azurepowershell.ts @@ -199,14 +199,14 @@ async function run() { ignoreReturnCode: true }; cleanupExitCode = await powershell.exec(options); - tl.debug(`Cleanup (RemoveAzContext.ps1) exit code: ${cleanupExitCode}`); + tl.debug(`Cleanup exit code: ${cleanupExitCode}`); } catch (err) { tl.debug("Az-clearContext not completed due to an error"); tl.setResult(tl.TaskResult.Failed, err.message); } if (cleanupExitCode !== 0) { - tl.setResult(tl.TaskResult.Failed, `Cleanup (RemoveAzContext.ps1) failed with exit code ${cleanupExitCode}`); + tl.setResult(tl.TaskResult.Failed, `Cleanup failed with exit code ${cleanupExitCode}`); } } }