ci: retry PSScriptAnalyzer per file when it throws #90

Merged
KofTwentyTwo merged 1 commits from ci/GH-89-analyzer-retry into main 2026-10-10 19:16:13 +00:00
+21 -4
View File
@@ -169,13 +169,30 @@ jobs:
shell: pwsh
run: |
Install-Module PSScriptAnalyzer -RequiredVersion 1.25.0 -Scope CurrentUser -Force
# One file per call: PSScriptAnalyzer 1.25.0 intermittently fails with
# "Object reference not set to an instance of an object" when it is
# handed a directory, and never when it is handed a single script.
# One file per call, three attempts each: PSScriptAnalyzer 1.25.0
# intermittently throws "Object reference not set to an instance of an
# object" from inside the analyzer (#89); a retry has always passed. A
# file that throws three times fails the gate with the analyzer's error.
$settings = (Resolve-Path ./PSScriptAnalyzerSettings.psd1).Path
$scripts = Get-ChildItem -Path .github/scripts, packaging/chocolatey/tools -Recurse -Include *.ps1, *.psm1, *.psd1
$findings = @()
foreach ($script in $scripts) { $findings += @(Invoke-ScriptAnalyzer -Path $script.FullName -Settings $settings) }
foreach ($script in $scripts)
{
for ($attempt = 1; $attempt -le 3; $attempt++)
{
try
{
$findings += @(Invoke-ScriptAnalyzer -Path $script.FullName -Settings $settings -ErrorAction Stop)
break
}
catch
{
if ($attempt -eq 3) { throw }
Write-Output "::warning file=$($script.FullName)::PSScriptAnalyzer threw on attempt $attempt ($($_.Exception.Message)); retrying."
Start-Sleep -Seconds 2
}
}
}
foreach ($f in $findings) { Write-Output "::error file=$($f.ScriptPath),line=$($f.Line)::$($f.RuleName): $($f.Message)" }
if ($findings.Count -gt 0) { exit 1 }
Write-Output 'PSScriptAnalyzer: no findings.'