From 1f7b40dee51a308f20ad6c3c5a8dece33a03e086 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 25 Sep 2025 15:49:08 -0700 Subject: [PATCH] Add DevOps logging support for invoke helper (#6752) Co-authored-by: Wes Haggard --- .../Helpers/CommandInvocation-Helpers.ps1 | 40 +++++++++++++------ eng/common/scripts/logging.ps1 | 12 ++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 index 0b9f810b8..9475164a5 100644 --- a/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 +++ b/eng/common/scripts/Helpers/CommandInvocation-Helpers.ps1 @@ -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" diff --git a/eng/common/scripts/logging.ps1 b/eng/common/scripts/logging.ps1 index 94dc900db..c27bfee71 100644 --- a/eng/common/scripts/logging.ps1 +++ b/eng/common/scripts/logging.ps1 @@ -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 +}