pipeline {
    agent { label 'jenkins-jnlp' }
    triggers {
        GenericTrigger(
         genericVariables: [
          [key: 'ref', value: '$.ref']
         ],
         causeString: 'Triggered on $ref',
         token: 'middleware-automatic',
         printContributedVariables: true,
         printPostContent: true,
         silentResponse: false,
         regexpFilterText: '$ref',
         regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
        )
      }
    options { buildDiscarder(logRotator(numToKeepStr: '5')) }
    stages {
        stage('build') {
            when { branch 'master' }
            steps {
                sh '/apache-maven-3.6.3/bin/mvn clean package -Dmaven.test.skip=true -U'
            }
            post {
                success {
                    sh "/usr/local/bin/workwechatctl -p ${env.JOB_NAME} -n 沈君杰 -c 项目编译成功进行下一阶段"
                }
                failure {
                    step([$class: 'Mailer',
                    notifyEveryUnstableBuild: true,
                    recipients: "shenjunjie@zhiweidata.com",
                    sendToIndividuals: true])
                }
            }
        }
        stage('Transmission Jar') {
            when { branch 'master' }
            steps {
                sh "scp middleware-automatic-center-server/target/middleware-automatic-center-server.jar nbzhiwei@192.168.0.38:/home/nbzhiwei/jar/middleware-automatic-2023"
            }
            post {
                failure {
                    step([$class: 'Mailer',
                    notifyEveryUnstableBuild: true,
                    recipients: "zhangjinxiao@zhiweidata.com",
                    sendToIndividuals: true])
                    
                    sh "/usr/local/bin/workwechatctl -p ${env.JOB_NAME} -n 张金晓 -c jar包传输失败"
                }
            }
        }
        stage('Deploy') {
            when { branch 'master' }
            steps {
                sh "supervisorctl-cli.go -active restart -host 192.168.0.38:9001 -name middleware-automatic-center-server -user 沈君杰"
            }
            post {
                success {
                    sh "/usr/local/bin/workwechatctl -p ${env.JOB_NAME} -n 沈君杰 -c 部署成功"
                }
                failure {
                    step([$class: 'Mailer',
                    notifyEveryUnstableBuild: true,
                    recipients: "zhangjinxiao@zhiweidata.com",
                    sendToIndividuals: true])
                    
                    sh "/usr/local/bin/workwechatctl -p ${env.JOB_NAME} -n 张金晓 -c 部署失败"
                }
            }
        }

    }
}

