-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
257 lines (249 loc) · 10.3 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
pipeline {
agent any
environment {
DOCKER_HUB_URL = 'registry.cn-hangzhou.aliyuncs.com'
DOCKER_HUB_WORKSPACE = 'xxxxxx'
DINGTALK_ID = 'xxxxxx'
// 获取Maven pom.xml项目版本号
//APP_VERSION = readMavenPom().getVersion()
APP_VERSION = '0.1.0'
}
parameters {
choice(
choices: [ 'N', 'Y' ],
description: '是否发布服务端',
name: 'DEPLOY_SERVER')
choice(
choices: [ 'N', 'Y' ],
description: '是否发布wwwroot_release',
name: 'DEPLOY_WWWROOT')
choice(
choices: [ 'N', 'Y' ],
description: '是否发布前端',
name: 'DEPLOY_UI')
choice(
choices: [ 'latest' ],
description: '镜像TAG',
name: 'IMAGE_TAG')
choice(
choices: [ 'prod' ],
description: '发布环境',
name: 'DEPLOY_ENV')
}
options {
//设置在项目打印日志时带上对应时间
timestamps()
//不允许同时执行流水线,被用来防止同时访问共享资源等
disableConcurrentBuilds()
// 表示保留n次构建历史
buildDiscarder(logRotator(numToKeepStr: '2'))
}
stages {
stage("Preparing") {
steps {
dingtalk (
robot: '${DINGTALK_ID}',
type: 'MARKDOWN',
at: [],
atAll: false,
title: '(${JOB_NAME} #${BUILD_NUMBER})',
text: [
'### 开始构建(${JOB_NAME} #${BUILD_NUMBER})',
'---',
'- DEPLOY_SERVER: ${DEPLOY_SERVER}',
'- DEPLOY_UI: ${DEPLOY_UI}',
'- IMAGE_TAG: ${IMAGE_TAG}',
'- DEPLOY_ENV: ${DEPLOY_ENV}',
],
messageUrl: '${BUILD_URL}',
picUrl: ''
)
}
}
stage("Checkout") {
steps {
dir('./Ruoyi-Vue-CMS') {
checkout([$class: 'GitSCM', branches: [[name: '*/dev']], extensions: [], userRemoteConfigs: [[credentialsId: 'LwyGitee', url: 'https://gitee.com/liweiyi/RuoYi-Vue-CMS.git']]])
}
dir('./wwwroot_release') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitea', url: 'http://gitea.huaray.com/liweiyi/swikoon_wwwroot_release.git']]])
}
}
}
stage("Build") {
when {
expression { return params.DEPLOY_SERVER == 'Y' }
}
steps {
dir('./Ruoyi-Vue-CMS') {
withEnv(['JAVA_HOME=/var/jenkins_home/jdk/jdk-17.0.4.1']) {
withMaven(maven: 'M3.8') {
sh 'mvn -U clean package -Dmaven.test.skip=true'
}
}
}
}
}
stage("ruoyi-admin") {
when {
expression { return params.DEPLOY_SERVER == 'Y' }
}
steps {
withEnv(['APP_PATH=ruoyi-admin', 'APP_NAME=ruoyi-admin']) {
echo "docker build start: ${APP_PATH}#${APP_VERSION}"
dir('./Ruoyi-Vue-CMS') {
withCredentials([usernamePassword(credentialsId: 'ALIYUN-DOCKER-REGISTRY-LWY', passwordVariable: 'DOCKERPWD', usernameVariable: 'DOCKERUSER')]) {
sh '''
cd ${APP_PATH}
echo ${DOCKERPWD} | docker login --username=${DOCKERUSER} --password-stdin ${DOCKER_HUB_URL}
docker build -t ${DOCKER_HUB_URL}/${DOCKER_HUB_WORKSPACE}/${APP_NAME}:${IMAGE_TAG} . --build-arg APP_NAME=${APP_PATH} --build-arg APP_VERSION=${APP_VERSION}
docker logout ${DOCKER_HUB_URL}
'''
}
}
echo "docker push start: ${APP_PATH}#${APP_VERSION}"
dir('./Ruoyi-Vue-CMS') {
withCredentials([usernamePassword(credentialsId: 'ALIYUN-DOCKER-REGISTRY-LWY', passwordVariable: 'DOCKERPWD', usernameVariable: 'DOCKERUSER')]) {
sh '''
echo ${DOCKERPWD} | docker login --username=${DOCKERUSER} --password-stdin ${DOCKER_HUB_URL}
docker push ${DOCKER_HUB_URL}/${DOCKER_HUB_WORKSPACE}/${APP_NAME}:${IMAGE_TAG}
docker logout ${DOCKER_HUB_URL}
cp -f bin/docker-image-clear.sh docker-image-clear.sh
sed -i "s/{{DOCKER_HUB_URL}}/${DOCKER_HUB_URL}/g" docker-image-clear.sh
sed -i "s/{{IMAGE_REPOSITORY}}/${DOCKER_HUB_WORKSPACE}\\/${APP_NAME}/g" docker-image-clear.sh
/bin/bash docker-image-clear.sh
rm -f docker-image-clear.sh
'''
}
}
// deploy
dir('./Ruoyi-Vue-CMS') {
withCredentials([usernamePassword(credentialsId: 'ALIYUN-DOCKER-REGISTRY-LWY', passwordVariable: 'DOCKERPWD', usernameVariable: 'DOCKERUSER')]) {
sh '''
cp -f bin/docker-deploy.sh ${APP_PATH}
cd ${APP_PATH}
cp -f ../bin/docker-deploy.sh docker-deploy.sh
sed -i "s/{{DOCKERUSER}}/${DOCKERUSER}/g" docker-deploy.sh
sed -i "s/{{DOCKERPWD}}/${DOCKERPWD}/g" docker-deploy.sh
sed -i "s/{{DOCKER_HUB_URL}}/${DOCKER_HUB_URL}/g" docker-deploy.sh
sed -i "s/{{IMAGE_REPOSITORY}}/${DOCKER_HUB_WORKSPACE}\\/${APP_NAME}/g" docker-deploy.sh
sed -i "s/{{IMAGE_TAG}}/${IMAGE_TAG}/g" docker-deploy.sh
cp -f docker/docker-compose_${DEPLOY_ENV}.yml docker-compose.yml
sed -i "s/{{DOCKER_IMAGE}}/${DOCKER_HUB_URL}\\/${DOCKER_HUB_WORKSPACE}\\/${APP_NAME}:${IMAGE_TAG}/g" docker-compose.yml
'''
sshPublisher(publishers: [sshPublisherDesc(configName: 'GameCluster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
mkdir -p /www/docker/ruoyi-admin
cd /www/docker/ruoyi-admin
sh docker-deploy.sh
rm -f docker-deploy.sh
''', execTimeout: 600000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'ruoyi-admin/',
remoteDirectorySDF: false, removePrefix: 'ruoyi-admin/',
sourceFiles: 'ruoyi-admin/docker-compose.yml,ruoyi-admin/docker-deploy.sh')],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
// delete tmp file
dir('./Ruoyi-Vue-CMS') {
sh 'rm -f ${APP_PATH}/docker-deploy.sh'
sh 'rm -f ${APP_PATH}/docker-compose.yml'
}
echo "build end: ${APP_PATH}"
}
}
}
stage("wwwroot_release") {
when {
expression { return params.DEPLOY_WWWROOT == 'Y' }
}
steps {
dir('./wwwroot_release') {
sh 'zip -q -r wwwroot_release.zip * --exclude *.svn* --exclude *.git*'
sshPublisher(publishers: [sshPublisherDesc(configName: 'GameCluster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
cd /www/docker/ruoyi-admin/wwwroot_release
unzip -o -q wwwroot_release.zip
rm -f wwwroot_release.zip
''', execTimeout: 600000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'ruoyi-admin/wwwroot_release/',
remoteDirectorySDF: false, removePrefix: '',
sourceFiles: 'wwwroot_release.zip')],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
}
stage("ruoyi-ui") {
when {
expression { return params.DEPLOY_UI == 'Y' }
}
steps {
dir('./Ruoyi-Vue-CMS/ruoyi-ui') {
nodejs('NodeJS16_13') {
sh '''
npm install --registry=https://registry.npmmirror.com
npm run build:prod
cd dist
zip -q -r ui.zip *
'''
}
}
dir('./Ruoyi-Vue-CMS/ruoyi-ui/dist') {
sshPublisher(publishers: [sshPublisherDesc(configName: 'GameCluster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
mkdir -p /www/docker/ruoyi-ui
cd /www/docker/ruoyi-ui
unzip -o -q ui.zip
rm -f ui.zip
''', execTimeout: 600000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'ruoyi-ui/',
remoteDirectorySDF: false, removePrefix: '',
sourceFiles: 'ui.zip')],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
sh 'rm -f ui.zip'
}
}
}
}
post {
success {
dingtalk (
robot: '${DINGTALK_ID}',
type: 'MARKDOWN',
at: [],
atAll: false,
title: '${JOB_NAME} #${BUILD_NUMBER}',
text: [
'### 构建成功(${JOB_NAME} #${BUILD_NUMBER})',
'---',
'- IMAGE_TAG: ${IMAGE_TAG}',
'- DEPLOY_ENV: ${DEPLOY_ENV}',
],
messageUrl: '${BUILD_URL}',
picUrl: ''
)
}
failure {
dingtalk (
robot: '${DINGTALK_ID}',
type: 'LINK',
at: [],
atAll: false,
title: '${JOB_NAME} #${BUILD_NUMBER}',
text: [
'构建失败',
],
messageUrl: '${BUILD_URL}',
picUrl: ''
)
}
unstable {
dingtalk (
robot: '${DINGTALK_ID}',
type: 'LINK',
at: [],
atAll: false,
title: '${JOB_NAME} #${BUILD_NUMBER}',
text: [
'构建流程可能出现问题,详情请查看流程日志',
],
messageUrl: '${BUILD_URL}',
picUrl: ''
)
}
}
}