gpt4 book ai didi

powershell - 即使使用非 0 退出代码,Azure DevOps 也会报告 PowerShell 任务已通过

转载 作者:行者123 更新时间:2023-12-03 23:04:27 35 4
gpt4 key购买 nike

在 Azure DevOps 中,我有一个运行 PowerShell 脚本的任务。在某些情况下,脚本应该以非 0 代码退出,导致任务报告为失败。但是,Azure DevOps 无论如何都会报告任务已通过。
这是一个问题,因为如果此作业失败,我有一个后续作业应该运行,但由于误报而不会发生这种情况。
enter image description here
脚本的相关部分显示,在截图的情况下,退出代码为1被检测到,这将导致脚本错误退出。

if ($exitCode -ne 0)
{
Write-Output ("[Error] Failing task since return code was {0} while expected 0." -f $exitCode)
}
exit $exitCode
该任务作为 deployment 的一部分运行工作,我确实可以选择 failOnStandardError设置为 true .
- task: AzurePowerShell@5
displayName: Check Function App Version
inputs:
azureSubscription: ${{ parameters.serviceConnectionName }}
scriptType: FilePath
scriptPath: ${{ parameters.scriptsArtefactPath }}/Test-FunctionAppVersion.ps1
scriptArguments: -Uri ${{ parameters.healthCheckUri }} -AuthHeaderName Authorization -AuthHeaderValue "$(healthCheckAuthHeaderValue)"
failOnStandardError: true
azurePowerShellVersion: LatestVersion
pwsh: true
如何让 Azure DevOps 遵守我的退出代码?

最佳答案

似乎 Azure PowerShell 任务在从脚本文件中捕获退出代码时出现问题,它适用于内联脚本。您可以 report this problem给微软开发团队。希望他们能尽快提供修复。
但是,您可以使用以下任一变通方法来修复它。
1、使用[Environment]::Exit($exitCode)而不是 exit .见下文:

if ($exitCode -ne 0)
{
Write-Output ("[Error] Failing task since return code was {0} while expected 0." -f $exitCode)
}
[Environment]::Exit($exitCode)
2、使用 logging command ##vso[task.complete result=Failed;]Failed如果 exitCode 为非 0,则手动使任务失败。见下文:
if ($exitCode -ne 0)
{
Write-Output ("[Error] Failing task since return code was {0} while expected 0." -f $exitCode)
Write-Host "##vso[task.complete result=Failed;]Failed"
}
exit $exitCode

关于powershell - 即使使用非 0 退出代码,Azure DevOps 也会报告 PowerShell 任务已通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63508364/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com