Jenkinsfile 构建示例

Jenkinsfile 构建示例

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'pipeline {
//在任何可用的代理上执行Pipeline
//
agent {
node {
label "PTtest"
}
}
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
disableConcurrentBuilds()
}
tools {
jdk 'Oracle JDK 8'
maven 'Maven 3'
}
environment {
def serviceName = ""
// hosts must be splited by dot
def warFilePath = 'mywo-crud-provider/target/mywo-crud-provider.war'
def hosts = "10.41.118.137"
def deploy_user = "app-deploy"
def artifactsPath = "devops-artifacts"
def department = "meiwozufang"
def artifactOssPath = "${ department }/${ env.JOB_NAME }"
def tempFolder = "${ WORKSPACE }/${ env.JOB_NAME }/${ env.BUILD_NUMBER }"
def target_dir = "/data/services/tomcat-mywo-crud-8090"
def git_commit_id = ""
def artifact = ""
def jmeterHome = "/home/jenkins/apache-jmeter-3.1"
def antHome = "/home/jenkins/apache-ant-1.9.9"

}
stages {
stage('Preparation') {
steps {
sh "rm -rf /tmp/${ env.JOB_NAME }"
}
}
stage('Checkout code') {
steps {
echo "starting fetchCode......"
checkout scm
}
}
stage('SonarQube analysis') {
steps {
echo "starting codeAnalyze......"
script {
echo "starting script......"
def sonarqubeScannerHome = tool name: 'SonarQube Scanner'
withSonarQubeEnv('SonarQube-Public') {
sh "${sonarqubeScannerHome}/bin/sonar-scanner"
}
}
}
}
// stage("SonarQube Quality Gate") {
// when {
// branch fuck
// }
// steps {
// echo "starting waitForQualityGate......"
// script {
// echo "starting script......"
// timeout(time: 1, unit: 'HOURS') {
// def qg = waitForQualityGate()
// if (qg.status != 'OK') {
// error "Pipeline aborted due to quality gate failure: ${qg.status}"
// }
// }
// }
// }
// }

stage('Compile branch master') {
when {
branch 'master'
}
steps {
withMaven(globalMavenSettingsConfig: 'bfa90212-2742-4611-aa71-8e0ec76d0c28', jdk: 'Oracle JDK 8', maven: 'Maven 3', mavenSettingsConfig: '02e95d2b-6bb6-457d-943f-13c63095e500') {
sh "mvn -Dmaven.test.skip=true clean package -U -DskipTests=True"
sh "echo this is master branch."
}
}
}
stage('Compile branch test') {
when {
branch 'test'
}
steps {
withMaven(globalMavenSettingsConfig: 'bfa90212-2742-4611-aa71-8e0ec76d0c28', jdk: 'Oracle JDK 8', maven: 'Maven 3', mavenSettingsConfig: '02e95d2b-6bb6-457d-943f-13c63095e500') {
sh "mvn -Dmaven.test.skip=true clean package -U -DskipTests=True"
sh "echo this is test branch."
}
}
}
stage('Package & Upload to OSS') {
steps {
sh "mkdir -p ${ tempFolder }"
script {
sh "echo Upload to OSS."
git_commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() //需要获取shell返回值操作
serviceName = sh(returnStdout: true, script: "basename ${ warFilePath } | cut -d . -f 1").trim()
sh "mv ${ warFilePath } ${ tempFolder }/ROOT.war"
artifact = "${ serviceName }_${ env.BUILD_NUMBER }-${ env.BRANCH_NAME }-${ git_commit_id }.tar.gz"
sh "mkdir -p ${ artifactsPath } && tar czf ${ artifactsPath }/${ artifact } -C ${ tempFolder } ROOT.war"
sh "upload-flash -f ${ artifactsPath }/${ artifact } -t ${ artifactOssPath }"
}
}
}
stage('Deploy Package') {
when {
branch 'test'
}
steps {
script {
echo "starting deploy......"
// 运维人员维护私有仓库,保存ansible-playbook的配置文件
git([url: 'git@git.shuke.com:shuke/ansiblefiles.git', branch: 'master', credentialsId: '7bc6cda9-a250-4d99-962c-e2ede002db06'])
withDockerRegistry([credentialsId: '82a6a986-3f85-49f9-a926-0edf499fd1e3', url: 'https://dockerhub.shuke.com:5000']) {
withDockerContainer([image: 'dockerhub.shuke.com:5000/test/ansible_deploy:1.0.4']) {
sh "ansible-playbook -i hosts --extra-vars 'warFilePath=${ tempFolder }/ROOT.war target_dir=${ target_dir }' -vv deploy_war.yml -l ${ hosts }"
}
}
}
}
}
stage('Integration Test') {
when {
branch 'test'
}
parallel {
stage('API Test') {
steps {
echo "starting API......"
echo "starting fetchAPI......"
dir('API') {
echo "starting APIScript......"
git([url: 'git@git.shuke.com:api_test/RentHouse.git', branch: 'master', credentialsId: '7726e85b-f455-40d1-b12c-447ecac70f23'])
script {
try {
withDockerRegistry([credentialsId: '82a6a986-3f85-49f9-a926-0edf499fd1e3', url: 'https://dockerhub.shuke.com:5000']) {
withDockerContainer([image: 'dockerhub.shuke.com:5000/test/robotframework:1.0.1']) {
sh "pybot RentHouse_API_Test/test"
}
}
}catch (exc) {
echo 'Something failed, I should sound the klaxons!'
emailContent = "API执行过程中报错了!!"
}finally{
sh "cp -r ./output.xml /home/jenkins/public/outputRentHouse_API_Prduction.xml"
}
}
}
}
}
// stage('UI') {
// steps {
// echo "starting UI......"
// // 测试闪银UI
// dir('UI') {
// git([url: 'git@git.shuke.com:ui_test/Wecash_RentalHouse.git', branch: 'master', credentialsId: '7726e85b-f455-40d1-b12c-447ecac70f23'])
// script {
// echo "starting UIScript......"
// try {
// //sh "cd /gg"
// echo "starting UIScript......"
// // 执行速度太慢,先注释代码
// withDockerRegistry([credentialsId: '82a6a986-3f85-49f9-a926-0edf499fd1e3', url: 'https://dockerhub.shuke.com:5000']) {
// withDockerContainer([image: 'dockerhub.shuke.com:5000/test/python-chromedriver:2.7-alpine3.7-selenium']) {
// sh "python TestSuite/Runner.py All All business-renthouse"
// }
// }
// sh "/usr/bin/python TestSuite/Runner.py All All business-renthouse"
// }catch (exc) {
// echo 'Something failed, I should sound the klaxons!'
// emailContent = "UI执行过程中报错了!!"
// }
// }
// }
// }
// }
stage('PT') {
steps {
echo "starting PT......"
dir('PT') {
git credentialsId: '7726e85b-f455-40d1-b12c-447ecac70f23', url: 'git@git.shuke.com:p_test/MeiWo.git'
}
dir('Tools') {
script {
echo "starting PTScript......"
git([url: 'git@git.shuke.com:gonghui/script.git', branch: 'master', credentialsId: '7726e85b-f455-40d1-b12c-447ecac70f23'])
try {
withEnv(["JAVA_HOME=${tool 'Oracle JDK 8'}"]) {
sh "${antHome}/bin/ant -f ${ WORKSPACE }/Tools/PerformFilesWithPipline/build.xml -Dname=MeiWo -Denv=Test -DJOB_NAME=${JOB_NAME} -DWORKSPACE=${WORKSPACE} -DjmeterHome=${jmeterHome} run"
}
echo "starting PTAnalyze......"
sh "python ${ WORKSPACE }/Tools/PerformFilesWithPipline/jmeterQYWeiXinWithTimeoutArgu.py ${ WORKSPACE }/PT/results/ MeiWo 2 5 500 4000"
sh "cp -r ${ WORKSPACE }/PT/results/ParseOri.html /home/jenkins/public/"
}catch (exc) {
echo 'PT执行报错了!!'
emailContent = "PT执行过程中报错了!!"
}
}
}
}
}
}
}
stage('conformReport&noticify') {
when {
branch 'test'
}
steps {
echo "starting email......"

dir('conformresult') {
script{
git([url: 'git@git.shuke.com:gonghui/script.git', branch: 'master', credentialsId: '7726e85b-f455-40d1-b12c-447ecac70f23'])
withDockerRegistry([credentialsId: '82a6a986-3f85-49f9-a926-0edf499fd1e3', url: 'https://dockerhub.shuke.com:5000']) {
withDockerContainer([image: 'dockerhub.shuke.com:5000/test/python3.6.5:1.0.0',args: '-v /data/docker/jenkins-slave-prod/public:/home/jenkins/public']) {
sh "python3 parseScript/conformReport.py /home/jenkins/public ${ email_list } ${JOB_NAME}-Build//${BUILD_ID}-"
}
}
}
}
}
}
}
post {
always {
echo 'always!'
deleteDir()
}
success {
emailext body: "Pipeline Success: Please Go to deploy if you pushed master just now",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "'${JOB_NAME}' (${BUILD_NUMBER})times SUCCESS",
to: ''
echo 'success!'
}
failure {
emailext body: "Pipeline error: Please go to ${BUILD_URL} and verify the build \n ${ emailContent }",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "'${JOB_NAME}' (${BUILD_NUMBER})times FAILED",
to: ''
echo 'failure!'
}
unstable {
echo 'unstable!'
}
changed {
echo 'changed!'
}
}
}

本文标题:Jenkinsfile 构建示例

文章作者:shuke

发布时间:2020年04月20日 - 19:04

最后更新:2020年04月20日 - 19:04

原始链接:https://shuke163.github.io/2020/04/20/Jenkinsfile-%E6%9E%84%E5%BB%BA%E7%A4%BA%E4%BE%8B/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------

本文标题:Jenkinsfile 构建示例

文章作者:shuke

发布时间:2020年04月20日 - 19:04

最后更新:2020年04月20日 - 19:04

原始链接:https://shuke163.github.io/2020/04/20/Jenkinsfile-%E6%9E%84%E5%BB%BA%E7%A4%BA%E4%BE%8B/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%