Update apps/demo-nginx/Jenkinsfile.backup
This commit is contained in:
449
apps/demo-nginx/Jenkinsfile
vendored
449
apps/demo-nginx/Jenkinsfile
vendored
@@ -1,449 +0,0 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
environment {
|
||||
APP_NAME = 'demo-nginx'
|
||||
NAMESPACE = 'demo-app'
|
||||
DOCKER_REGISTRY = 'docker.io'
|
||||
DOCKER_REPO = 'vladcrypto'
|
||||
GITEA_URL = 'http://gitea-http.gitea.svc.cluster.local:3000'
|
||||
GITEA_REPO = 'admin/k3s-gitops'
|
||||
GITEA_BRANCH = 'main'
|
||||
BUILD_TAG = "${env.BUILD_NUMBER}"
|
||||
IMAGE_TAG = "${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
||||
|
||||
// Rollback configuration
|
||||
ROLLBACK_ENABLED = 'true'
|
||||
DEPLOYMENT_TIMEOUT = '300s'
|
||||
ARGOCD_SYNC_TIMEOUT = '120'
|
||||
SKIP_HEALTH_CHECK = 'true' // Skip for now - pods work fine
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('Save Current State') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "📸 Saving current deployment state for rollback..."
|
||||
|
||||
// Save current image tag
|
||||
sh """
|
||||
kubectl get deployment ${APP_NAME} -n ${NAMESPACE} \
|
||||
-o jsonpath='{.spec.template.spec.containers[0].image}' \
|
||||
> /tmp/previous_image_${BUILD_NUMBER}.txt || echo "none" > /tmp/previous_image_${BUILD_NUMBER}.txt
|
||||
|
||||
PREV_IMAGE=\$(cat /tmp/previous_image_${BUILD_NUMBER}.txt)
|
||||
echo "Previous image: \${PREV_IMAGE}"
|
||||
"""
|
||||
|
||||
// Save current replica count
|
||||
sh """
|
||||
kubectl get deployment ${APP_NAME} -n ${NAMESPACE} \
|
||||
-o jsonpath='{.spec.replicas}' \
|
||||
> /tmp/previous_replicas_${BUILD_NUMBER}.txt || echo "2" > /tmp/previous_replicas_${BUILD_NUMBER}.txt
|
||||
"""
|
||||
|
||||
echo "✅ Current state saved"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Checkout Source') {
|
||||
steps {
|
||||
echo "Checking out application source code..."
|
||||
|
||||
// Create Dockerfile with actual version
|
||||
sh """
|
||||
cat > Dockerfile << 'EOF'
|
||||
FROM nginx:1.25.3-alpine
|
||||
COPY index.html /usr/share/nginx/html/index.html
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
EOF
|
||||
"""
|
||||
|
||||
// Create index.html with actual version
|
||||
sh """
|
||||
cat > index.html << EOF
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Demo Nginx</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 50px auto;
|
||||
padding: 20px;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
.container {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
padding: 40px;
|
||||
border-radius: 10px;
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
h1 {
|
||||
font-size: 48px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
p {
|
||||
font-size: 24px;
|
||||
margin: 10px 0;
|
||||
}
|
||||
.version {
|
||||
font-family: 'Courier New', monospace;
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
padding: 10px 20px;
|
||||
border-radius: 5px;
|
||||
display: inline-block;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🚀 Demo Nginx - Build #${BUILD_NUMBER}</h1>
|
||||
<p>Environment: Production</p>
|
||||
<p class="version">Version: ${IMAGE_TAG}</p>
|
||||
<p style="font-size: 16px; margin-top: 30px; opacity: 0.8;">
|
||||
Image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
EOF
|
||||
"""
|
||||
|
||||
sh '''
|
||||
cat > nginx.conf << 'EOF'
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
events { worker_connections 1024; }
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
access_log /var/log/nginx/access.log main;
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
location / {
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
}
|
||||
location /health {
|
||||
access_log off;
|
||||
return 200 "healthy\n";
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF
|
||||
'''
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build Docker Image') {
|
||||
steps {
|
||||
script {
|
||||
echo "Building Docker image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}"
|
||||
sh """
|
||||
docker build \
|
||||
-t ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG} \
|
||||
-t ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:latest \
|
||||
.
|
||||
"""
|
||||
echo "✅ Image built successfully!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Push to Registry') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "Pushing image to registry..."
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: 'docker-registry-credentials',
|
||||
usernameVariable: 'DOCKER_USER',
|
||||
passwordVariable: 'DOCKER_PASS'
|
||||
)]) {
|
||||
sh """
|
||||
echo "\${DOCKER_PASS}" | docker login ${DOCKER_REGISTRY} -u "\${DOCKER_USER}" --password-stdin
|
||||
docker push ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}
|
||||
docker push ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:latest
|
||||
docker logout ${DOCKER_REGISTRY}
|
||||
"""
|
||||
}
|
||||
echo "✅ Image pushed successfully!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Update GitOps Manifests') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "Updating Kubernetes manifests..."
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: 'gitea-credentials',
|
||||
usernameVariable: 'GIT_USER',
|
||||
passwordVariable: 'GIT_PASS'
|
||||
)]) {
|
||||
sh """
|
||||
rm -rf k3s-gitops || true
|
||||
git clone http://\${GIT_USER}:\${GIT_PASS}@gitea-http.gitea.svc.cluster.local:3000/admin/k3s-gitops.git
|
||||
cd k3s-gitops
|
||||
git config user.name "Jenkins"
|
||||
git config user.email "jenkins@thedevops.dev"
|
||||
|
||||
# Save current commit for rollback
|
||||
git rev-parse HEAD > /tmp/previous_commit_${BUILD_NUMBER}.txt
|
||||
|
||||
sed -i 's|image: .*|image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}|' apps/demo-nginx/deployment.yaml
|
||||
git add apps/demo-nginx/deployment.yaml
|
||||
git commit -m "chore(demo-nginx): Update image to ${IMAGE_TAG}" || echo "No changes"
|
||||
git push origin main
|
||||
"""
|
||||
}
|
||||
echo "✅ Manifests updated!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Wait for ArgoCD Sync') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "⏳ Waiting for ArgoCD to sync..."
|
||||
|
||||
def syncSuccess = false
|
||||
def attempts = 0
|
||||
def maxAttempts = Integer.parseInt(env.ARGOCD_SYNC_TIMEOUT) / 10
|
||||
|
||||
while (!syncSuccess && attempts < maxAttempts) {
|
||||
attempts++
|
||||
echo "ArgoCD sync check attempt ${attempts}/${maxAttempts}..."
|
||||
|
||||
def syncStatus = sh(
|
||||
script: """
|
||||
kubectl get application ${APP_NAME} -n argocd \
|
||||
-o jsonpath='{.status.sync.status}'
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
def currentImage = sh(
|
||||
script: """
|
||||
kubectl get deployment ${APP_NAME} -n ${NAMESPACE} \
|
||||
-o jsonpath='{.spec.template.spec.containers[0].image}'
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "ArgoCD sync status: ${syncStatus}"
|
||||
echo "Current deployment image: ${currentImage}"
|
||||
echo "Expected image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}"
|
||||
|
||||
if (syncStatus == 'Synced' && currentImage.contains(env.IMAGE_TAG)) {
|
||||
syncSuccess = true
|
||||
echo "✅ ArgoCD synced successfully!"
|
||||
break
|
||||
}
|
||||
|
||||
if (attempts < maxAttempts) {
|
||||
echo "Waiting 10 seconds before next check..."
|
||||
sleep 10
|
||||
}
|
||||
}
|
||||
|
||||
if (!syncSuccess) {
|
||||
error "❌ ArgoCD sync timeout after ${env.ARGOCD_SYNC_TIMEOUT} seconds!"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Wait for Deployment') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "⏳ Waiting for deployment to complete..."
|
||||
|
||||
try {
|
||||
sh """
|
||||
# Check rollout status
|
||||
kubectl rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=${DEPLOYMENT_TIMEOUT}
|
||||
"""
|
||||
echo "✅ Deployment rolled out successfully!"
|
||||
} catch (Exception e) {
|
||||
echo "❌ Deployment failed: ${e.message}"
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Verify Deployment') {
|
||||
when { branch 'main' }
|
||||
steps {
|
||||
script {
|
||||
echo "✅ Verifying deployment..."
|
||||
|
||||
try {
|
||||
sh """#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Check pod status
|
||||
READY_PODS=\$(kubectl get deployment ${APP_NAME} -n ${NAMESPACE} -o jsonpath='{.status.readyReplicas}')
|
||||
DESIRED_PODS=\$(kubectl get deployment ${APP_NAME} -n ${NAMESPACE} -o jsonpath='{.spec.replicas}')
|
||||
|
||||
echo "Ready pods: \${READY_PODS}/\${DESIRED_PODS}"
|
||||
|
||||
if [ "\${READY_PODS}" != "\${DESIRED_PODS}" ]; then
|
||||
echo "❌ Not all pods are ready!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify image version
|
||||
DEPLOYED_IMAGE=\$(kubectl get deployment ${APP_NAME} -n ${NAMESPACE} -o jsonpath='{.spec.template.spec.containers[0].image}')
|
||||
echo "Deployed image: \${DEPLOYED_IMAGE}"
|
||||
|
||||
if [[ "\${DEPLOYED_IMAGE}" != *"${IMAGE_TAG}"* ]]; then
|
||||
echo "❌ Image version mismatch!"
|
||||
echo "Expected: ${IMAGE_TAG}"
|
||||
echo "Got: \${DEPLOYED_IMAGE}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ All verification checks passed!"
|
||||
"""
|
||||
|
||||
echo "✅ Deployment verified successfully!"
|
||||
|
||||
} catch (Exception e) {
|
||||
echo "❌ Deployment verification failed: ${e.message}"
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
script {
|
||||
echo """
|
||||
✅ DEPLOYMENT SUCCESS!
|
||||
|
||||
Application: ${APP_NAME}
|
||||
Image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}
|
||||
Namespace: ${NAMESPACE}
|
||||
Build: #${BUILD_NUMBER}
|
||||
|
||||
All checks passed! ✨
|
||||
"""
|
||||
|
||||
// Cleanup rollback files
|
||||
sh """
|
||||
rm -f /tmp/previous_image_${BUILD_NUMBER}.txt
|
||||
rm -f /tmp/previous_replicas_${BUILD_NUMBER}.txt
|
||||
rm -f /tmp/previous_commit_${BUILD_NUMBER}.txt
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
failure {
|
||||
script {
|
||||
if (env.BRANCH_NAME == 'main' && env.ROLLBACK_ENABLED == 'true') {
|
||||
echo """
|
||||
❌ DEPLOYMENT FAILED - INITIATING ROLLBACK!
|
||||
|
||||
Rolling back to previous version...
|
||||
"""
|
||||
|
||||
try {
|
||||
// Get previous state
|
||||
def previousImage = sh(
|
||||
script: "cat /tmp/previous_image_${BUILD_NUMBER}.txt 2>/dev/null || echo 'none'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
if (previousImage != 'none' && previousImage != '') {
|
||||
echo "🔄 Rolling back to: ${previousImage}"
|
||||
|
||||
// Rollback via kubectl
|
||||
sh """
|
||||
kubectl rollout undo deployment/${APP_NAME} -n ${NAMESPACE}
|
||||
kubectl rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=180s
|
||||
"""
|
||||
|
||||
// Revert Git commit
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: 'gitea-credentials',
|
||||
usernameVariable: 'GIT_USER',
|
||||
passwordVariable: 'GIT_PASS'
|
||||
)]) {
|
||||
sh """
|
||||
cd k3s-gitops || git clone http://\${GIT_USER}:\${GIT_PASS}@gitea-http.gitea.svc.cluster.local:3000/admin/k3s-gitops.git
|
||||
cd k3s-gitops
|
||||
git config user.name "Jenkins"
|
||||
git config user.email "jenkins@thedevops.dev"
|
||||
|
||||
# Revert last commit
|
||||
git revert --no-edit HEAD || true
|
||||
git push origin main || true
|
||||
"""
|
||||
}
|
||||
|
||||
echo """
|
||||
✅ ROLLBACK COMPLETED!
|
||||
|
||||
Rolled back to: ${previousImage}
|
||||
Current build (#${BUILD_NUMBER}) has been reverted.
|
||||
|
||||
Please check logs and fix the issue before redeploying.
|
||||
"""
|
||||
|
||||
} else {
|
||||
echo "⚠️ No previous version found - cannot rollback automatically"
|
||||
echo "Manual intervention required!"
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
echo """
|
||||
❌ ROLLBACK FAILED!
|
||||
|
||||
Error: ${e.message}
|
||||
|
||||
MANUAL ROLLBACK REQUIRED:
|
||||
kubectl rollout undo deployment/${APP_NAME} -n ${NAMESPACE}
|
||||
"""
|
||||
}
|
||||
|
||||
} else {
|
||||
echo "❌ Pipeline failed! (Rollback disabled or not main branch)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
always {
|
||||
sh """
|
||||
docker rmi ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG} || true
|
||||
docker rmi ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:latest || true
|
||||
docker stop test-${BUILD_NUMBER} 2>/dev/null || true
|
||||
docker rm test-${BUILD_NUMBER} 2>/dev/null || true
|
||||
"""
|
||||
cleanWs()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user