// Jenkins CI for WinDiagnostics // Static validation only: the diagnostic script itself targets Windows. // // Linux agent requirements: // - git // - PowerShell 7.2.11+ (`pwsh`) // - network access to PowerShell Gallery on first run // // PSScriptAnalyzer is installed into the Jenkins user's CurrentUser scope // when absent, so no root privileges are required. pipeline { agent { label 'linux' } options { timestamps() ansiColor('xterm') disableConcurrentBuilds() } environment { POWERSHELL_TELEMETRY_OPTOUT = '1' } stages { stage('Environment') { steps { sh ''' set -eu echo "PowerShell:" command -v pwsh pwsh --version echo "Repository:" git --version git status --short ''' } } stage('PowerShell Syntax') { steps { sh ''' set -eu pwsh -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $files = @(Get-ChildItem -Path . -Recurse -File | Where-Object { $_.Extension -in ".ps1", ".psm1", ".psd1" }) if ($files.Count -eq 0) { throw "No PowerShell files found." } $failed = $false foreach ($file in $files) { Write-Host "Parsing $($file.FullName)" $tokens = $null $errors = $null [void][System.Management.Automation.Language.Parser]::ParseFile( $file.FullName, [ref]$tokens, [ref]$errors ) if ($errors.Count -gt 0) { $failed = $true foreach ($parseError in $errors) { Write-Error ( "{0}:{1}:{2}: {3}" -f $file.FullName, $parseError.Extent.StartLineNumber, $parseError.Extent.StartColumnNumber, $parseError.Message ) } } } if ($failed) { throw "PowerShell parser validation failed." } Write-Host "All PowerShell files parsed successfully." ' ''' } } stage('PSScriptAnalyzer') { steps { sh ''' set -eu pwsh -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { Write-Host "Installing PSScriptAnalyzer..." Set-PSRepository PSGallery -InstallationPolicy Trusted Install-Module PSScriptAnalyzer ` -Scope CurrentUser ` -Force ` -ErrorAction Stop } Import-Module PSScriptAnalyzer -ErrorAction Stop $module = Get-Module PSScriptAnalyzer Write-Host "PSScriptAnalyzer version: $($module.Version)" $issues = @( Invoke-ScriptAnalyzer ` -Path . ` -Recurse ` -Severity Error,Warning ) if ($issues.Count -gt 0) { $issues | Sort-Object ScriptName, Line, Column | Format-Table ` Severity, ScriptName, Line, Column, RuleName, Message ` -AutoSize | Out-String | Write-Host } $errors = @( $issues | Where-Object Severity -eq "Error" ) $warnings = @( $issues | Where-Object Severity -eq "Warning" ) Write-Host ( "PSScriptAnalyzer: {0} error(s), {1} warning(s)." -f $errors.Count, $warnings.Count ) // Warnings are reported but do not fail CI. // Static analyzer errors do fail the build. if ($errors.Count -gt 0) { throw "PSScriptAnalyzer reported errors." } ' ''' } } stage('JSON Contract Sanity') { steps { sh ''' set -eu pwsh -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $script = Get-Content ` -LiteralPath "./WindowsMaintenanceReport.ps1" ` -Raw $requiredMarkers = @( "ConvertTo-Json", "schema_version", "report_type", "generated_at", "completed_at", "sections" ) foreach ($marker in $requiredMarkers) { if (-not $script.Contains($marker)) { throw ( "Expected structured-report marker missing: " + $marker ) } } Write-Host "Structured JSON report contract markers found." ' ''' } } } post { success { echo 'PowerShell syntax and static analysis passed.' } failure { echo 'WinDiagnostics integrity checks failed.' } } }