267 lines
9.6 KiB
Groovy
267 lines
9.6 KiB
Groovy
// Jenkins CI for WinDiagnostics
|
|
// Static validation only: the diagnostic script itself targets Windows.
|
|
//
|
|
// FreeBSD agent requirements: git and the shells/powershell package.
|
|
// Install once as root: pkg install git powershell
|
|
// Label the Jenkins node "freebsd". See README.md for setup details.
|
|
// PSScriptAnalyzer is saved into a workspace-local module directory.
|
|
|
|
pipeline {
|
|
agent {
|
|
label 'rust_no_docker'
|
|
}
|
|
|
|
options {
|
|
timestamps()
|
|
disableConcurrentBuilds()
|
|
}
|
|
|
|
environment {
|
|
POWERSHELL_TELEMETRY_OPTOUT = '1'
|
|
|
|
// FreeBSD packages install executables under /usr/local/bin.
|
|
PATH = "/usr/local/bin:/usr/local/sbin:${env.PATH}"
|
|
PWSH = 'pwsh'
|
|
|
|
// Keep installed PowerShell modules inside the Jenkins workspace.
|
|
PS_MODULE_HOME = "${WORKSPACE}/.tools/psmodules"
|
|
}
|
|
|
|
stages {
|
|
stage('Check FreeBSD prerequisites') {
|
|
steps {
|
|
sh '''
|
|
set -eu
|
|
|
|
if [ "$(uname -s)" != "FreeBSD" ]; then
|
|
echo "This pipeline requires a FreeBSD agent labeled freebsd." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v git >/dev/null 2>&1 || ! command -v "$PWSH" >/dev/null 2>&1; then
|
|
echo "Missing git or pwsh. On the agent, install as root: pkg install git powershell" >&2
|
|
exit 1
|
|
fi
|
|
|
|
"$PWSH" -NoLogo -NoProfile -NonInteractive -Command '
|
|
$ErrorActionPreference = "Stop"
|
|
if ($PSVersionTable.PSVersion.Major -lt 7) {
|
|
throw "PowerShell 7 or later is required."
|
|
}
|
|
$PSVersionTable | Out-String | Write-Host
|
|
'
|
|
'''
|
|
}
|
|
}
|
|
|
|
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"
|
|
|
|
# Limit validation to source files, excluding workspace caches.
|
|
$paths = @(git -c core.quotePath=false ls-files -- "*.ps1" "*.psm1" "*.psd1")
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Unable to list tracked PowerShell files."
|
|
}
|
|
$files = @($paths | ForEach-Object { Get-Item -LiteralPath $_ })
|
|
|
|
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-Host (
|
|
"{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)"
|
|
|
|
# Limit validation to source files, excluding workspace caches.
|
|
$paths = @(git -c core.quotePath=false ls-files -- "*.ps1" "*.psm1" "*.psd1")
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Unable to list tracked PowerShell files."
|
|
}
|
|
$files = @($paths | ForEach-Object { Get-Item -LiteralPath $_ })
|
|
|
|
if ($files.Count -eq 0) {
|
|
throw "No PowerShell files found."
|
|
}
|
|
|
|
$issues = @(
|
|
foreach ($file in $files) {
|
|
Invoke-ScriptAnalyzer `
|
|
-Path $file.FullName `
|
|
-Severity Error,Warning `
|
|
-ErrorAction Stop
|
|
}
|
|
)
|
|
|
|
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.'
|
|
}
|
|
}
|
|
} |