// Jenkins CI for WinDiagnostics // Static validation only: the diagnostic script itself targets Windows. // // Linux agent requirements: // - git // - curl // - tar // - sha256sum // - network access to GitHub and PowerShell Gallery // // PowerShell is downloaded as a portable archive into the workspace. // PSScriptAnalyzer is installed into a workspace-local module directory. pipeline { agent { label 'linux' } options { timestamps() ansiColor('xterm') disableConcurrentBuilds() } environment { POWERSHELL_TELEMETRY_OPTOUT = '1' // Pin the CI runtime for reproducible builds. PWSH_VERSION = '7.6.6' PWSH_HOME = "${WORKSPACE}/.tools/powershell-7.6.6" PWSH = "${WORKSPACE}/.tools/powershell-7.6.6/pwsh" // Keep installed PowerShell modules inside the Jenkins workspace. PS_MODULE_HOME = "${WORKSPACE}/.tools/psmodules" } stages { stage('Bootstrap PowerShell') { steps { sh ''' set -eu mkdir -p "$WORKSPACE/.tools" ARCH="$(uname -m)" case "$ARCH" in x86_64|amd64) PWSH_ARCH="x64" PWSH_SHA256="DDBC4A2D113BBD46D283CFEDCBCD117A70CAEFD7673F41F2B4E0000BADF103BC" ;; aarch64|arm64) PWSH_ARCH="arm64" PWSH_SHA256="924829E54C983648F6F1419A2DC7F9433C861B2FB5BD57736FF096C24F133729" ;; *) echo "Unsupported Linux architecture: $ARCH" >&2 exit 1 ;; esac PACKAGE="powershell-${PWSH_VERSION}-linux-${PWSH_ARCH}.tar.gz" URL="https://github.com/PowerShell/PowerShell/releases/download/v${PWSH_VERSION}/${PACKAGE}" ARCHIVE="$WORKSPACE/.tools/$PACKAGE" if [ -x "$PWSH" ]; then CURRENT_VERSION="$($PWSH -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')" if [ "$CURRENT_VERSION" = "$PWSH_VERSION" ]; then echo "Using cached PowerShell $CURRENT_VERSION" "$PWSH" --version exit 0 fi echo "Cached PowerShell version is $CURRENT_VERSION; reinstalling $PWSH_VERSION." rm -rf "$PWSH_HOME" fi echo "Downloading PowerShell $PWSH_VERSION for $PWSH_ARCH..." curl \ --fail \ --location \ --retry 3 \ --retry-delay 2 \ --output "$ARCHIVE" \ "$URL" echo "Verifying SHA-256..." printf '%s %s\n' "$PWSH_SHA256" "$ARCHIVE" | sha256sum --check - rm -rf "$PWSH_HOME" mkdir -p "$PWSH_HOME" tar -xzf "$ARCHIVE" -C "$PWSH_HOME" chmod 755 "$PWSH" rm -f "$ARCHIVE" echo "PowerShell installed locally:" "$PWSH" --version ''' } } stage('Environment') { steps { sh ''' set -eu echo "PowerShell:" "$PWSH" --version echo "Repository:" git --version git status --short echo "Architecture:" uname -m ''' } } 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 mkdir -p "$PS_MODULE_HOME" "$PWSH" -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" $moduleHome = $env:PS_MODULE_HOME $separator = [IO.Path]::PathSeparator $env:PSModulePath = $moduleHome + $separator + $env:PSModulePath if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { Write-Host "Installing PSScriptAnalyzer into $moduleHome..." Save-Module PSScriptAnalyzer ` -Path $moduleHome ` -Repository PSGallery ` -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.' } } }