Sync eng/common directory with azure-sdk-tools for PR 12532 (#6800)

* Change logging functions to use Write-Host

Change logging helpers to always write to host and either set color or use devops/gh formatting.

We do not want to use Write-Error or Write-Warning directly because they can stop the script or not depending on preferences which makes it difficult to ensure local runs of scripts work the same as in the pipelines. So, we should never depend on these logging commands to cause a script to stop execution.

* Clean up error handling in CommandInvocation-Helpers

Removed legacy error handling for command failures.

* Fix error logging for command execution

* Add option to skip exiting

---------

Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com>
Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
This commit is contained in:
Azure SDK Bot 2025-10-22 09:45:26 -07:00 committed by GitHub
parent d9dc195971
commit 7a3f332491
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 12 deletions

View File

@ -23,6 +23,7 @@ function Invoke-LoggedCommand
[string] $ExecutePath, [string] $ExecutePath,
[switch] $GroupOutput, [switch] $GroupOutput,
[int[]] $AllowedExitCodes = @(0), [int[]] $AllowedExitCodes = @(0),
[switch] $DoNotExitOnFailedExitCode,
[scriptblock] $OutputProcessor [scriptblock] $OutputProcessor
) )
@ -51,17 +52,11 @@ function Invoke-LoggedCommand
LogGroupEnd LogGroupEnd
} }
if($LastExitCode -notin $AllowedExitCodes) if($LASTEXITCODE -notin $AllowedExitCodes)
{ {
LogError "Command failed to execute ($duration): $Command`n" LogError "Command failed to execute ($duration): $Command`n"
if (!$DoNotExitOnFailedExitCode) {
# This fix reproduces behavior that existed before exit $LASTEXITCODE
# https://github.com/Azure/azure-sdk-tools/pull/12235
# Before that change, if a command failed Write-Error was always
# invoked in the failure case. Today, LogError only does Write-Error
# when running locally (not in a CI environment)
if ((Test-SupportsDevOpsLogging) -or (Test-SupportsGitHubLogging)) {
Write-Error "Command failed to execute ($duration): $Command`n"
} }
} }
else { else {

View File

@ -38,7 +38,7 @@ function LogWarning {
Write-Host ("::warning::$args" -replace "`n", "%0D%0A") Write-Host ("::warning::$args" -replace "`n", "%0D%0A")
} }
else { else {
Write-Warning "$args" Write-Host "$args" -ForegroundColor Yellow
} }
} }
@ -59,7 +59,7 @@ function LogErrorForFile($file, $errorString)
Write-Host ("::error file=$file,line=1,col=1::$errorString" -replace "`n", "%0D%0A") Write-Host ("::error file=$file,line=1,col=1::$errorString" -replace "`n", "%0D%0A")
} }
else { else {
Write-Error "[Error in file $file]$errorString" Write-Host "[Error in file $file]$errorString" -ForegroundColor Red
} }
} }
@ -71,7 +71,7 @@ function LogError {
Write-Host ("::error::$args" -replace "`n", "%0D%0A") Write-Host ("::error::$args" -replace "`n", "%0D%0A")
} }
else { else {
Write-Error "$args" Write-Host "$args" -ForegroundColor Red
} }
} }