pipeline { agent any tools { jdk 'jdk17' maven 'maven3' } parameters { choice(name: 'DEPLOY_ENV', choices: ['blue', 'green'], description: 'Choose which environment to deploy: Blue or Green') choice(name: 'DOCKER_TAG', choices: ['blue', 'green'], description: 'Choose the Docker image tag for the deployment') booleanParam(name: 'SWITCH_TRAFFIC', defaultValue: false, description: 'Switch traffic between Blue and Green') } environment { IMAGE_NAME = "aziz27uk/bankapp" TAG = "${params.DOCKER_TAG}" // The image tag now comes from the parameter KUBE_NAMESPACE = 'webapps' SCANNER_HOME= tool 'sonar-scanner' } stages { stage('Git Checkout') { steps { git branch:'main', url: 'https://github.com/AbdulAziz-uk/Blue-Green-Deployment_Java_Practice.git' } } stage ('compile') { steps { sh 'mvn clean package -DskipTests' } } stage('SonarQube Analysis') { steps { withSonarQubeEnv('sonar') { sh "$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectKey=nodejsmysql -Dsonar.projectName=nodejsmysql -Dsonar.java.binaries=target" } } } stage('Trivy FS Scan') { steps { sh "trivy fs --format table -o fs.html ." } } stage('Docker build') { steps { script { withDockerRegistry(credentialsId: 'docker-cred') { sh "docker build -t ${IMAGE_NAME}:${TAG} ." } } } } stage('Trivy Image Scan') { steps { sh "trivy image --format table -o image.html ${IMAGE_NAME}:${TAG}" } } stage('Docker Push Image') { steps { script { withDockerRegistry(credentialsId: 'docker-cred') { sh "docker push ${IMAGE_NAME}:${TAG}" } } } } stage('Deploy MySQL Deployment and Service') { steps { script { withKubeConfig(caCertificate: '', clusterName: 'star-cluster', contextName: '', credentialsId: 'k8-token', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://389F23FF01C0305689C5DE1A178CE20A.sk1.eu-west-2.eks.amazonaws.com') { sh "kubectl apply -f mysql-ds.yml -n ${KUBE_NAMESPACE}" // Ensure you have the MySQL deployment YAML ready } } } } stage('Deploy SVC-APP') { steps { script { withKubeConfig(caCertificate: '', clusterName: 'star-cluster', contextName: '', credentialsId: 'k8-token', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://389F23FF01C0305689C5DE1A178CE20A.sk1.eu-west-2.eks.amazonaws.com') { sh """ if ! kubectl get svc bankapp-service -n ${KUBE_NAMESPACE}; then kubectl apply -f bankapp-service.yml -n ${KUBE_NAMESPACE} fi """ } } } } stage('Deploy to Kubernetes') { steps { script { def deploymentFile = "" if (params.DEPLOY_ENV == 'blue') { deploymentFile = 'app-deployment-blue.yml' } else { deploymentFile = 'app-deployment-green.yml' } withKubeConfig(caCertificate: '', clusterName: 'star-cluster', contextName: '', credentialsId: 'k8-token', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://389F23FF01C0305689C5DE1A178CE20A.sk1.eu-west-2.eks.amazonaws.com') { sh "kubectl apply -f ${deploymentFile} -n ${KUBE_NAMESPACE}" } } } } stage('Switch Traffic Between Blue & Green Environment') { when { expression { return params.SWITCH_TRAFFIC } } steps { script { def newEnv = params.DEPLOY_ENV // Always switch traffic based on DEPLOY_ENV withKubeConfig(caCertificate: '', clusterName: 'stark-cluster', contextName: '', credentialsId: 'k8-token', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://389F23FF01C0305689C5DE1A178CE20A.sk1.eu-west-2.eks.amazonaws.com') { sh ''' kubectl patch service bankapp-service -p "{\\"spec\\": {\\"selector\\": {\\"app\\": \\"bankapp\\", \\"version\\": \\"''' + newEnv + '''\\"}}}" -n ${KUBE_NAMESPACE} ''' } echo "Traffic has been switched to the ${newEnv} environment." } } } stage('Verify Deployment') { steps { script { def verifyEnv = params.DEPLOY_ENV withKubeConfig(caCertificate: '', clusterName: 'star-cluster', contextName: '', credentialsId: 'k8-token', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://389F23FF01C0305689C5DE1A178CE20A.sk1.eu-west-2.eks.amazonaws.com') { sh """ kubectl get pods -l version=${verifyEnv} -n ${KUBE_NAMESPACE} kubectl get svc bankapp-service -n ${KUBE_NAMESPACE} """ } } } } } }