168 lines
6.0 KiB
Groovy
168 lines
6.0 KiB
Groovy
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}"
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout Source') {
|
|
steps {
|
|
echo "Checking out application source code..."
|
|
|
|
sh '''
|
|
cat > Dockerfile << 'EOF'
|
|
FROM nginx:1.25.3-alpine
|
|
RUN echo "<html><body><h1>Demo Nginx - Build ${BUILD_NUMBER}</h1><p>Environment: Production</p><p>Version: ${IMAGE_TAG}</p></body></html>" > /usr/share/nginx/html/index.html
|
|
COPY nginx.conf /etc/nginx/nginx.conf
|
|
EXPOSE 80
|
|
CMD ["nginx", "-g", "daemon off;"]
|
|
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"
|
|
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('Verify Deployment') {
|
|
when { branch 'main' }
|
|
steps {
|
|
script {
|
|
echo "Verifying deployment..."
|
|
sh """
|
|
sleep 30
|
|
kubectl rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=300s || true
|
|
kubectl get pods -n ${NAMESPACE} -l app=${APP_NAME}
|
|
"""
|
|
echo "✅ Deployment completed!"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo """
|
|
✅ Pipeline SUCCESS!
|
|
Image: ${DOCKER_REGISTRY}/${DOCKER_REPO}/${APP_NAME}:${IMAGE_TAG}
|
|
Namespace: ${NAMESPACE}
|
|
"""
|
|
}
|
|
failure {
|
|
echo "❌ Pipeline failed!"
|
|
}
|
|
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()
|
|
}
|
|
}
|
|
}
|