Prompted by migrating SonarQube to k8s mid-session, which briefly broke Jenkins' connectivity to it (server URL config points at a fixed IP:port that had to move). That's exactly the kind of outage this stage shouldn't be able to turn into a failed deploy: analysis quality isn't a deploy gate the way the build/Sonar-unrelated stages are. Wrapped in catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') — if SonarQube is unreachable for any reason, the stage fails visibly (shows red in the stage view) and the build is marked UNSTABLE (not hidden), but the pipeline continues to Deploy instead of stopping there.
98 lines
3.7 KiB
Groovy
98 lines
3.7 KiB
Groovy
pipeline {
|
|
agent {
|
|
kubernetes {
|
|
cloud 'k3s-jenkins-agents'
|
|
label 'docker-agent'
|
|
defaultContainer 'docker-cli'
|
|
}
|
|
}
|
|
|
|
options {
|
|
skipDefaultCheckout()
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout') {
|
|
steps {
|
|
sh '''
|
|
echo "Copying CloudOps from the VPS's own checkout (hostPath-mounted, no Gitea fetch)..."
|
|
cp -r /root/CloudOps/. .
|
|
test -f platform/app.py
|
|
'''
|
|
}
|
|
}
|
|
|
|
stage('Build Docker Image') {
|
|
steps {
|
|
sh '''
|
|
docker build -t management-platform:${BUILD_NUMBER} -t management-platform:latest ./platform/
|
|
echo "✅ Docker image built"
|
|
docker images management-platform:${BUILD_NUMBER} --format "Size: {{.Size}}"
|
|
'''
|
|
}
|
|
}
|
|
|
|
stage('SonarQube Analysis') {
|
|
steps {
|
|
container('sonar-scanner') {
|
|
// Analysis quality, not a deploy gate: SonarQube being
|
|
// unreachable (moved server, migration in progress,
|
|
// temporary outage) shouldn't block shipping an
|
|
// otherwise-good build. catchError marks the build
|
|
// UNSTABLE (visible in the UI, doesn't need separate
|
|
// watching) and lets the pipeline continue to Deploy
|
|
// instead of failing the whole run.
|
|
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
|
|
withSonarQubeEnv('sonarqube') {
|
|
script {
|
|
def scannerHome = tool 'sonar-scanner'
|
|
sh """
|
|
${scannerHome}/bin/sonar-scanner \
|
|
-Dsonar.projectKey=management-platform \
|
|
-Dsonar.sources=platform \
|
|
-Dsonar.python.version=3
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Deploy to Kubernetes') {
|
|
steps {
|
|
container('docker-cli') {
|
|
sh '''
|
|
docker save management-platform:${BUILD_NUMBER} | \
|
|
/usr/local/bin/k3s ctr -a /run/k3s/containerd/containerd.sock -n k8s.io images import -
|
|
|
|
set +x
|
|
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
|
|
CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
|
|
|
|
/usr/local/bin/k3s kubectl --server=https://kubernetes.default.svc \
|
|
--token="$TOKEN" --certificate-authority="$CACERT" \
|
|
set image deployment/management-platform \
|
|
management-platform=management-platform:${BUILD_NUMBER} \
|
|
-n management-platform
|
|
|
|
/usr/local/bin/k3s kubectl --server=https://kubernetes.default.svc \
|
|
--token="$TOKEN" --certificate-authority="$CACERT" \
|
|
rollout status deployment/management-platform -n management-platform --timeout=90s
|
|
|
|
echo "✅ Deployed management-platform:${BUILD_NUMBER} to Kubernetes"
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo '✅ Pipeline succeeded — build + Sonar passed, deployed to k8s'
|
|
}
|
|
failure {
|
|
echo '❌ Pipeline failed — check logs'
|
|
}
|
|
}
|
|
} |