Add Jenkins static validation pipeline

This commit is contained in:
Dandaedre 2026-10-16 13:51:20 +02:00
parent 73e8f01528
commit 99aaa17e69
2 changed files with 366 additions and 0 deletions

249
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,249 @@
// Add this webhook in Gitea:
// http://192.168.1.37:8080/gitea-webhook/post
pipeline {
agent { label 'rust_no_docker' }
options {
// We handle checkout explicitly below.
skipDefaultCheckout(true)
}
environment {
PYTHONUNBUFFERED = '1'
}
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 "Python:"
python3 --version
echo
echo "Repository:"
git status --short
'''
}
}
stage('Validate CI Tools') {
steps {
sh '''
set -eu
if ! command -v python3 >/dev/null 2>&1; then
echo "ERROR: python3 is required for Config Central CI." >&2
exit 1
fi
if [ ! -f tools/check_powershell.py ]; then
echo "ERROR: tools/check_powershell.py is missing." >&2
exit 1
fi
echo "Python validator found."
'''
}
}
stage('Validate PowerShell Sources') {
steps {
sh '''
set -eu
echo "Running lightweight PowerShell integrity checks..."
echo
python3 tools/check_powershell.py
'''
}
}
stage('Validate JSON') {
steps {
sh '''
set -eu
echo "Validating JSON files..."
echo
python3 - <<'PY'
import json
import pathlib
paths = sorted(pathlib.Path('.').glob('schemas/*.json'))
paths += sorted(pathlib.Path('.').glob('profiles/*.json'))
paths += sorted(pathlib.Path('.').glob('machines/*.json'))
if not paths:
raise SystemExit('ERROR: no JSON contracts or manifests found.')
for path in paths:
with path.open(encoding='utf-8') as stream:
json.load(stream)
print(f'OK: {path}')
PY
'''
}
}
stage('Repository Sanity') {
steps {
sh '''
set -eu
echo "Tracked PowerShell files:"
echo
ps_files="$(git ls-files -- '*.ps1' '*.psm1' '*.psd1')"
if [ -z "${ps_files}" ]; then
echo "ERROR: no tracked PowerShell files found." >&2
exit 1
fi
echo "${ps_files}"
for required in bootstrap.ps1 cc.ps1 schemas/profile.schema.json schemas/machine.schema.json; do
if [ ! -s "${required}" ]; then
echo "ERROR: required file is missing or empty: ${required}" >&2
exit 1
fi
done
echo
echo "Repository sanity checks passed."
'''
}
}
stage('Check Repository Hygiene') {
steps {
sh '''
set -eu
echo "Checking repository hygiene..."
echo
failed=0
if git grep -n -e '^<<<<<<< ' -e '^=======$' -e '^>>>>>>> ' -- '*.ps1' '*.psm1' '*.md' '*.json'; then
echo >&2
echo "ERROR: unresolved merge conflict marker found." >&2
failed=1
else
echo "No merge conflict markers found."
fi
suspicious="$(git ls-files | grep -E '\.(tmp|bak|old|orig|rej)$' || true)"
if [ -n "${suspicious}" ]; then
echo >&2
echo "WARNING: suspicious tracked backup/temp files:"
echo "${suspicious}"
else
echo "No obvious backup/temp files tracked."
fi
if [ "${failed}" -ne 0 ]; then
exit 1
fi
echo
echo "Repository hygiene checks passed."
'''
}
}
stage('Summary') {
steps {
sh '''
set -eu
echo
echo "========================================"
echo " Config Central CI validation complete"
echo "========================================"
echo
echo "Checks performed:"
echo " - Git checkout"
echo " - Python availability"
echo " - PowerShell structural integrity"
echo " - JSON schemas and manifests"
echo " - Required project files"
echo " - Merge conflict detection"
echo " - Repository hygiene"
echo
echo "NOTE:"
echo " PowerShell/CoreCLR cannot currently run"
echo " on this Jenkins FreeBSD worker."
echo
echo " CI therefore performs static checks here."
echo " Pester and real WinGet/elevation tests must"
echo " run on a disposable Windows worker."
'''
}
}
}
post {
success {
publishChecks(
conclusion: 'SUCCESS',
name: 'Config Central CI',
summary: 'Config Central passed all CI integrity checks.',
text: 'Static PowerShell validation, JSON contract checks, and repository hygiene checks completed successfully. Windows runtime tests are intentionally separate.',
title: 'Config Central validation successful'
)
echo '✅ Config Central integrity checks passed.'
}
failure {
publishChecks(
conclusion: 'FAILURE',
name: 'Config Central CI',
summary: 'Config Central failed CI.',
text: 'A checkout, PowerShell structural, JSON contract, or repository hygiene check failed. Check the Jenkins log.',
title: 'Config Central CI failed'
)
echo '❌ Config Central integrity checks failed.'
}
unstable {
publishChecks(
conclusion: 'ACTION_REQUIRED',
name: 'Config Central CI',
summary: 'Config Central CI is unstable.',
text: 'The Jenkins build completed with an unstable status.',
title: 'Config Central CI unstable'
)
echo '⚠️ Config Central CI is unstable.'
}
always {
echo 'CI finished.'
}
}
}

117
tools/check_powershell.py Normal file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env python3
"""Dependency-free structural checks for PowerShell on non-PowerShell CI workers."""
from __future__ import annotations
import json
import pathlib
import re
import sys
ROOT = pathlib.Path(__file__).resolve().parents[1]
POWERSHELL = sorted((*ROOT.glob("*.ps1"), *ROOT.glob("src/*.ps1"), *ROOT.glob("src/*.psm1")))
def check_balanced(path: pathlib.Path) -> list[str]:
text = path.read_text(encoding="utf-8-sig")
errors: list[str] = []
stack: list[tuple[str, int]] = []
pairs = {"(": ")", "[": "]", "{": "}"}
closing = set(pairs.values())
quote: str | None = None
escaped = False
line = 1
index = 0
while index < len(text):
char = text[index]
if char == "\n":
line += 1
if quote is None:
escaped = False
index += 1
continue
if quote:
if quote == '"' and char == "`" and not escaped:
escaped = True
index += 1
continue
if char == quote and not escaped:
if quote == "'" and index + 1 < len(text) and text[index + 1] == "'":
index += 2
continue
quote = None
escaped = False
index += 1
continue
if char == "#":
newline = text.find("\n", index)
index = len(text) if newline < 0 else newline
continue
if char in ("'", '"'):
quote = char
elif char in pairs:
stack.append((char, line))
elif char in closing:
if not stack or pairs[stack[-1][0]] != char:
errors.append(f"{path.relative_to(ROOT)}:{line}: unexpected {char}")
else:
stack.pop()
index += 1
if quote:
errors.append(f"{path.relative_to(ROOT)}:{line}: unterminated string")
for opener, opener_line in stack:
errors.append(f"{path.relative_to(ROOT)}:{opener_line}: unclosed {opener}")
return errors
def main() -> int:
errors: list[str] = []
if not POWERSHELL:
errors.append("No PowerShell source files found.")
for path in POWERSHELL:
errors.extend(check_balanced(path))
source = path.read_text(encoding="utf-8-sig")
if path.name != "bootstrap.ps1" and "Set-StrictMode" not in source:
errors.append(f"{path.relative_to(ROOT)}: missing Set-StrictMode")
for relative in ("schemas/profile.schema.json", "schemas/machine.schema.json"):
path = ROOT / relative
try:
document = json.loads(path.read_text(encoding="utf-8"))
if document.get("$schema") != "https://json-schema.org/draft/2020-12/schema":
errors.append(f"{relative}: unexpected JSON Schema dialect")
except (OSError, json.JSONDecodeError) as exc:
errors.append(f"{relative}: {exc}")
for path in sorted((*ROOT.glob("profiles/*.json"), *ROOT.glob("machines/*.json"))):
try:
document = json.loads(path.read_text(encoding="utf-8"))
if document.get("schemaVersion") != 1:
errors.append(f"{path.relative_to(ROOT)}: schemaVersion must be 1")
except json.JSONDecodeError as exc:
errors.append(f"{path.relative_to(ROOT)}: {exc}")
required_cli_markers = ("doctor", "plan", "apply", "sync", "status", "restore", "menu")
cli = (ROOT / "cc.ps1").read_text(encoding="utf-8-sig")
for marker in required_cli_markers:
if not re.search(rf"['\"]{re.escape(marker)}['\"]", cli):
errors.append(f"cc.ps1: missing command marker {marker}")
forbidden = ("DisableAntiSpyware", "Set-MpPreference -DisableRealtimeMonitoring", "--ignore-security-hash")
for path in POWERSHELL:
source = path.read_text(encoding="utf-8-sig")
for marker in forbidden:
if marker.lower() in source.lower():
errors.append(f"{path.relative_to(ROOT)}: forbidden security bypass marker {marker}")
if errors:
print("PowerShell structural validation failed:", file=sys.stderr)
for error in errors:
print(f" ERROR: {error}", file=sys.stderr)
return 1
print(f"Validated {len(POWERSHELL)} PowerShell files, JSON schemas, manifests, and CLI markers.")
return 0
if __name__ == "__main__":
raise SystemExit(main())