239 lines
7.2 KiB
Groovy
239 lines
7.2 KiB
Groovy
pipeline {
|
|
agent {
|
|
kubernetes {
|
|
label 'ananke-quality'
|
|
defaultContainer 'go-tester'
|
|
yaml """
|
|
apiVersion: v1
|
|
kind: Pod
|
|
spec:
|
|
nodeSelector:
|
|
kubernetes.io/arch: arm64
|
|
node-role.kubernetes.io/worker: "true"
|
|
containers:
|
|
- name: go-tester
|
|
image: golang:1.25-bookworm
|
|
command: ["cat"]
|
|
tty: true
|
|
volumeMounts:
|
|
- name: workspace-volume
|
|
mountPath: /home/jenkins/agent
|
|
- name: publisher
|
|
image: python:3.12-slim
|
|
command: ["cat"]
|
|
tty: true
|
|
volumeMounts:
|
|
- name: workspace-volume
|
|
mountPath: /home/jenkins/agent
|
|
volumes:
|
|
- name: workspace-volume
|
|
emptyDir: {}
|
|
"""
|
|
}
|
|
}
|
|
|
|
environment {
|
|
SUITE_NAME = 'ananke'
|
|
PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091'
|
|
QUALITY_GATE_SONARQUBE_REPORT = 'build/sonarqube-quality-gate.json'
|
|
QUALITY_GATE_IRONBANK_REPORT = 'build/ironbank-compliance.json'
|
|
}
|
|
|
|
options {
|
|
disableConcurrentBuilds()
|
|
}
|
|
|
|
triggers {
|
|
pollSCM('H/5 * * * *')
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout') {
|
|
steps {
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
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
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Run quality gate') {
|
|
steps {
|
|
container('go-tester') {
|
|
sh '''
|
|
set -eu
|
|
mkdir -p build
|
|
set +e
|
|
export GOFLAGS='-buildvcs=false'
|
|
ANANKE_QUALITY_METRICS_ENABLED=0 \
|
|
ANANKE_QUALITY_PUSHGATEWAY_ENABLED=0 \
|
|
ANANKE_QUALITY_STATE_FILE="$PWD/build/quality-gate.state" \
|
|
./scripts/quality_gate.sh > build/quality-gate.out 2>&1
|
|
gate_rc=$?
|
|
set -e
|
|
cat build/quality-gate.out
|
|
printf '%s\\n' "${gate_rc}" > build/quality-gate.rc
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Publish test metrics') {
|
|
steps {
|
|
container('publisher') {
|
|
sh '''
|
|
set -eu
|
|
ok_runs="$(awk -F= '$1=="ok"{print $2}' build/quality-gate.state 2>/dev/null | tail -n1)"
|
|
failed_runs="$(awk -F= '$1=="failed"{print $2}' build/quality-gate.state 2>/dev/null | tail -n1)"
|
|
[ -n "${ok_runs}" ] || ok_runs=0
|
|
[ -n "${failed_runs}" ] || failed_runs=0
|
|
gate_rc="$(cat build/quality-gate.rc 2>/dev/null || echo 1)"
|
|
tests_passed=0
|
|
tests_failed=1
|
|
if [ "${gate_rc}" -eq 0 ]; then
|
|
tests_passed=1
|
|
tests_failed=0
|
|
fi
|
|
coverage_percent="$(python3 - <<'PY'
|
|
import re
|
|
from pathlib import Path
|
|
|
|
log_path = Path("build/quality-gate.out")
|
|
text = log_path.read_text(encoding="utf-8", errors="ignore") if log_path.exists() else ""
|
|
values = [float(match.group(1)) for match in re.finditer(r"([0-9]+(?:\\.[0-9]+)?)%", text)]
|
|
print(values[-1] if values else 0.0)
|
|
PY
|
|
)"
|
|
source_lines_over_500="$(python3 - <<'PY'
|
|
from pathlib import Path
|
|
|
|
root = Path(".")
|
|
skip = {".git", ".venv", "venv", "build", "dist", "node_modules", "__pycache__", ".pytest_cache"}
|
|
suffixes = {".go", ".py", ".sh", ".json", ".yaml", ".yml"}
|
|
count = 0
|
|
for path in root.rglob("*"):
|
|
if not path.is_file():
|
|
continue
|
|
if any(part in skip for part in path.parts):
|
|
continue
|
|
if path.name != "Jenkinsfile" and path.suffix.lower() not in suffixes:
|
|
continue
|
|
try:
|
|
lines = sum(1 for _ in path.open("r", encoding="utf-8", errors="ignore"))
|
|
except OSError:
|
|
continue
|
|
if lines > 500:
|
|
count += 1
|
|
print(count)
|
|
PY
|
|
)"
|
|
check_status="failed"
|
|
if [ "${gate_rc}" -eq 0 ]; then
|
|
check_status="ok"
|
|
fi
|
|
python3 scripts/publish_quality_metrics.py \
|
|
--pushgateway-url "${PUSHGATEWAY_URL}" \
|
|
--job-name platform-quality-ci \
|
|
--suite "${SUITE_NAME}" \
|
|
--trigger jenkins \
|
|
--local-ok "${ok_runs}" \
|
|
--local-failed "${failed_runs}" \
|
|
--tests-passed "${tests_passed}" \
|
|
--tests-failed "${tests_failed}" \
|
|
--tests-error 0 \
|
|
--tests-skipped 0 \
|
|
--coverage-percent "${coverage_percent}" \
|
|
--source-lines-over-500 "${source_lines_over_500}" \
|
|
--check "tests:${check_status}" \
|
|
--check "hygiene:${check_status}" \
|
|
--check "lint:${check_status}" \
|
|
--check "coverage:${check_status}" \
|
|
--check "gate:${check_status}"
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Enforce quality gate') {
|
|
steps {
|
|
container('publisher') {
|
|
sh '''
|
|
set -eu
|
|
test "$(cat build/quality-gate.rc 2>/dev/null || echo 1)" -eq 0
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always {
|
|
archiveArtifacts artifacts: 'build/quality-gate.out,build/quality-gate.rc', allowEmptyArchive: true, fingerprint: true
|
|
}
|
|
}
|
|
}
|