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'
  }

  options {
    disableConcurrentBuilds()
  }

  triggers {
    pollSCM('H/5 * * * *')
  }

  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }

    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
            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}"
          '''
        }
      }
    }

    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
    }
  }
}
