1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| import jenkins.model.*
pipeline { agent any
tools { jdk 'jdk8' }
options { timestamps() disableConcurrentBuilds() buildDiscarder(logRotator(numToKeepStr: "5")) } parameters { string( defaultValue: '/usr/local/maven/apache-maven-3.8.1', description: ''' maven 目录 ''', name: 'maven_home', trim: true ) string( defaultValue: '', description: ''' 发布的机器ip列表 ''', name: 'remote_ip_list_str', trim: true ) string( defaultValue: '*/test', description: ''' git 分支 ''', name: 'git_branches', trim: true ) string( defaultValue: '20', description: ''' git_time_out ''', name: 'git_time_out', trim: true ) string( defaultValue: '', description: ''' git 仓库地址 ''', name: 'git_url', trim: true ) credentials( credentialType: 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey', defaultValue: 'gitlab_jenkins', description: ''' ssh 凭证 ''', name: 'credentials_id', required: true ) booleanParam( defaultValue: false, description: '是否启用测试覆盖率', name: 'is_enable_jacoco' ) credentials( credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl', defaultValue: 'server-user-admin', description: ''' 服务器登陆账户 ''', name: 'server_user', required: true )
}
stages { stage('打印环境变量') { steps { println("=========开始打印环境变量=========") sh "printenv" } }
stage('拉取git代码') { steps { script { def branches = params.git_branches def gitTimeOut = params.git_time_out def credentialsId = params.credentials_id def gitUrl = params.git_url println("========开始拉取git代码=======") println(gitUrl) checkout scmGit(branches: [[name: branches]], extensions: [cloneOption(noTags: false, reference: '', shallow: true, timeout: gitTimeOut)], userRemoteConfigs: [[credentialsId: credentialsId, url: gitUrl]])
}
} } stage('maven编译') { steps {
script { println("========开始maven编译=======") sh """ pwd ${params.maven_home}/bin/mvn clean install -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true -U -B -f ${env.WORKSPACE}/pom.xml """ }
} }
stage('部署代码') { steps { script {
withCredentials([usernamePassword(credentialsId: params.server_user, passwordVariable: 'upwd', usernameVariable: 'uname')]) { def remote = [:] def serveripList = params.remote_ip_list_str.split(",") for (ip in serveripList) { remote.password = upwd remote.user = uname remote.host = ip remote.name = ip remote.allowAnyHosts = true
println(remote) sshPut remote: remote, from: 'test.jar', into: '/home/target/jenkins' sshScript remote: remote, script: '/home/back/java_start.sh'
while (true) { println("=== 开始健康检查===") try { def response = sh( script: """ curl -X GET "http://${ip}:8191/test/health/check" """, returnStdout: true ).trim() if (response.contains("SUCCESS")) { break } println(" 健康检查失败 sleep 10 s") sleep(10) } catch (exec) { println(" 健康检查失败 ${exec} sleep 10 s ") sleep(10) } } println("====== 健康检查通过 =====")
} }
}
}
}
stage('测试覆盖率统计'){ when { expression { return params.is_enable_jacoco } } steps { script{ def serveripList = params.remote_ip_list_str.split(",") def ip=serveripList[0] println (" === 统计单元测试覆盖率 ====") sh """ ${params.maven_home}/bin/mvn org.jacoco:jacoco-maven-plugin:0.8.4:dump -Djacoco.address=${ip} -Djacoco.port=6300 """ }
println (" === 生成测试覆盖率html报告 ====") jacoco() } }
} }
|