soteria/Jenkinsfile

219 lines
6.4 KiB
Plaintext
Raw Normal View History

2026-01-31 05:18:53 -03:00
pipeline {
agent {
kubernetes {
defaultContainer 'builder'
yaml """
apiVersion: v1
kind: Pod
spec:
nodeSelector:
kubernetes.io/arch: arm64
node-role.kubernetes.io/worker: "true"
containers:
- name: dind
image: docker:27-dind
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"
volumeMounts:
- name: dind-storage
mountPath: /var/lib/docker
- name: workspace-volume
mountPath: /home/jenkins/agent
- name: builder
image: docker:27
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
- name: tester
image: golang:1.23-bookworm
command:
- cat
tty: true
volumeMounts:
- name: workspace-volume
mountPath: /home/jenkins/agent
2026-01-31 05:18:53 -03:00
volumes:
- name: docker-config-writable
emptyDir: {}
- name: dind-storage
2026-02-06 19:59:00 -03:00
persistentVolumeClaim:
claimName: jenkins-dind-cache
2026-01-31 05:18:53 -03:00
- name: harbor-config
secret:
2026-02-05 13:10:03 -03:00
secretName: harbor-bstein-robot
2026-01-31 05:18:53 -03:00
items:
- key: .dockerconfigjson
path: config.json
- name: workspace-volume
emptyDir: {}
"""
}
}
environment {
SUITE_NAME = 'soteria'
PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091'
}
2026-01-31 05:18:53 -03:00
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Prep toolchain') {
steps {
container('builder') {
sh '''
set -eu
apk add --no-cache bash git jq curl
2026-01-31 05:18:53 -03:00
mkdir -p /root/.docker
cp /docker-config/config.json /root/.docker/config.json
'''
}
}
}
stage('Unit tests') {
steps {
container('tester') {
sh '''
set -eu
go test ./...
'''
}
}
}
2026-01-31 05:18:53 -03:00
stage('Compute version') {
steps {
container('builder') {
script {
2026-02-05 13:02:52 -03:00
sh 'git config --global --add safe.directory /home/jenkins/agent/workspace/Soteria'
2026-01-31 05:18:53 -03:00
def semver = sh(returnStdout: true, script: 'git describe --tags --exact-match || true').trim()
if (!semver) {
semver = sh(returnStdout: true, script: 'git rev-list --count HEAD').trim()
semver = "0.1.0-${semver}"
}
sh "echo SEMVER=${semver} > build.env"
}
}
}
}
stage('Buildx setup') {
steps {
container('builder') {
sh '''
set -eu
2026-01-31 05:18:53 -03:00
seq 1 10 | while read _; do
docker info && break || sleep 2
done
BUILDER_NAME="soteria-${BUILD_NUMBER}"
docker buildx rm "${BUILDER_NAME}" >/dev/null 2>&1 || true
docker buildx create --name "${BUILDER_NAME}" --driver docker-container --bootstrap --use
2026-01-31 05:18:53 -03:00
'''
}
}
}
stage('Build & push image') {
steps {
container('builder') {
sh '''
set -eu
2026-01-31 05:18:53 -03:00
VERSION_TAG=$(cut -d= -f2 build.env)
docker buildx build --platform linux/arm64 \
2026-02-05 13:58:29 -03:00
--provenance=false \
2026-02-05 13:10:37 -03:00
--tag registry.bstein.dev/bstein/soteria:${VERSION_TAG} \
--tag registry.bstein.dev/bstein/soteria:latest \
2026-01-31 05:18:53 -03:00
--push .
'''
}
}
}
}
post {
success {
container('builder') {
sh '''
set -eu
suite="${SUITE_NAME}"
gateway="${PUSHGATEWAY_URL}"
fetch_counter() {
status="$1"
line="$(curl -fsS "${gateway}/metrics" 2>/dev/null | awk -v suite="${suite}" -v status="${status}" '
/platform_quality_gate_runs_total/ {
if (index($0, "job=\\"platform-quality-ci\\"") && index($0, "suite=\\"" suite "\\"") && index($0, "status=\\"" status "\\"")) {
print $2
exit
}
}
' || true)"
[ -n "${line}" ] && printf '%s\n' "${line}" || printf '0\n'
}
ok_count="$(fetch_counter ok)"
failed_count="$(fetch_counter failed)"
ok_count=$((ok_count + 1))
cat <<METRICS | curl -fsS --data-binary @- "${gateway}/metrics/job/platform-quality-ci/suite/${suite}" >/dev/null
# TYPE platform_quality_gate_runs_total counter
platform_quality_gate_runs_total{suite="${suite}",status="ok"} ${ok_count}
platform_quality_gate_runs_total{suite="${suite}",status="failed"} ${failed_count}
METRICS
'''
}
}
failure {
container('builder') {
sh '''
set -eu
suite="${SUITE_NAME}"
gateway="${PUSHGATEWAY_URL}"
fetch_counter() {
status="$1"
line="$(curl -fsS "${gateway}/metrics" 2>/dev/null | awk -v suite="${suite}" -v status="${status}" '
/platform_quality_gate_runs_total/ {
if (index($0, "job=\\"platform-quality-ci\\"") && index($0, "suite=\\"" suite "\\"") && index($0, "status=\\"" status "\\"")) {
print $2
exit
}
}
' || true)"
[ -n "${line}" ] && printf '%s\n' "${line}" || printf '0\n'
}
ok_count="$(fetch_counter ok)"
failed_count="$(fetch_counter failed)"
failed_count=$((failed_count + 1))
cat <<METRICS | curl -fsS --data-binary @- "${gateway}/metrics/job/platform-quality-ci/suite/${suite}" >/dev/null
# TYPE platform_quality_gate_runs_total counter
platform_quality_gate_runs_total{suite="${suite}",status="ok"} ${ok_count}
platform_quality_gate_runs_total{suite="${suite}",status="failed"} ${failed_count}
METRICS
'''
}
}
2026-01-31 05:18:53 -03:00
always {
script {
if (fileExists('build.env')) {
def env = readProperties file: 'build.env'
echo "Build complete for ${env.SEMVER}"
}
}
archiveArtifacts artifacts: 'build/*', allowEmptyArchive: true
}
}
}