2025-12-18 01:13:04 -03:00
pipeline {
agent {
kubernetes {
defaultContainer 'builder'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: bstein-dev-home
spec:
nodeSelector:
kubernetes.io/arch: arm64
node-role.kubernetes.io/worker: "true"
containers:
- name: dind
2026-04-21 13:35:10 -03:00
image: registry.bstein.dev/bstein/docker:27-dind
2025-12-18 01:13:04 -03:00
securityContext:
privileged: true
env:
- name: DOCKER_TLS_CERTDIR
value: ""
args:
2025-12-18 10:28:50 -03:00
- --mtu=1400
- --host=unix:///var/run/docker.sock
- --host=tcp://0.0.0.0:2375
2026-04-21 16:59:14 -03:00
- --tls=false
2025-12-18 01:13:04 -03:00
volumeMounts:
- name: dind-storage
mountPath: /var/lib/docker
- name: builder
2026-04-21 13:35:10 -03:00
image: registry.bstein.dev/bstein/docker:27
2025-12-18 01:13:04 -03:00
command: ["cat"]
tty: true
env:
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: DOCKER_TLS_CERTDIR
value: ""
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: docker-config-writable
mountPath: /root/.docker
- name: harbor-config
mountPath: /docker-config
2026-04-10 05:18:52 -03:00
- name: tester
2026-04-21 13:18:07 -03:00
image: registry.bstein.dev/bstein/python:3.12-slim
2026-04-10 05:18:52 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
2026-04-11 00:02:26 -03:00
- name: frontend
2026-04-21 13:35:10 -03:00
image: registry.bstein.dev/bstein/playwright:v1.59.1-jammy
2026-04-11 00:02:26 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
2025-12-18 01:13:04 -03:00
volumes:
- name: workspace-volume
emptyDir: {}
- name: docker-config-writable
emptyDir: {}
- name: dind-storage
2026-04-10 05:59:39 -03:00
emptyDir: {}
2025-12-18 01:13:04 -03:00
- name: harbor-config
secret:
secretName: harbor-bstein-robot
items:
- key: .dockerconfigjson
path: config.json
"""
}
}
environment {
REGISTRY = 'registry.bstein.dev/bstein'
FRONT_IMAGE = "${REGISTRY}/bstein-dev-home-frontend"
BACK_IMAGE = "${REGISTRY}/bstein-dev-home-backend"
2025-12-18 11:08:45 -03:00
VERSION_TAG = 'dev'
SEMVER = 'dev'
2026-04-19 14:12:32 -03:00
SUITE_NAME = 'bstein_home'
2026-04-10 05:18:52 -03:00
PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091'
2026-04-19 21:29:34 -03:00
SONARQUBE_HOST_URL = 'http://sonarqube.quality.svc.cluster.local:9000'
SONARQUBE_PROJECT_KEY = 'bstein_home'
QUALITY_GATE_SONARQUBE_ENFORCE = '0'
2026-04-19 14:12:32 -03:00
QUALITY_GATE_SONARQUBE_REPORT = 'build/sonarqube-quality-gate.json'
2026-04-19 21:29:34 -03:00
QUALITY_GATE_IRONBANK_ENFORCE = '0'
2026-04-19 21:16:09 -03:00
QUALITY_GATE_IRONBANK_REQUIRED = '1'
2026-04-19 14:12:32 -03:00
QUALITY_GATE_IRONBANK_REPORT = 'build/ironbank-compliance.json'
2025-12-18 01:13:04 -03:00
}
options {
disableConcurrentBuilds()
2026-04-20 12:29:29 -03:00
buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '200', artifactDaysToKeepStr: '30', artifactNumToKeepStr: '120'))
2025-12-18 01:13:04 -03:00
}
triggers {
2025-12-18 02:23:39 -03:00
// Poll every 2 minutes; notifyCommit can also trigger, but polling keeps it moving without webhook tokens.
pollSCM('H/2 * * * *')
2025-12-18 01:13:04 -03:00
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
2026-04-19 14:12:32 -03:00
stage('Collect SonarQube evidence') {
steps {
container('tester') {
sh '''
set -euo pipefail
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('tester') {
sh '''
set -euo pipefail
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
'''
}
}
}
2025-12-18 01:13:04 -03:00
stage('Prep toolchain') {
steps {
container('builder') {
2026-04-20 13:09:28 -03:00
sh '''
set -euo pipefail
for attempt in 1 2 3 4 5; do
if apk add --no-cache bash git jq curl; then
break
fi
if [ "$attempt" -eq 5 ]; then
echo "apk add failed after ${attempt} attempts" >&2
exit 1
fi
sleep $((attempt * 2))
done
mkdir -p /root/.docker
cp /docker-config/config.json /root/.docker/config.json
'''
2025-12-18 01:13:04 -03:00
}
}
}
stage('Compute version') {
steps {
container('builder') {
script {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2025-12-18 02:50:07 -03:00
if git describe --tags --exact-match >/dev/null 2>&1; then
SEMVER="$(git describe --tags --exact-match)"
else
BASE="$(jq -r '.version' frontend/package.json || echo '0.1.0')"
SEMVER="${BASE}-${BUILD_NUMBER}"
fi
# Accept bare semver or leading v (with optional prerelease).
2025-12-18 11:00:53 -03:00
if ! echo "$SEMVER" | grep -Eq '^v?[0-9]+\\.[0-9]+\\.[0-9]+([-.][0-9A-Za-z]+)?$'; then
2025-12-18 02:50:07 -03:00
SEMVER="0.1.0-${BUILD_NUMBER}"
2025-12-18 01:13:04 -03:00
fi
echo "SEMVER=${SEMVER}" > build.env
'''
def props = readProperties file: 'build.env'
2025-12-18 11:08:45 -03:00
env.SEMVER = props['SEMVER'] ?: "0.1.0-${env.BUILD_NUMBER}"
2025-12-18 01:13:04 -03:00
env.VERSION_TAG = env.SEMVER
}
}
}
}
2026-04-21 15:55:49 -03:00
stage('Docker readiness') {
2025-12-18 01:13:04 -03:00
steps {
container('builder') {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2026-04-19 15:45:15 -03:00
mkdir -p build
set +e
2026-04-10 05:59:39 -03:00
ready=0
2025-12-18 01:13:04 -03:00
for i in $(seq 1 10); do
if docker info >/dev/null 2>&1; then
2026-04-10 05:59:39 -03:00
ready=1
2025-12-18 01:13:04 -03:00
break
fi
sleep 2
done
2026-04-19 15:45:15 -03:00
rc=0
2026-04-10 05:59:39 -03:00
if [ "${ready}" -ne 1 ]; then
echo "docker daemon did not become ready on ${DOCKER_HOST}" >&2
docker version || true
2026-04-19 15:45:15 -03:00
rc=1
fi
set -e
2026-04-21 15:55:49 -03:00
printf '%s\n' "${rc}" > build/docker-ready.rc
2026-04-19 15:45:15 -03:00
if [ "${rc}" -ne 0 ]; then
2026-04-21 15:55:49 -03:00
echo "warning: docker daemon readiness failed; publish stages will fail later" >&2
2026-04-10 05:59:39 -03:00
fi
2025-12-18 01:13:04 -03:00
'''
}
}
}
2026-04-10 05:18:52 -03:00
stage('Backend unit tests') {
steps {
container('tester') {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2026-04-10 05:18:52 -03:00
mkdir -p build
2026-04-10 06:24:22 -03:00
export PYTHONPATH="${WORKSPACE}/backend:${PYTHONPATH:-}"
2026-04-19 15:37:06 -03:00
set +e
2026-04-20 13:13:07 -03:00
pip_rc=1
for attempt in 1 2 3 4 5; do
python -m pip install --no-cache-dir -r backend/requirements.txt -r backend/requirements-dev.txt
pip_rc=$?
if [ "${pip_rc}" -eq 0 ]; then
break
fi
if [ "${attempt}" -lt 5 ]; then
sleep $((attempt * 4))
fi
done
backend_rc=1
if [ "${pip_rc}" -eq 0 ]; then
python -m pytest backend/tests -q --cov=backend/atlas_portal --cov-report=xml:build/backend-coverage.xml --junitxml=build/junit-backend.xml
backend_rc=$?
else
echo "backend dependency install failed after retries" >&2
fi
2026-04-19 15:37:06 -03:00
set -e
printf '%s\n' "${backend_rc}" > build/backend-tests.rc
2026-04-11 00:02:26 -03:00
'''
}
}
}
stage('Frontend tests') {
steps {
container('frontend') {
2026-04-19 15:01:31 -03:00
sh(script: '''#!/usr/bin/env bash
set -euo pipefail
mkdir -p build
2026-04-21 14:57:28 -03:00
ulimit -n 8192 || true
2026-04-19 15:01:31 -03:00
cd frontend
2026-04-19 15:37:06 -03:00
set +e
2026-04-20 13:13:07 -03:00
npm_ci_rc=1
for attempt in 1 2 3 4 5; do
npm ci
npm_ci_rc=$?
if [ "${npm_ci_rc}" -eq 0 ]; then
break
fi
if [ "${attempt}" -lt 5 ]; then
sleep $((attempt * 4))
fi
done
frontend_rc=1
if [ "${npm_ci_rc}" -eq 0 ]; then
npm run lint
npm run test:unit
npm run test:component
npm run test:e2e
frontend_rc=$?
else
echo "frontend dependency install failed after retries" >&2
fi
2026-04-19 15:37:06 -03:00
set -e
printf '%s\n' "${frontend_rc}" > ../build/frontend-tests.rc
2026-04-19 15:01:31 -03:00
''')
2026-04-11 00:02:26 -03:00
}
}
}
2026-04-19 14:12:32 -03:00
stage('Run quality gate') {
2026-04-11 00:02:26 -03:00
steps {
container('tester') {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2026-04-11 00:02:26 -03:00
export PYTHONPATH="${WORKSPACE}:${PYTHONPATH:-}"
2026-04-19 14:12:32 -03:00
set +e
2026-04-11 00:02:26 -03:00
python -m testing.ci.quality_gate \
--backend-coverage build/backend-coverage.xml \
--frontend-coverage frontend/coverage/coverage-summary.json \
--report build/quality-gate.json
2026-04-19 14:12:32 -03:00
gate_rc=$?
2026-04-19 15:37:06 -03:00
backend_rc="$(cat build/backend-tests.rc 2>/dev/null || echo 1)"
frontend_rc="$(cat build/frontend-tests.rc 2>/dev/null || echo 1)"
if [ "${backend_rc}" -ne 0 ] || [ "${frontend_rc}" -ne 0 ]; then
gate_rc=1
fi
2026-04-19 14:12:32 -03:00
set -e
printf '%s\n' "${gate_rc}" > build/quality-gate.rc
'''
}
}
}
stage('Publish test metrics') {
steps {
container('tester') {
sh '''
set -euo pipefail
gate_rc="$(cat build/quality-gate.rc 2>/dev/null || echo 1)"
status="ok"
if [ "${gate_rc}" -ne 0 ]; then
status="failed"
fi
python -m testing.ci.publish_metrics \
--gateway "${PUSHGATEWAY_URL}" \
--suite "${SUITE_NAME}" \
--job platform-quality-ci \
--status "${status}" \
--quality-report build/quality-gate.json \
--junit build/junit-backend.xml build/junit-frontend-unit.xml build/junit-frontend-component.xml build/junit-frontend-e2e.xml
'''
}
}
}
stage('Enforce quality gate') {
steps {
container('tester') {
sh '''
set -euo pipefail
2026-04-19 21:16:09 -03:00
gate_rc="$(cat build/quality-gate.rc 2>/dev/null || echo 1)"
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) ;;
*)
echo "sonarqube gate failed: ${sonar_status}" >&2
fail=1
;;
esac
fi
ironbank_required="${QUALITY_GATE_IRONBANK_REQUIRED:-1}"
if [ "${PUBLISH_IMAGES:-false}" = "true" ]; then
ironbank_required=1
fi
if enabled "${QUALITY_GATE_IRONBANK_ENFORCE:-1}"; then
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)
compliant = payload.get("compliant")
if compliant is True:
print("ok")
elif compliant is False:
print("failed")
else:
status = str(payload.get("status") or payload.get("result") or payload.get("compliance") or "").strip().lower()
print(status or "missing")
PY
)"
case "${supply_status}" in
ok|pass|passed|success|compliant) ;;
not_applicable|na|n/a)
if enabled "${ironbank_required}"; then
echo "supply chain gate required but status=${supply_status}" >&2
fail=1
fi
;;
*)
if enabled "${ironbank_required}"; then
echo "supply chain gate failed: ${supply_status}" >&2
fail=1
else
echo "supply chain gate not passing (${supply_status}) but not required for this run" >&2
fi
;;
esac
fi
exit "${fail}"
2026-04-10 05:18:52 -03:00
'''
}
}
}
2025-12-18 01:13:04 -03:00
stage('Build & push frontend') {
steps {
container('builder') {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2026-04-21 15:55:49 -03:00
test "$(cat build/docker-ready.rc 2>/dev/null || echo 1)" -eq 0
2025-12-18 11:30:17 -03:00
VERSION_TAG="$(cut -d= -f2 build.env)"
2026-04-21 15:55:49 -03:00
for attempt in 1 2 3; do
2026-04-21 16:49:15 -03:00
if docker build --tag "${FRONT_IMAGE}:${VERSION_TAG}" --tag "${FRONT_IMAGE}:latest" --file Dockerfile.frontend .; then
2026-04-21 15:55:49 -03:00
break
fi
if [ "${attempt}" -eq 3 ]; then
exit 1
fi
sleep $((attempt * 10))
done
docker push "${FRONT_IMAGE}:${VERSION_TAG}"
docker push "${FRONT_IMAGE}:latest"
2025-12-18 01:13:04 -03:00
'''
}
}
}
stage('Build & push backend') {
steps {
container('builder') {
sh '''
2026-04-19 14:12:32 -03:00
set -euo pipefail
2026-04-21 15:55:49 -03:00
test "$(cat build/docker-ready.rc 2>/dev/null || echo 1)" -eq 0
2025-12-18 11:30:17 -03:00
VERSION_TAG="$(cut -d= -f2 build.env)"
2026-04-21 15:55:49 -03:00
for attempt in 1 2 3; do
2026-04-21 16:49:15 -03:00
if docker build --tag "${BACK_IMAGE}:${VERSION_TAG}" --tag "${BACK_IMAGE}:latest" --file Dockerfile.backend .; then
2026-04-21 15:55:49 -03:00
break
fi
if [ "${attempt}" -eq 3 ]; then
exit 1
fi
sleep $((attempt * 10))
done
docker push "${BACK_IMAGE}:${VERSION_TAG}"
docker push "${BACK_IMAGE}:latest"
2025-12-18 01:13:04 -03:00
'''
}
}
}
}
post {
always {
2025-12-18 11:30:17 -03:00
script {
def props = fileExists('build.env') ? readProperties(file: 'build.env') : [:]
echo "Build complete for ${props['SEMVER'] ?: env.VERSION_TAG}"
}
2026-04-21 09:22:06 -03:00
archiveArtifacts artifacts: 'build/**', allowEmptyArchive: true, fingerprint: true
2025-12-18 01:13:04 -03:00
}
}
}