WinDiagnostics/Jenkinsfile

455 lines
16 KiB
Groovy

// Add this webhook in Gitea:
// http://192.168.1.37:8080/gitea-webhook/post
pipeline {
agent { label 'rust_no_docker' }
environment {
POWERSHELL_TELEMETRY_OPTOUT = '1'
// Keep PowerShell modules inside the Jenkins workspace.
PS_MODULE_HOME = "${WORKSPACE}/.tools/psmodules"
}
stages {
stage('Checkout') {
steps {
checkout scm
sh '''
set -eu
git submodule sync --recursive
git submodule update --init --recursive
'''
}
}
stage('Environment') {
steps {
sh '''
set -eu
echo "System:"
uname -a
echo
echo "Architecture:"
uname -m
echo
echo "Git:"
git --version
echo
echo "Repository:"
git status --short
'''
}
}
stage('Setup PowerShell') {
steps {
sh '''
set -eu
# ---------------------------------------------------------
# PowerShell
# ---------------------------------------------------------
#
# WinDiagnostics itself targets Windows, but PowerShell
# Core is used here only for static parsing and linting.
#
# On this FreeBSD worker, pwsh should normally be provided
# by the existing worker environment.
# ---------------------------------------------------------
if command -v pwsh >/dev/null 2>&1; then
echo "PowerShell already installed."
else
echo "ERROR: pwsh was not found on this Jenkins worker." >&2
echo >&2
echo "Install PowerShell on the rust_no_docker worker:" >&2
echo " pkg install powershell" >&2
exit 1
fi
echo
echo "Using:"
pwsh --version
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('PowerShell Syntax') {
steps {
sh '''
set -eu
echo "Checking PowerShell syntax..."
echo
pwsh \
-NoLogo \
-NoProfile \
-NonInteractive \
-Command '
$ErrorActionPreference = "Stop"
$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 ""
Write-Host "All PowerShell files parsed successfully."
'
'''
}
}
stage('Setup 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
}
else {
Write-Host (
"PSScriptAnalyzer already installed."
)
}
Import-Module \
PSScriptAnalyzer \
-ErrorAction Stop
$module = Get-Module PSScriptAnalyzer
Write-Host (
"PSScriptAnalyzer version: " +
"$($module.Version)"
)
'
'''
}
}
stage('PSScriptAnalyzer') {
steps {
sh '''
set -eu
echo "Running PSScriptAnalyzer..."
echo
pwsh \
-NoLogo \
-NoProfile \
-NonInteractive \
-Command '
$ErrorActionPreference = "Stop"
$separator = [IO.Path]::PathSeparator
$env:PSModulePath =
$env:PS_MODULE_HOME +
$separator +
$env:PSModulePath
Import-Module \
PSScriptAnalyzer \
-ErrorAction Stop
$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 informative.
# Analyzer errors fail CI.
if ($errors.Count -gt 0) {
throw (
"PSScriptAnalyzer reported errors."
)
}
'
'''
}
}
stage('Validate JSON Contract') {
steps {
sh '''
set -eu
echo "Checking WinDiagnostics JSON report contract..."
echo
if [ ! -f WindowsMaintenanceReport.ps1 ]; then
echo "ERROR: WindowsMaintenanceReport.ps1 is missing." >&2
exit 1
fi
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 "Found: $marker"
}
Write-Host ""
Write-Host (
"Structured JSON report contract looks valid."
)
'
'''
}
}
stage('Repository Sanity') {
steps {
sh '''
set -eu
echo "Tracked PowerShell files:"
git ls-files -- '*.ps1' '*.psm1' '*.psd1'
echo
echo "CI validation complete."
'''
}
}
}
post {
success {
publishChecks(
conclusion: 'SUCCESS',
name: 'WinDiagnostics CI',
summary: 'WinDiagnostics passed all CI integrity checks.',
text: 'PowerShell parsing, PSScriptAnalyzer, and JSON report contract validation completed successfully.',
title: 'WinDiagnostics validation successful'
)
echo '✅ WinDiagnostics integrity checks passed.'
}
failure {
publishChecks(
conclusion: 'FAILURE',
name: 'WinDiagnostics CI',
summary: 'WinDiagnostics failed CI.',
text: 'A checkout, PowerShell parsing, static analysis, or JSON contract validation step failed. Check the Jenkins log.',
title: 'WinDiagnostics CI failed'
)
echo '❌ WinDiagnostics integrity checks failed.'
}
unstable {
publishChecks(
conclusion: 'ACTION_REQUIRED',
name: 'WinDiagnostics CI',
summary: 'WinDiagnostics CI is unstable.',
text: 'The Jenkins build completed with an unstable status.',
title: 'WinDiagnostics CI unstable'
)
echo '⚠️ WinDiagnostics CI is unstable.'
}
always {
echo 'CI finished.'
}
}
}