2026-04-10 03:25:23 -03:00
pipeline {
agent {
kubernetes {
defaultContainer 'go-tester'
yaml """
apiVersion: v1
kind: Pod
spec:
nodeSelector:
kubernetes.io/arch: arm64
node-role.kubernetes.io/worker: "true"
containers:
- name: go-tester
2026-04-21 13:36:03 -03:00
image: registry.bstein.dev/bstein/golang:1.22-bookworm
2026-04-10 03:25:23 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: node-tester
2026-04-21 13:36:03 -03:00
image: registry.bstein.dev/bstein/node:20-bookworm
2026-04-10 03:25:23 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: publisher
2026-04-21 13:17:49 -03:00
image: registry.bstein.dev/bstein/python:3.12-slim
2026-04-10 03:25:23 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
volumes:
- name: workspace-volume
emptyDir: {}
"""
}
}
environment {
SUITE_NAME = 'pegasus'
PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091'
2026-04-19 21:29:40 -03:00
SONARQUBE_HOST_URL = 'http://sonarqube.quality.svc.cluster.local:9000'
SONARQUBE_PROJECT_KEY = 'pegasus'
QUALITY_GATE_SONARQUBE_ENFORCE = '0'
2026-04-19 14:12:08 -03:00
QUALITY_GATE_SONARQUBE_REPORT = 'build/sonarqube-quality-gate.json'
2026-04-19 21:29:40 -03:00
QUALITY_GATE_IRONBANK_ENFORCE = '0'
2026-04-19 21:16:15 -03:00
QUALITY_GATE_IRONBANK_REQUIRED = '0'
2026-04-19 14:12:08 -03:00
QUALITY_GATE_IRONBANK_REPORT = 'build/ironbank-compliance.json'
2026-04-10 03:25:23 -03:00
}
options {
disableConcurrentBuilds()
2026-04-20 21:59:42 -03:00
buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '200', artifactDaysToKeepStr: '30', artifactNumToKeepStr: '120'))
2026-04-10 03:25:23 -03:00
}
triggers {
pollSCM('H/5 * * * *')
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
2026-04-19 14:12:08 -03:00
stage('Collect SonarQube evidence') {
steps {
container('publisher') {
sh '''
set -eu
mkdir -p build
python3 - <<'PY'
import base64
import json
import os
import urllib.parse
import urllib.request
host = os.getenv('SONARQUBE_HOST_URL', '').strip().rstrip('/')
project_key = os.getenv('SONARQUBE_PROJECT_KEY', '').strip()
token = os.getenv('SONARQUBE_TOKEN', '').strip()
report_path = os.getenv('QUALITY_GATE_SONARQUBE_REPORT', 'build/sonarqube-quality-gate.json')
payload = {"status": "ERROR", "note": "missing SONARQUBE_HOST_URL and/or SONARQUBE_PROJECT_KEY"}
if host and project_key:
query = urllib.parse.urlencode({"projectKey": project_key})
request = urllib.request.Request(f"{host}/api/qualitygates/project_status?{query}", method="GET")
if token:
encoded = base64.b64encode(f"{token}:".encode("utf-8")).decode("utf-8")
request.add_header("Authorization", f"Basic {encoded}")
try:
with urllib.request.urlopen(request, timeout=12) as response:
payload = json.loads(response.read().decode("utf-8"))
except Exception as exc: # noqa: BLE001
payload = {"status": "ERROR", "error": str(exc)}
with open(report_path, "w", encoding="utf-8") as handle:
json.dump(payload, handle, indent=2, sort_keys=True)
handle.write("\\n")
PY
'''
}
}
}
stage('Collect Supply Chain evidence') {
steps {
container('publisher') {
sh '''
set -eu
mkdir -p build
python3 - <<'PY'
import json
import os
from pathlib import Path
report_path = Path(os.getenv('QUALITY_GATE_IRONBANK_REPORT', 'build/ironbank-compliance.json'))
if report_path.exists():
raise SystemExit(0)
status = os.getenv('IRONBANK_COMPLIANCE_STATUS', '').strip()
compliant = os.getenv('IRONBANK_COMPLIANT', '').strip().lower()
payload = {"status": status or "unknown", "compliant": compliant in {"1", "true", "yes", "on"} if compliant else None}
payload = {k: v for k, v in payload.items() if v is not None}
if "status" not in payload:
payload["status"] = "unknown"
payload["note"] = "Set IRONBANK_COMPLIANCE_STATUS/IRONBANK_COMPLIANT or write build/ironbank-compliance.json in image-building repos."
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\\n", encoding="utf-8")
PY
'''
}
}
}
2026-04-10 03:25:23 -03:00
stage('Backend unit tests') {
steps {
container('go-tester') {
sh '''
2026-04-10 03:38:19 -03:00
set -eu
2026-04-11 00:02:59 -03:00
export PEGASUS_SESSION_KEY=test-session-key
export PEGASUS_COOKIE_INSECURE=1
2026-04-10 03:25:23 -03:00
mkdir -p build
cd backend
2026-04-21 11:56:46 -03:00
export GOPROXY="${GOPROXY:-https://proxy.golang.org,direct}"
retry_command() {
attempts=4
delay=8
attempt=1
while [ "${attempt}" -le "${attempts}" ]; do
"$@"
rc=$?
if [ "${rc}" -eq 0 ]; then
return 0
fi
if [ "${attempt}" -eq "${attempts}" ]; then
return "${rc}"
fi
echo "command failed with rc=${rc}; retrying in ${delay}s (${attempt}/${attempts})"
sleep "${delay}"
delay=$((delay * 2))
attempt=$((attempt + 1))
done
}
2026-04-10 03:25:23 -03:00
set +e
2026-04-21 11:56:46 -03:00
retry_command go install github.com/jstemmer/go-junit-report/v2@latest
tool_rc=$?
if [ "${tool_rc}" -eq 0 ]; then
retry_command go test -coverprofile=../build/coverage-backend.out ./... > ../build/backend-test.out 2>&1
test_rc=$?
else
test_rc=1
printf 'go-junit-report install failed with rc=%s; skipping backend go test so metrics can publish\\n' "${tool_rc}" > ../build/backend-test.out
fi
2026-04-10 03:25:23 -03:00
set -e
cat ../build/backend-test.out
2026-04-21 11:56:46 -03:00
if [ "${tool_rc}" -eq 0 ] && [ -x "$(go env GOPATH)/bin/go-junit-report" ]; then
"$(go env GOPATH)/bin/go-junit-report" < ../build/backend-test.out > ../build/junit-backend.xml
else
cat > ../build/junit-backend.xml <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" failures="0" errors="0" skipped="0"></testsuites>
EOF
fi
2026-04-10 03:25:23 -03:00
coverage="0"
if [ -f ../build/coverage-backend.out ]; then
coverage="$(go tool cover -func=../build/coverage-backend.out | awk '/^total:/ {gsub("%","",$3); print $3}')"
fi
printf '%s\n' "${coverage}" > ../build/coverage-backend-percent.txt
printf '%s\n' "${test_rc}" > ../build/backend-test.rc
'''
}
}
}
stage('Frontend unit tests') {
steps {
container('node-tester') {
sh '''
2026-04-10 03:38:19 -03:00
set -eu
2026-04-10 03:25:23 -03:00
mkdir -p build
cd frontend
2026-04-21 11:56:46 -03:00
retry_command() {
attempts=4
delay=8
attempt=1
while [ "${attempt}" -le "${attempts}" ]; do
"$@"
rc=$?
if [ "${rc}" -eq 0 ]; then
return 0
fi
if [ "${attempt}" -eq "${attempts}" ]; then
return "${rc}"
fi
echo "command failed with rc=${rc}; retrying in ${delay}s (${attempt}/${attempts})"
sleep "${delay}"
delay=$((delay * 2))
attempt=$((attempt + 1))
done
}
2026-04-10 03:25:23 -03:00
set +e
2026-04-21 11:56:46 -03:00
retry_command npm ci
npm_ci_rc=$?
if [ "${npm_ci_rc}" -eq 0 ]; then
retry_command npm run test:ci > ../build/frontend-test.out 2>&1
test_rc=$?
else
test_rc=1
printf 'npm ci failed with rc=%s; skipping frontend tests so metrics can publish\\n' "${npm_ci_rc}" > ../build/frontend-test.out
fi
2026-04-10 03:25:23 -03:00
set -e
cat ../build/frontend-test.out
2026-04-21 11:56:46 -03:00
if [ ! -f ../build/junit-frontend.xml ]; then
cat > ../build/junit-frontend.xml <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" failures="0" errors="0" skipped="0"></testsuites>
EOF
fi
2026-04-10 03:25:23 -03:00
if [ -f ../build/frontend-coverage/coverage-summary.json ]; then
node -e 'const fs=require("fs");const p=JSON.parse(fs.readFileSync("../build/frontend-coverage/coverage-summary.json","utf8"));const pct=((p.total||{}).lines||{}).pct||0;process.stdout.write(String(pct));' > ../build/coverage-frontend-percent.txt
else
echo "0" > ../build/coverage-frontend-percent.txt
fi
printf '%s\n' "${test_rc}" > ../build/frontend-test.rc
'''
}
}
}
2026-04-19 14:12:08 -03:00
stage('Run quality gate') {
2026-04-10 03:25:23 -03:00
steps {
container('publisher') {
sh '''
2026-04-10 03:38:19 -03:00
set -eu
2026-04-21 11:56:46 -03:00
mkdir -p build
set +e
2026-04-19 14:40:37 -03:00
apt-get update
2026-04-21 11:56:46 -03:00
apt_rc=$?
if [ "${apt_rc}" -eq 0 ]; then
apt-get install -y --no-install-recommends golang-go nodejs npm
apt_rc=$?
fi
if [ "${apt_rc}" -eq 0 ]; then
python -m testing.pegasus_gate report
gate_rc=$?
else
gate_rc="${apt_rc}"
fi
set -e
if [ ! -f build/gate-summary.json ]; then
python3 - <<'PY'
import json
from pathlib import Path
Path("build/gate-summary.json").write_text(
json.dumps(
{
"ok": False,
"issues": [
{
"check": "gate_glue",
"path": "Jenkinsfile",
"detail": "quality gate dependencies or report command failed before summary generation",
}
],
"file_count": 0,
"backend_coverage": {},
"frontend_coverage": {},
},
indent=2,
sort_keys=True,
)
+ "\\n",
encoding="utf-8",
)
PY
fi
printf '%s\n' "${gate_rc}" > build/quality-report.rc
2026-04-10 03:25:23 -03:00
'''
}
}
}
stage('Publish test metrics') {
steps {
container('publisher') {
sh '''
2026-04-10 03:38:19 -03:00
set -eu
2026-04-10 03:25:23 -03:00
python scripts/publish_test_metrics.py
'''
}
}
}
2026-04-19 14:12:08 -03:00
stage('Enforce quality gate') {
2026-04-10 03:25:23 -03:00
steps {
container('publisher') {
sh '''
2026-04-21 11:56:46 -03:00
set -eu
2026-04-19 14:40:37 -03:00
apt-get update
2026-04-19 15:02:44 -03:00
apt-get install -y --no-install-recommends golang-go nodejs npm
2026-04-19 21:16:15 -03:00
set +e
2026-04-11 00:02:59 -03:00
python -m testing.pegasus_gate enforce
2026-04-19 21:16:15 -03:00
gate_rc=$?
set -e
fail=0
if [ "${gate_rc}" -ne 0 ]; then
echo "quality gate failed with rc=${gate_rc}" >&2
fail=1
fi
enabled() {
case "$(printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]')" in
1|true|yes|on) return 0 ;;
*) return 1 ;;
esac
}
if enabled "${QUALITY_GATE_SONARQUBE_ENFORCE:-1}"; then
sonar_status="$(python3 - <<'PY'
import json
from pathlib import Path
path = Path("build/sonarqube-quality-gate.json")
if not path.exists():
print("missing")
raise SystemExit(0)
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except Exception: # noqa: BLE001
print("error")
raise SystemExit(0)
status = (payload.get("status") or payload.get("projectStatus", {}).get("status") or payload.get("qualityGate", {}).get("status") or "").strip().lower()
print(status or "missing")
PY
)"
case "${sonar_status}" in
ok|pass|passed|success) ;;
*)
2026-04-20 22:02:14 -03:00
echo "SonarQube gate failed: ${sonar_status}" >&2
2026-04-19 21:16:15 -03:00
fail=1
;;
esac
fi
2026-04-20 22:02:14 -03:00
ironbank_required=0
if enabled "${QUALITY_GATE_IRONBANK_REQUIRED:-0}"; then
2026-04-19 21:16:15 -03:00
ironbank_required=1
fi
2026-04-20 22:02:14 -03:00
if enabled "${PUBLISH_IMAGES:-0}"; then
ironbank_required=1
fi
if enabled "${QUALITY_GATE_IRONBANK_ENFORCE:-1}" || [ "${ironbank_required}" -eq 1 ]; then
2026-04-19 21:16:15 -03:00
supply_status="$(python3 - <<'PY'
import json
from pathlib import Path
path = Path("build/ironbank-compliance.json")
if not path.exists():
print("missing")
raise SystemExit(0)
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except Exception: # noqa: BLE001
print("error")
raise SystemExit(0)
2026-04-20 22:02:14 -03:00
status = payload.get("status")
if isinstance(status, str) and status.strip():
print(status.strip().lower())
raise SystemExit(0)
2026-04-19 21:16:15 -03:00
compliant = payload.get("compliant")
2026-04-20 22:02:14 -03:00
if isinstance(compliant, bool):
print("ok" if compliant else "failed")
raise SystemExit(0)
print("unknown")
2026-04-19 21:16:15 -03:00
PY
)"
case "${supply_status}" in
2026-04-20 22:02:14 -03:00
ok|pass|passed|success|compliant)
;;
not_applicable)
if [ "${ironbank_required}" -eq 1 ]; then
echo "Supply-chain check is not applicable but required for this build" >&2
2026-04-19 21:16:15 -03:00
fail=1
fi
;;
*)
2026-04-20 22:02:14 -03:00
echo "Supply-chain check failed: ${supply_status}" >&2
fail=1
2026-04-19 21:16:15 -03:00
;;
esac
fi
2026-04-20 22:02:14 -03:00
if [ "${fail}" -ne 0 ]; then
exit 1
fi
2026-04-10 03:25:23 -03:00
'''
}
}
}
}
post {
always {
script {
if (fileExists('build/junit-backend.xml') || fileExists('build/junit-frontend.xml')) {
try {
junit allowEmptyResults: true, testResults: 'build/junit-backend.xml,build/junit-frontend.xml'
} catch (Throwable err) {
echo "junit step unavailable: ${err.class.simpleName}"
}
}
}
archiveArtifacts artifacts: 'build/**', allowEmptyArchive: true, fingerprint: true
}
}
}