ariadne/Jenkinsfile

379 lines
11 KiB
Plaintext
Raw Normal View History

2026-01-19 16:57:18 -03:00
pipeline {
agent {
kubernetes {
defaultContainer 'builder'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: ariadne
spec:
nodeSelector:
kubernetes.io/arch: arm64
node-role.kubernetes.io/worker: "true"
2026-01-22 14:30:53 -03:00
imagePullSecrets:
2026-02-07 10:48:07 -03:00
- name: harbor-robot-pipeline
2026-01-19 16:57:18 -03:00
containers:
- name: dind
2026-04-21 13:34:21 -03:00
image: registry.bstein.dev/bstein/docker:27-dind
2026-01-19 16:57:18 -03:00
securityContext:
privileged: true
env:
- name: DOCKER_TLS_CERTDIR
value: ""
args:
- --mtu=1400
- --host=unix:///var/run/docker.sock
- --host=tcp://0.0.0.0:2375
- --tls=false
2026-01-19 16:57:18 -03:00
volumeMounts:
- name: dind-storage
mountPath: /var/lib/docker
- name: builder
2026-04-21 13:34:21 -03:00
image: registry.bstein.dev/bstein/docker:27
2026-01-19 16:57:18 -03:00
command: ["cat"]
tty: true
env:
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: DOCKER_TLS_CERTDIR
value: ""
2026-01-22 11:28:32 -03:00
- name: DOCKER_CONFIG
value: /root/.docker
2026-01-19 16:57:18 -03:00
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: docker-config-writable
mountPath: /root/.docker
- name: harbor-config
mountPath: /docker-config
2026-01-19 19:01:32 -03:00
- name: tester
2026-04-21 13:16:49 -03:00
image: registry.bstein.dev/bstein/python:3.12-slim
2026-01-19 19:01:32 -03:00
command: ["cat"]
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
2026-01-19 16:57:18 -03:00
volumes:
- name: workspace-volume
emptyDir: {}
- name: docker-config-writable
emptyDir: {}
- name: dind-storage
emptyDir: {}
2026-01-19 16:57:18 -03:00
- name: harbor-config
secret:
2026-02-07 10:48:07 -03:00
secretName: harbor-robot-pipeline
2026-01-19 16:57:18 -03:00
items:
- key: .dockerconfigjson
path: config.json
"""
}
}
environment {
REGISTRY = 'registry.bstein.dev/bstein'
IMAGE = "${REGISTRY}/ariadne"
VERSION_TAG = 'dev'
SEMVER = 'dev'
COVERAGE_MIN = '95'
COVERAGE_JSON = 'build/coverage.json'
JUNIT_XML = 'build/junit.xml'
SUITE_NAME = 'ariadne'
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'
BUILDKIT_IMAGE = 'registry.bstein.dev/bstein/buildkit:buildx-stable-1'
2026-01-19 16:57:18 -03:00
}
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '200', artifactDaysToKeepStr: '30', artifactNumToKeepStr: '120'))
2026-01-19 16:57:18 -03:00
}
triggers {
pollSCM('H/2 * * * *')
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
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
'''
}
}
}
stage('Run quality gate') {
2026-01-19 19:01:32 -03:00
steps {
container('tester') {
sh(script: '''
set -euo pipefail
mkdir -p build
set +e
python -m pip install --no-cache-dir -r requirements.txt -r requirements-dev.txt
install_rc=$?
docs_rc=1
lint_rc=1
loc_rc=1
tests_rc=1
coverage_contract_rc=0
gate_rc=1
if [ "${install_rc}" -eq 0 ]; then
python scripts/check_docstrings.py --root ariadne
docs_rc=$?
python -m ruff check ariadne scripts --select PLR
lint_rc=$?
python scripts/check_file_sizes.py --roots ariadne scripts tests --max-lines 500 --waivers ci/loc_hygiene_waivers.tsv
loc_rc=$?
python -m slipcover \
--json \
--out "${COVERAGE_JSON}" \
--source ariadne \
--fail-under "${COVERAGE_MIN}" \
-m pytest -ra -vv --durations=20 --junitxml "${JUNIT_XML}"
tests_rc=$?
python -c "import json; payload=json.load(open('build/coverage.json', encoding='utf-8')); percent=(payload.get('summary') or {}).get('percent_covered'); print(f'Coverage summary: {percent:.2f}%' if percent is not None else 'Coverage summary unavailable')" || true
if [ -f "${COVERAGE_JSON}" ] && [ -f scripts/check_coverage_contract.py ] && [ -f ci/coverage_contract.json ]; then
python scripts/check_coverage_contract.py "${COVERAGE_JSON}" ci/coverage_contract.json
coverage_contract_rc=$?
else
echo "coverage contract check skipped: checker, contract, or coverage report missing"
fi
fi
printf '%s\n' "${docs_rc}" > build/docs-naming.rc
if [ "${install_rc}" -eq 0 ]; then
gate_rc=0
[ "${docs_rc}" -eq 0 ] || gate_rc=1
[ "${lint_rc}" -eq 0 ] || gate_rc=1
[ "${loc_rc}" -eq 0 ] || gate_rc=1
[ "${tests_rc}" -eq 0 ] || gate_rc=1
[ "${coverage_contract_rc}" -eq 0 ] || gate_rc=1
fi
set -e
printf '%s\n' "${gate_rc}" > build/quality-gate.rc
'''.stripIndent())
}
}
}
stage('Publish test metrics') {
steps {
container('tester') {
sh '''
set -euo pipefail
python scripts/publish_test_metrics.py || true
'''
}
}
}
stage('Enforce quality gate') {
steps {
container('tester') {
sh '''
set -euo pipefail
test "$(cat build/quality-gate.rc 2>/dev/null || echo 1)" -eq 0
2026-01-19 19:01:32 -03:00
'''
}
}
}
2026-01-19 16:57:18 -03:00
stage('Prep toolchain') {
steps {
container('builder') {
sh '''
set -euo pipefail
mkdir -p /root/.docker
cp /docker-config/config.json /root/.docker/config.json
2026-01-22 11:28:32 -03:00
echo "Docker config: ${DOCKER_CONFIG:-unset} HOME=${HOME:-unset}"
2026-01-19 16:57:18 -03:00
'''
}
}
}
stage('Compute version') {
steps {
container('builder') {
script {
sh '''
set -euo pipefail
2026-01-22 14:30:53 -03:00
SEMVER="0.1.0-${BUILD_NUMBER}"
2026-01-19 16:57:18 -03:00
echo "SEMVER=${SEMVER}" > build.env
'''
def props = readProperties file: 'build.env'
env.SEMVER = props['SEMVER'] ?: "0.1.0-${env.BUILD_NUMBER}"
env.VERSION_TAG = env.SEMVER
}
}
}
}
stage('Buildx setup') {
steps {
container('builder') {
sh '''
set -euo pipefail
ready=0
2026-01-19 16:57:18 -03:00
for i in $(seq 1 10); do
if docker info >/dev/null 2>&1; then
ready=1
2026-01-19 16:57:18 -03:00
break
fi
sleep 2
done
if [ "${ready}" -ne 1 ]; then
echo "docker daemon did not become ready on ${DOCKER_HOST}" >&2
docker version || true
exit 1
fi
BUILDER_NAME="ariadne-${BUILD_NUMBER}"
docker buildx rm "${BUILDER_NAME}" >/dev/null 2>&1 || true
docker buildx create --name "${BUILDER_NAME}" --driver docker-container --driver-opt "image=${BUILDKIT_IMAGE}" --bootstrap --use
docker buildx inspect "${BUILDER_NAME}" --bootstrap
2026-01-19 16:57:18 -03:00
'''
}
}
}
stage('Build & push base image') {
steps {
container('builder') {
sh '''
set -euo pipefail
2026-04-21 18:07:00 -03:00
retry_buildx() {
attempt=1
while [ "${attempt}" -le 3 ]; do
if docker buildx build "$@"; then
return 0
fi
echo "buildx attempt ${attempt}/3 failed; retrying after registry/network backoff" >&2
sleep $((attempt * 15))
attempt=$((attempt + 1))
done
return 1
}
retry_buildx \
--platform linux/arm64 \
--file Dockerfile.base \
--tag "${REGISTRY}/ariadne-base:py312" \
--push \
.
'''
}
2026-01-19 16:57:18 -03:00
}
}
stage('Build & push image') {
steps {
container('builder') {
sh '''
set -euo pipefail
VERSION_TAG="$(cut -d= -f2 build.env)"
2026-04-21 18:07:00 -03:00
retry_buildx() {
attempt=1
while [ "${attempt}" -le 3 ]; do
if docker buildx build "$@"; then
return 0
fi
echo "buildx attempt ${attempt}/3 failed; retrying after registry/network backoff" >&2
sleep $((attempt * 15))
attempt=$((attempt + 1))
done
return 1
}
retry_buildx \
2026-01-19 16:57:18 -03:00
--platform linux/arm64 \
--tag "${IMAGE}:${VERSION_TAG}" \
--tag "${IMAGE}:latest" \
--push \
.
'''
}
}
}
}
post {
always {
script {
if (fileExists('build/junit.xml')) {
try {
junit allowEmptyResults: true, testResults: 'build/junit.xml'
} catch (Throwable err) {
echo "junit step unavailable: ${err.class.simpleName}"
}
}
}
archiveArtifacts artifacts: 'build/**', allowEmptyArchive: true, fingerprint: true
2026-01-19 16:57:18 -03:00
script {
def props = fileExists('build.env') ? readProperties(file: 'build.env') : [:]
echo "Build complete for ${props['SEMVER'] ?: env.VERSION_TAG}"
}
}
}
}