instruction: Build a Jenkins pipeline script for a Java Spring Boot application with Maven
output: Here's a comprehensive Jenkins pipeline for a Java Spring Boot application:
  
  ```groovy
  pipeline {
   agent {
   kubernetes {
   yaml """
   apiVersion: v1
   kind: Pod
   spec:
   containers:
   - name: maven
   image: maven:3.8.4-openjdk-17
   command:
   - cat
   tty: true
   volumeMounts:
   - name: docker-sock
   mountPath: /var/run/docker.sock
   - name: docker
   image: docker:20.10.12
   command:
   - cat
   tty: true
   volumeMounts:
   - name: docker-sock
   mountPath: /var/run/docker.sock
   - name: kubectl
   image: bitnami/kubectl:latest
   command:
   - cat
   tty: true
   volumes:
   - name: docker-sock
   hostPath:
   path: /var/run/docker.sock
   """
   }
   }
   
   environment {
   DOCKER_REGISTRY = 'your-registry.com'
   IMAGE_NAME = 'spring-boot-app'
   MAVEN_OPTS = '-Dmaven.repo.local=.m2/repository'
   SONAR_TOKEN = credentials('sonar-token')
   DOCKER_CREDENTIALS = credentials('docker-registry-creds')
   KUBECONFIG = credentials('kubeconfig')
   }
   
   parameters {
   choice(
   name: 'ENVIRONMENT',
   choices: ['dev', 'staging', 'production'],
   description: 'Target deployment environment'
   )
   booleanParam(
   name: 'SKIP_TESTS',
   defaultValue: false,
   description: 'Skip running tests'
   )
   booleanParam(
   name: 'DEPLOY',
   defaultValue: true,
   description: 'Deploy to target environment'
   )
   }
   
   stages {
   stage('Checkout') {
   steps {
   script {
   // Clean workspace and checkout code
   deleteDir()
   checkout scm
   
   // Set build information
   env.GIT_COMMIT_SHORT = sh(
   script: "git rev-parse --short HEAD",
   returnStdout: true
   ).trim()
   
   env.BUILD_VERSION = "${env.BUILD_NUMBER}-${env.GIT_COMMIT_SHORT}"
   env.IMAGE_TAG = "${env.BUILD_VERSION}"
   
   currentBuild.displayName = "#${env.BUILD_NUMBER} - ${env.GIT_COMMIT_SHORT}"
   currentBuild.description = "Branch: ${env.BRANCH_NAME}, Environment: ${params.ENVIRONMENT}"
   }
   }
   }
   
   stage('Build & Test') {
   parallel {
   stage('Maven Build') {
   steps {
   container('maven') {
   sh '''
   echo "Building Spring Boot application..."
   mvn clean compile -B -e
   '''
   }
   }
   }
   
   stage('Unit Tests') {
   when {
   not { params.SKIP_TESTS }
   }
   steps {
   container('maven') {
   sh '''
   echo "Running unit tests..."
   mvn test -B -e
   '''
   }
   }
   post {
   always {
   publishTestResults(
   testResultsPattern: 'target/surefire-reports/*.xml',
   allowEmptyResults: true
   )
   }
   }
   }
   }
   }
   
   stage('Code Quality') {
   parallel {
   stage('SonarQube Analysis') {
   when {
   anyOf {
   branch 'main'
   branch 'develop'
   changeRequest()
   }
   }
   steps {
   container('maven') {
   withSonarQubeEnv('SonarQube') {
   sh '''
   mvn sonar:sonar \
   -Dsonar.projectKey=spring-boot-app \
   -Dsonar.host.url=${SONAR_HOST_URL} \
   -Dsonar.login=${SONAR_TOKEN}
   '''
   }
   }
   }
   }
   
   stage('Security Scan') {
   steps {
   container('maven') {
   sh '''
   echo "Running OWASP dependency check..."
   mvn org.owasp:dependency-check-maven:check \
   -DfailBuildOnCVSS=7 \
   -DsuppressionFiles=owasp-suppressions.xml
   '''
   }
   }
   post {
   always {
   publishHTML([
   allowMissing: false,
   alwaysLinkToLastBuild: true,
   keepAll: true,
   reportDir: 'target',
   reportFiles: 'dependency-check-report.html',
   reportName: 'OWASP Dependency Check Report'
   ])
   }
   }
   }
   }
   }
   
   stage('Integration Tests') {
   when {
   allOf {
   not { params.SKIP_TESTS }
   anyOf {
   branch 'main'
   branch 'develop'
   }
   }
   }
   steps {
   container('maven') {
   sh '''
   echo "Running integration tests..."
   mvn verify -P integration-tests -B -e
   '''
   }
   }
   post {
   always {
   publishTestResults(
   testResultsPattern: 'target/failsafe-reports/*.xml',
   allowEmptyResults: true
   )
   }
   }
   }
   
   stage('Package Application') {
   steps {
   container('maven') {
   sh '''
   echo "Packaging application..."
   mvn package -DskipTests -B -e
   
   # Create build info
   echo "Build Number: ${BUILD_NUMBER}" > target/build-info.txt
   echo "Git Commit: ${GIT_COMMIT}" >> target/build-info.txt
   echo "Build Date: $(date)" >> target/build-info.txt
   echo "Branch: ${BRANCH_NAME}" >> target/build-info.txt
   '''
   }
   }
   post {
   success {
   archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
   archiveArtifacts artifacts: 'target/build-info.txt', fingerprint: true
   }
   }
   }
   
   stage('Build Docker Image') {
   steps {
   container('docker') {
   script {
   // Build Docker image
   sh '''
   echo "Building Docker image..."
   docker build \
   --build-arg JAR_FILE=target/*.jar \
   --build-arg BUILD_VERSION=${BUILD_VERSION} \
   -t ${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG} \
   -t ${DOCKER_REGISTRY}/${IMAGE_NAME}:latest .
   '''
   
   // Run security scan on image
   sh '''
   echo "Scanning Docker image for vulnerabilities..."
   docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
   aquasec/trivy:latest image \
   --exit-code 1 \
   --severity HIGH,CRITICAL \
   ${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
   '''
   }
   }
   }
   }
   
   stage('Push Docker Image') {
   when {
   anyOf {
   branch 'main'
   branch 'develop'
   expression { params.DEPLOY }
   }
   }
   steps {
   container('docker') {
   script {
   docker.withRegistry("https://${DOCKER_REGISTRY}", env.DOCKER_CREDENTIALS) {
   sh '''
   echo "Pushing Docker image..."
   docker push ${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
   docker push ${DOCKER_REGISTRY}/${IMAGE_NAME}:latest
   '''
   }
   }
   }
   }
   }
   
   stage('Deploy to Environment') {
   when {
   expression { params.DEPLOY }
   }
   steps {
   container('kubectl') {
   script {
   def namespace = params.ENVIRONMENT
   def replicas = params.ENVIRONMENT == 'production' ? 3 : 1
   
   sh '''
   echo "Deploying to ${ENVIRONMENT} environment..."
   
   # Update deployment manifest
   sed -i "s/IMAGE_TAG_PLACEHOLDER/${IMAGE_TAG}/g" k8s/deployment.yaml
   sed -i "s/NAMESPACE_PLACEHOLDER/${ENVIRONMENT}/g" k8s/deployment.yaml
   sed -i "s/REPLICAS_PLACEHOLDER/${replicas}/g" k8s/deployment.yaml
   
   # Apply Kubernetes manifests
   kubectl apply -f k8s/namespace.yaml
   kubectl apply -f k8s/configmap.yaml -n ${ENVIRONMENT}
   kubectl apply -f k8s/secret.yaml -n ${ENVIRONMENT}
   kubectl apply -f k8s/deployment.yaml -n ${ENVIRONMENT}
   kubectl apply -f k8s/service.yaml -n ${ENVIRONMENT}
   kubectl apply -f k8s/ingress.yaml -n ${ENVIRONMENT}
   
   # Wait for rollout
   kubectl rollout status deployment/spring-boot-app -n ${ENVIRONMENT} --timeout=300s
   
   # Verify deployment
   kubectl get pods -n ${ENVIRONMENT} -l app=spring-boot-app
   '''
   }
   }
   }
   }
   
   stage('Health Check') {
   when {
   expression { params.DEPLOY }
   }
   steps {
   script {
   def healthCheckUrl = getHealthCheckUrl(params.ENVIRONMENT)
   
   retry(5) {
   sleep(10)
   sh '''
   echo "Performing health check..."
   curl -f ${healthCheckUrl}/actuator/health || exit 1
   
   echo "Checking application info..."
   curl -f ${healthCheckUrl}/actuator/info || exit 1
   '''
   }
   }
   }
   }
   
   stage('Performance Tests') {
   when {
   allOf {
   expression { params.DEPLOY }
   anyOf {
   branch 'main'
   expression { params.ENVIRONMENT == 'staging' }
   }
   }
   }
   steps {
   container('maven') {
   sh '''
   echo "Running performance tests..."
   mvn gatling:test -P performance-tests
   '''
   }
   }
   post {
   always {
   publishHTML([
   allowMissing: false,
   alwaysLinkToLastBuild: true,
   keepAll: true,
   reportDir: 'target/gatling',
   reportFiles: 'index.html',
   reportName: 'Performance Test Report'
   ])
   }
   }
   }
   }
   
   post {
   always {
   // Clean up Docker images
   script {
   if (env.IMAGE_TAG) {
   sh '''
   docker rmi ${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG} || true
   docker rmi ${DOCKER_REGISTRY}/${IMAGE_NAME}:latest || true
   '''
   }
   }
   
   // Archive logs
   archiveArtifacts artifacts: 'target/logs/**/*.log', allowEmptyArchive: true
   
   // Publish coverage reports
   publishCoverage adapters: [jacocoAdapter('target/site/jacoco/jacoco.xml')]
   }
   
   success {
   script {
   if (params.DEPLOY && params.ENVIRONMENT == 'production') {
   // Create Git tag for production deployments
   sh '''
   git tag -a v${BUILD_VERSION} -m "Release version ${BUILD_VERSION}"
   git push origin v${BUILD_VERSION}
   '''
   }
   }
   
   // Send success notification
   slackSend(
   channel: '#deployments',
   color: 'good',
   message: ":white_check_mark: *SUCCESS* - ${env.JOB_NAME} - Build #${env.BUILD_NUMBER}\n" +
   "Branch: ${env.BRANCH_NAME}\n" +
   "Environment: ${params.ENVIRONMENT}\n" +
   "Image: ${env.DOCKER_REGISTRY}/${env.IMAGE_NAME}:${env.IMAGE_TAG}"
   )
   }
   
   failure {
   // Send failure notification
   slackSend(
   channel: '#deployments',
   color: 'danger',
   message: ":x: *FAILED* - ${env.JOB_NAME} - Build #${env.BUILD_NUMBER}\n" +
   "Branch: ${env.BRANCH_NAME}\n" +
   "Environment: ${params.ENVIRONMENT}\n" +
   "Build URL: ${env.BUILD_URL}"
   )
   
   // Send email to committers
   emailext(
   to: '${DEFAULT_RECIPIENTS}',
   subject: 'Build Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER}',
   body: '''Build failed for ${env.JOB_NAME} - ${env.BUILD_NUMBER}
   
   Branch: ${env.BRANCH_NAME}
   Commit: ${env.GIT_COMMIT}
   
   Build URL: ${env.BUILD_URL}
   Console Output: ${env.BUILD_URL}console
   
   Please check the build logs for details.'''
   )
   }
   
   unstable {
   slackSend(
   channel: '#deployments',
   color: 'warning',
   message: ":warning: *UNSTABLE* - ${env.JOB_NAME} - Build #${env.BUILD_NUMBER}\n" +
   "Branch: ${env.BRANCH_NAME}\n" +
   "Some tests may have failed."
   )
   }
   }
  }
  
  // Helper function to get health check URL
  def getHealthCheckUrl(environment) {
   switch(environment) {
   case 'dev':
   return 'https://dev-api.yourcompany.com'
   case 'staging':
   return 'https://staging-api.yourcompany.com'
   case 'production':
   return 'https://api.yourcompany.com'
   default:
   return 'http://localhost:8080'
   }
  }
  ```
  
  This Jenkins pipeline includes:
  - Multi-stage build with parallel execution
  - Comprehensive testing (unit, integration, performance)
  - Code quality analysis with SonarQube
  - Security scanning with OWASP and Trivy
  - Docker image building and pushing
  - Kubernetes deployment with health checks
  - Environment-specific configurations
  - Notification system with Slack and email
  - Artifact archiving and test reporting