Add DevOps logging support for invoke helper (#6752)

Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
This commit is contained in:
Azure SDK Bot 2025-09-25 15:49:08 -07:00 committed by GitHub
parent ad5bd689af
commit 1f7b40dee5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 12 deletions

View File

@ -1,4 +1,6 @@
function Invoke-LoggedCommand
. $PSScriptRoot/../logging.ps1
function Invoke-LoggedMsbuildCommand
{
[CmdletBinding()]
param
@ -8,12 +10,26 @@ function Invoke-LoggedCommand
[switch] $GroupOutput,
[int[]] $AllowedExitCodes = @(0)
)
return Invoke-LoggedCommand $Command -ExecutePath $ExecutePath -GroupOutput:$GroupOutput -AllowedExitCodes $AllowedExitCodes -OutputProcessor { param($line) ProcessMsBuildLogLine $line }
}
function Invoke-LoggedCommand
{
[CmdletBinding()]
param
(
[string] $Command,
[string] $ExecutePath,
[switch] $GroupOutput,
[int[]] $AllowedExitCodes = @(0),
[scriptblock] $OutputProcessor
)
$pipelineBuild = !!$env:TF_BUILD
$startTime = Get-Date
if($pipelineBuild -and $GroupOutput) {
Write-Host "##[group]$Command"
if($GroupOutput) {
LogGroupStart $Command
} else {
Write-Host "> $Command"
}
@ -22,22 +38,22 @@ function Invoke-LoggedCommand
Push-Location $ExecutePath
}
if (!$OutputProcessor) {
$OutputProcessor = { param($line) $line }
}
try {
Invoke-Expression $Command
Invoke-Expression $Command | Foreach-Object { & $OutputProcessor $_ }
$duration = (Get-Date) - $startTime
if($pipelineBuild -and $GroupOutput) {
Write-Host "##[endgroup]"
if($GroupOutput) {
LogGroupEnd
}
if($LastExitCode -notin $AllowedExitCodes)
{
if($pipelineBuild) {
Write-Error "##[error]Command failed to execute ($duration): $Command`n"
} else {
Write-Error "Command failed to execute ($duration): $Command`n"
}
LogError "Command failed to execute ($duration): $Command`n"
}
else {
Write-Host "Command succeeded ($duration)`n"

View File

@ -111,3 +111,15 @@ function LogJobFailure() {
}
# No equivalent for GitHub Actions. Failure is only determined by nonzero exit code.
}
function ProcessMsBuildLogLine($line) {
if (Test-SupportsDevOpsLogging) {
if ($line -like "*: warning*") {
return ("##vso[task.LogIssue type=warning;]$line" -replace "`n", "%0D%0A")
}
elseif ($line -like "*: error*") {
return ("##vso[task.LogIssue type=error;]$line" -replace "`n", "%0D%0A")
}
}
return $line
}