为什么我的 VSTS 自定义任务通过退出代码 1?
Why does my VSTS custom task passes with exit code 1?
我遇到了以下烦人的问题。我的自定义 VSTS 构建任务不会失败。
它总是通过,而 $LASTEXITCODE 不为零。
代码按预期运行。它会在日志中生成错误。尽管如此,步骤还是成功了,构建/发布继续。
截图:
我已经包含了一个带有退出代码的写入主机,它也显示了退出代码 1。
代码:
Try {
....
#Loop through the server list
Foreach ($Server in $machines)
{
# Use SSL or not
If($UseSSL -eq $true)
{
Write-Host "Connecting to $Server using a SSL connection (TCP/5986), Skip CA Check: $CheckCA ..."
$s = New-PSSession -ComputerName $Server -Credential $Cred -UseSSL -SessionOption $SessionOptions
}
Else
{
Write-Host "Connecting to $Server with an unsecure connection (TCP/5985) ..."
$s = New-PSSession -ComputerName $Server -Credential $Cred
}
# Run
$ExitCode = Invoke-Command -Session $s -ScriptBlock $script -ArgumentList $ApplicationPoolName,$Action,$Killswitch
# Cleanup the session
Write-Host "Closing connection to $Server."
Remove-PSSession -Session $s
}
} Catch {
Write-Host "##vso[task.logissue type=Error;]$Error"
$ExitCode = 1
} Finally {
#Leave TFS/VSTS trace
if (Get-Command -Name Trace-VstsEnteringInvocation -ErrorAction SilentlyContinue) {
Trace-VstsLeavingInvocation $MyInvocation
}
write-host "ExitCode: $ExitCode"
Exit $ExitCode
}
我在这里错过了什么?
我通过删除 finally 部分解决了它。
不工作:
try {
.... do stuff ....
} catch {
write-error "some error"
exit 1
} Finally {
.. some final steps ...
}
工作:
try {
.... do stuff ....
} catch {
write-error "some error"
exit 1
}
对于到达这里的人,遇到与 BAT/CMD 文件相同的问题,我的解决方案是添加以下行:
exit /b %ERRORLEVEL%
强制将错误级别传输到 VSTS/TFS 命令行任务
我遇到了以下烦人的问题。我的自定义 VSTS 构建任务不会失败。 它总是通过,而 $LASTEXITCODE 不为零。
代码按预期运行。它会在日志中生成错误。尽管如此,步骤还是成功了,构建/发布继续。
截图:
代码:
Try {
....
#Loop through the server list
Foreach ($Server in $machines)
{
# Use SSL or not
If($UseSSL -eq $true)
{
Write-Host "Connecting to $Server using a SSL connection (TCP/5986), Skip CA Check: $CheckCA ..."
$s = New-PSSession -ComputerName $Server -Credential $Cred -UseSSL -SessionOption $SessionOptions
}
Else
{
Write-Host "Connecting to $Server with an unsecure connection (TCP/5985) ..."
$s = New-PSSession -ComputerName $Server -Credential $Cred
}
# Run
$ExitCode = Invoke-Command -Session $s -ScriptBlock $script -ArgumentList $ApplicationPoolName,$Action,$Killswitch
# Cleanup the session
Write-Host "Closing connection to $Server."
Remove-PSSession -Session $s
}
} Catch {
Write-Host "##vso[task.logissue type=Error;]$Error"
$ExitCode = 1
} Finally {
#Leave TFS/VSTS trace
if (Get-Command -Name Trace-VstsEnteringInvocation -ErrorAction SilentlyContinue) {
Trace-VstsLeavingInvocation $MyInvocation
}
write-host "ExitCode: $ExitCode"
Exit $ExitCode
}
我在这里错过了什么?
我通过删除 finally 部分解决了它。
不工作:
try {
.... do stuff ....
} catch {
write-error "some error"
exit 1
} Finally {
.. some final steps ...
}
工作:
try {
.... do stuff ....
} catch {
write-error "some error"
exit 1
}
对于到达这里的人,遇到与 BAT/CMD 文件相同的问题,我的解决方案是添加以下行:
exit /b %ERRORLEVEL%
强制将错误级别传输到 VSTS/TFS 命令行任务