pipeline {
    agent any 
    environment {
        // 设置自定义JDK路径，因为每个项目可能需要不同版本的jdk
        JAVA_HOME = '/usr/lib/jvm/jdk1.8.0_381'
        JOBNAME="${JOB_NAME}"
        JENKINS_NODE_COOKIE = "dontKillMe"
        #SPRING_PROFILES_ACTIVE = "dev"
    }
    stages {
        stage('Check out') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'f7070987-9b49-4ae1-af6d-53a3bf26799b', passwordVariable: 'Max@2023', usernameVariable: 'bonus')]) {
                    checkout([
                        $class: 'GitSCM',
                        branches: [[name: '*/master']],
                        doGenerateSubmoduleConfigurations: false,
                        extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '']],
                        submoduleCfg: [],
                        userRemoteConfigs: [[url: 'http://192.168.0.244:3000/bonus/Bonus-Cloud-Smart-Canteen.git']]
                    ])
                }
            }
        }
        stage('Move file') {
             steps {
                echo 'move file start!'
                sh 'chmod 777 ${WORKSPACE}/scripts/*'
                // 如果需要拷贝文件，打开并编写以下脚本
                // sh '${WORKSPACE}/scripts/moveFile.sh'
            }
        }
        stage('Build') {
            steps {
                echo 'build start!'
                sh 'env JAVA_HOME=$JAVA_HOME  /usr/local/src/apache-maven-3.9.4/bin/mvn  clean package'
            }
        }
        stage('Sonar scan') {
            steps {
                script {
                    echo 'sonar scan start!'
                    def scannerHome = tool 'sonar-scanner'
                    withSonarQubeEnv('sonarqube-server') {
                        sh "/usr/local/sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner " +
                           "-Dsonar.projectKey=${JOB_NAME} " +
                           "-Dsonar.projectName=${JOB_NAME} " +
                           "-Dsonar.sourceEncoding=UTF-8 " +
                           "-Dsonar.language=java " +
                           "-Dsonar.projectVersion=1.0 " +
                           "-Dsonar.sources=. " +
                           "-Dsonar.java.source=1.8 " +
                           "-Dsonar.java.target=1.8 " +
                           "-Dsonar.scm.disabled=true " +
                           "-Dsonar.exclusions=test/**,node_modules/**,static/**,**/*.js,**/*.html,**/*.css,**/*.jar " +
                           "-Dsonar.java.binaries=**/target/classes"
                    }
                }
            }
		    }
        stage('Deploy') {
              steps {
                  echo 'deploy start!'
                  sh '''BUILD_ID=DONTKILLME  sh ${WORKSPACE}/scripts/deploy.sh'''
              }
        }
        stage('Dingding') {
              steps {
                  echo 'Dingding start!'
                  script {
                      def webhookUrl = 'https://oapi.dingtalk.com/robot/send?access_token=eae0af98514a0db9a9a0d5cd8e337d13e7c16cd81d4a90b503e465a573020add' //替换为你自己的钉钉机器人Webhook地址
                      def message = "${JOB_NAME} TEST已完成: ${env.BUILD_URL}  \n 到Sonarqube查看问题：http://192.168.0.244:9000/dashboard?id=${JOB_NAME} \n 查看新包： http://192.168.0.244/${JOB_NAME}/${BUILD_NUMBER}    "
                      echo "${message}"
                      sh "curl -H 'Content-Type: application/json' -d '{\"msgtype\": \"text\", \"text\": {\"content\": \"${message}\"}, \"at\": {\"atMobiles\": [\"张三，李四\"], \"isAtAll\": true}}' ${webhookUrl}"
                   }
              }
        }
    }
}

