pipeline {
    agent any
    environment {
        GITURL = 'http://101.36.160.140:21044/delivery-warehouse-java/delivery-depot-admin.git'
        //git地址
        GETBRANCH = 'master' //git 分支 变更分支注意修改这里
        CONFIGNAME = '128' //选择配置好的部署服务器名称
        SOURCEFILES = 'delivery-server/target/delivery-server.jar' //jar 包在 Jenkins 工作空间+项目名称下 的相对路径
        REMOVEPREFIX = 'delivery-server/target/' //去除前缀，只要 jar 包名称
        REMOTEDIRECTORY = '/home/delivery/jar/dev' //部署路径
        EXECCOMMAND = 'cd /home/delivery/jar/dev && ./start.sh && ps aux | grep java' //执行命令，启动项目 && ./start.sh
    }
    stages {
        stage('pull code') {
            steps {
                echo '从 gitlab 拉取代码'
                git branch: "${GETBRANCH}", credentialsId: '71f78619-368a-4880-b752-0feec5fd34e0', url: "${GITURL}"
            }
        }
        stage('build project') {
            steps {
                echo 'maven 编译项目'
                sh '''
                    echo \'++++++++++开始打包+++++++++\'
                    mvn clean package -Dmaven.test.skip=true
                    echo \'++++++++++完成打包+++++++++\'
                '''
            }
        }
        stage('publish project') {
            steps {
                echo '开始推送 jar 包到部署服务器'
                sshPublisher(publishers: [
                        sshPublisherDesc(
                                configName: "${CONFIGNAME}", // 使用双引号引用变量
                                transfers: [sshTransfer(
                                        cleanRemote: false,
                                        excludes: '',
                                        execCommand: "${EXECCOMMAND}", // 使用双引号引用变量
                                        execTimeout: 120000,
                                        flatten: false,
                                        makeEmptyDirs: false,
                                        noDefaultExcludes: false,
                                        patternSeparator: '[, ]+',
                                        remoteDirectory: "${REMOTEDIRECTORY}", // 使用双引号引用变量
                                        remoteDirectorySDF: false,
                                        removePrefix: "${REMOVEPREFIX}", // 使用双引号引用变量
                                        sourceFiles: "${SOURCEFILES}",// 使用双引号引用变量
                                        usePty: true
                                )],
                                usePromotionTimestamp: false,
                                useWorkspaceInPromotion: false,
                                verbose: false
                        )
                ])
            }
        }
    }
}
