forked from sftfjugg/vAdmin-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
277 lines (242 loc) · 9.75 KB
/
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
env.PROJ_DIR="${JENKINS_HOME}/workspace" //jenkins workspace
env.PROJ_DIR_HOSTPATH="/opt/data/jenkinsdata/workspace"
env.PROJ_URL="http://xx.xx.xx:7166/vue/vAdmin-web.git" // 项目的http地址
//env.PROJ_NAME="*********" // 项目名
env.LANGUAGE="node" //基础镜像
env.TAGS="14.16.0-alpine" //基础镜像TAG
env.HARBOR="registry.cn-beijing.aliyuncs.com/k8s22333" //镜像仓库的URL
env.INAME="vadmin-web" //制作的镜像名(自定义时不要使用大写字母)
// String tag='latest' //制作的镜像TAG
node {
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
def email = env.BUILD_USER_EMAIL
println user
}
//def env = ["JAVA_HOME=${tool 'jdk1.8.0_131'}", "PATH+MAVEN=${tool 'maven_3.1.1'}/bin:${env.JAVA_HOME}/bin", "PATH+GRADLE=${tool 'gradle_4.1'}/bin:${env.JAVA_HOME}/bin" ]
//def err = null
//currentBuild.result = "SUCCESS"
}
def InPutCheckBranch(){
timeout(time:10, unit:'SECONDS') {
INPUT = input message: "两次输入不一致! 请重新输入你想构建的代码分支",
ok: "do it",
// submitter: "zhahuangbo",
parameters: [
string(name: 'branch', defaultValue: '', description: 'input your branch'),
string(name: 'branch_confirm', defaultValue: '', description: 'input your branch again'),
]
}
env.branch="${INPUT.branch}"
}
class MyChange {
String author;
String msg;
}
@NonCPS
def GetChanges(){
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++)
{
def entries = changeLogSets[0].items
for (int j = 0; j < entries.length; j++)
{
def entry = entries[0]
def change = new MyChange()
change.author = entry.author
change.msg = entry.msg
return change
}
}
}
pipeline {
agent any
//triggers {
// cron('H 4/* 0 0 1-5')
//}
//tools {
//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。
// maven 'apache-maven-3.0.1'
//}
environment {
_workspace = "${env.WORKSPACE}"
_projectName = "${env.JOB_NAME}"
_BUILD_NUMBER = "${env.BUILD_NUMBER}"
//_ScannerMsBuildHome = "C:\\Users\\htsd\\Downloads\\sonar-scanner-msbuild-4.6.1.2049-net46"
//_MSBuildHome = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\amd64"
}
stages {
stage('Example') {
steps {
echo 'Hello World'
script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
}
}
stage('Get code') {
steps {
checkout([ // git repo
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: './']],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'vugitlab',
url: 'http://xx.xx.xx:7166/vue/vAdmin-web.git'
]]
])
}
}
stage('Get commit_msg') {
steps {
script {
env.commitmsg = sh(script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
env.tagfull = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
env.tag = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.GIT_COMMIT_EMAIL = sh(script: "git --no-pager show -s --format=\'%ae\'", returnStdout: true).trim()
env.BUILD_FULL = sh (script: "git log -1 --pretty=%B | grep '\\[jenkins-full]'",returnStatus: true) == 0
}
}
}
stage('Sonar scan') {
steps {
// groovy 脚本
sh "echo ${WORKSPACE} "
sh "echo ${JOB_NAME} "
//sh "echo ${deploy_step} "
//sh "echo ${env.JOB_NAME} "
// groovy 脚本
script {
switch(SONAR_SCAN) {
case "TRUE":
sh "echo \u001B[31m不扫描\u001B[0m !!!"
default:
sh "echo 扫描"
sh '''/var/jenkins_home/sonar-scanner-4.6.0.2311/bin/sonar-scanner \
-Dsonar.projectKey=vadmin-web \
-Dsonar.sources=. \
-Dsonar.host.url=http://xx.xx.xx:9000 \
-Dsonar.login=admin \
-Dsonar.password=sftf0423@'''
}
}
}
}
stage('Docker Build') { //构建镜像,ssh到远程主机执行命令,该命令要用"",Dodckerfile顶格
steps {
withEnv(["IMAGE_TAG=${tag}"]) {
sh '''
ssh -i /var/jenkins_home/id_rsa [email protected] "cd $PROJ_DIR_HOSTPATH/$JOB_NAME/;
cat << EOF > Dockerfile
FROM ${LANGUAGE}:${TAGS} as builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add python2 make gcc
RUN mkdir /tmp/$JOB_NAME
COPY . /tmp/$JOB_NAME/
#RUN npm i node-sass -s -g cnpm --unsafe-perm --registry=http://registry.npm.taobao.org/
RUN npm install -g cnpm --registry=http://registry.npm.taobao.org/
RUN npm config set registry http://registry.npm.taobao.org
RUN cd /tmp/$JOB_NAME && cnpm install --save vue-fontawesome-elementui-icon-picker
RUN cd /tmp/$JOB_NAME && cnpm install
RUN cd /tmp/$JOB_NAME && npm run build:prod
FROM nginx as prod
COPY --from=builder /tmp/$JOB_NAME/dist /usr/share/nginx/html
COPY --from=builder /tmp/$JOB_NAME/nginx.conf /etc/nginx/conf.d/$JOB_NAME.conf
EXPOSE 80
CMD [\\"nginx\\", \\"-g\\", \\"daemon off;\\"]
EOF
docker build -t ${HARBOR}/${INAME}:$IMAGE_TAG ."
'''
}
}
}
stage('Image Push') { //上传镜像到远程镜像仓库
steps {
withEnv(["IMAGE_TAG=${tag}"]) {
sh '''
ssh -i /var/jenkins_home/id_rsa [email protected] "docker push ${HARBOR}/${INAME}:$IMAGE_TAG;
"
'''
}
}
}
//部署阶段
stage('Image Pull') { //从远程镜像仓库拉取镜像
steps {
withEnv(["IMAGE_TAG=${tag}"]) {
sh '''
ssh -i /var/jenkins_home/id_rsa [email protected] "docker pull ${HARBOR}/${INAME}:$IMAGE_TAG"
'''
}
}
}
stage('Remove old container') { //删除原有容器
steps {
script {
try{
sh '''
ssh -i /var/jenkins_home/id_rsa [email protected] "docker rm -f $JOB_NAME"
'''
}catch (error){
}finally{
echo "remove old container success"
}
}
}
}
stage("Image Deploy") {
steps {
withEnv(["IMAGE_TAG=${tag}"]) {
timeout(time: 1, unit: 'MINUTES') {
script {
//env.DEPLOY_ENV = input message: '选择部署的环境', ok: 'deploy',
// parameters: [choice(name: 'DEPLOY_ENV', choices: ['dev', 'test', 'pre', 'prd'], description: '选择部署环境')]
//switch("${env.DEPLOY_ENV}"){
switch("${environment}"){
case 'dev':
println('deploy dev env')
sh '''
ssh -i /var/jenkins_home/id_rsa [email protected] "docker run -d -p 7178:80 --name=$JOB_NAME ${HARBOR}/${INAME}:${IMAGE_TAG}"
'''
echo '记录线上版本'
sh "echo > /var/jenkins_home/onlineRevision/${JOB_NAME}.${tagfull}.`date '+%Y%m%d-%H:%M:%y'`.revision"
break;
case 'test':
println('deploy test env')
break;
case 'pre':
println('deploy pre env')
break;
case 'prd':
println('deploy prd env')
break;
default:
println('error env')
}
}
}
}
}
}
}
post { //stages所有任务执行后触发post
always { //发送钉钉通知
echo "构建、部署"
//echo 'Dingtalk Notification'
//sh "python3 sonar.py"
}
failure { //构建失败通知
echo "构建失败"
//dingTalk accessToken:'(钉钉通知密钥)',imageUrl: '', jenkinsUrl: 'http://xx.xx.xx:7176/', message: '代码部署失败', notifyPeople: 'phone'
}
success { //构建成功通知
echo "构建成功"
//dingTalk accessToken:'(钉钉通知密钥)',imageUrl: '', jenkinsUrl: 'http://xx.xx.xx:7176/', message: '代码部署成功', notifyPeople: 'phone'
}
}
}