jenkins-doc-搭建和使用

搭建

jdk

1
2
3
4
# https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
wget https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz?AuthParam=1544106359_59a062517dc08e0eae75256f0b230864
mv jdk-8u191-linux-x64.tar.gz\?AuthParam\=1544106716_9ebe1b6f1f7a1d719305bd147fb0150e jdk-8u191-linux-x64.tar.gz
tar -xzf jdk-8u191-linux-x64.tar.gz

tomcat

1
2
3
# tomcat
# https://tomcat.apache.org/download-90.cgi
# wget 会失败,直接官网然后上传

jenkins war包

1
2
3
4
5
# https://jenkins.io/download/
cd ~/apache-tomcat-9.0.13/webapps/
wget http://ftp-nyc.osuosl.org/pub/jenkins/war/2.154/jenkins.war

# 复制war包到tomcat的webapps

配置JAVA_HOME

1
2
3
export JAVA_HOME=/opt/jdk1.8.0_191/
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

启动

1
2
3
4
5
6
7
8
9
10
# 切到apache的bin启动apache
sh startup.sh

# 修改插件升级路径
# http://47.92.232.220:8080/jenkins/pluginManager/advanced
# Update Site 的https 改成http

# 重启apache
sh shutdown.sh
sh startup.sh

构建自动删除

1
2
3
4
5
6
7
8
9
10
11
12
13
配置Discard old builds选项
打开一个Jenkins job的Configure页面,勾选上”Discard old builds”,填入Day of keep builds (保留几天的build记录)和Max# of builds to keep (保留最多几个build记录)。

点击Advanced按钮,还可以填入Days of keep artifacts (保留几天的artifact)和 Max# of builds to keep with artifacts (保留最多几个artifacts)。

通常来讲,如果磁盘空间足够,这4个选项设置为15就可以了(也就是两周 + 1天)。
如果磁盘空间不足时,可以将这4个选项都设置为3。

Jenkins job在每次build结束后(无论成功还是失败)都会自动执行discard old builds,这样当下次执行build后,这个job占用的磁盘空间就会被释放。

$JENKINS_HOME/jobs/[JOB_NAME]/builds 目录存储了该Jenkins job的全部构建记录(目录名为Build Number)。

$JENKINS_HOME/jobs/[JOB_NAME]/builds/BUILD_NUMBER/artifacts 目录存储了该次构建的artifact。

用户管理

安装插件Role-based

  • 进入系统管理
  • 进入插件管理
  • 安装插件Role-base
  • 进入全局安全配置
  • 配置使用Role-Based Strategy

Role-based插件安装

Role-based插件安装

用户配置

  • 注意
    在Assign Roles里面添加用户的时候,不勾选对应的权限组点save或apply都是不生效的!
    配置项目用户的时候,需要同时配置全局权限,但要注意全局权限的权限勾选,全局权限是会覆盖项目权限的。

创建全局用户

创建全局用户
![创建项目用户](/images/app/Role-based配置项目用户.gif)
创建项目用户

配置节点

linux节点

  • 注意
    配置完节点直接连接节点的是会失败的,需要在对应的jenkins工作目录加入jdk,并且jdk目录名称为jdk

linux节点配置

linux节点配置

window节点

流水线配置

脚本

  • 后退运行脚本
1
2
3
4
5
withEnv(['JENKINS_NODE_COOKIE=background_job']) {
sh """
nohup xxxxx &
"""
}

参考

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pipeline {
agent none

environment {
def PRO_DIR = "E:/egame/client-fanti"
def CLIENT_REL_DIR = "E:/egame/client-fanti/eGameClient/bin-release/web"
def CLIENT_DIR = "E:/egame/client-fanti/eGameClient"
def DATE_DIR ="E:/egame/client-fanti/eGameData"
def THIRT_DIR = "E:/egame/client-fanti/eGameThirdParts"
def DYN_DIR = "E:/egame/client-fanti/eGameClientDynamic"
def REALES_DIR = "E:/egame/client-fanti/eGameClientH5Release"

def WEB_DIR = "/data/egame/eGameClientH5Release"
}

stages {

stage('删除client的web') {
agent { label 'window10' }

steps {
dir(CLIENT_REL_DIR) {
powershell 'Remove-Item -Force -Recurse ./*'
}

echo '==============删除client的web=============='
}
}

stage('更新client') {
agent { label 'window10' }

steps {
dir(CLIENT_DIR) {
powershell 'git checkout ./'
powershell 'git pull'
}

echo '==============更新原client=============='
}
}

stage('更新data') {
agent { label 'window10' }

steps {
dir(DATE_DIR) {
powershell 'git checkout ./'
powershell 'git pull'
}

echo '==============更新原data=============='
}
}

stage('更新thirdpart') {
agent { label 'window10' }

steps {
dir(THIRT_DIR) {
powershell 'git checkout ./'
powershell 'git pull'
}

echo '==============更新原thirdpart=============='
}
}

stage('更新release') {
agent { label 'window10' }

steps {
dir(REALES_DIR) {
powershell 'git clean -df ./'
powershell 'git checkout ./'
powershell 'git pull'
}

echo '==============更新release=============='
}
}

stage('删除增量旧目录') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell 'Remove-Item -Force -Recurse ./eGameClient'
powershell 'Remove-Item -Force -Recurse ./eGameData'
powershell 'Remove-Item -Force -Recurse ./eGameThirdParts'
powershell 'Remove-Item -Force -Recurse ./eGameLogicServerTs'
powershell 'Remove-Item -Force -Recurse ./release'
}

echo '==============删除增量旧目录=============='
}
}

stage('复制目录到增量') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
bat 'call copy.bat'
}

echo '==============复制目录到增量=============='
}
}

stage('生成MD5') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell './dynamic.exe 1 ../eGameClientH5Release/resource/assets ../eGameClientH5Release/resource/load_runTime ../eGameClientH5Release/resource/load_preload ../eGameClientH5Release/resource/load_loading'
}

echo '==============生成MD5=============='
}
}

stage('修改翻译配置') {
agent { label 'window10' }

steps {
dir(DATE_DIR) {
powershell 'Remove-Item -Force -Recurse ./language/languageType.json'

sleep 2

writeFile file: 'E:\\egame\\client-fanti\\eGameData\\language\\languageType.json', text: '''{
"languageType":2
}'''

}

echo '==============修改翻译配置=============='
}
}

stage('转表') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
bat 'call zhuanbiao.bat'
}

echo '==============转表=============='
}
}

stage('资源增量处理') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell './dynamic.exe 1 ../eGameClientH5Release/resource/assets ../eGameClientH5Release/resource/load_runTime ../eGameClientH5Release/resource/load_preload ../eGameClientH5Release/resource/load_loading'

powershell './dynamic.exe 0'

powershell './dynamic.exe 3 ./eGameClient/resource/assets ./eGameClient/resource/load_runTime ./eGameClient/resource/load_preload ./eGameClient/resource/load_loading'
}

echo '==============资源增量处理=============='
}
}

stage('重新生成资源配置') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
bat 'call ziyuan.bat'
}

echo '==============重新生成资源配置=============='
}
}

stage('修正main.ts') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell './dynamic.exe 4 ./eGameClient'
}

echo '==============修正main.ts=============='
}
}

stage('清理客户端') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell 'C:/Users/Administrator/AppData/Roaming/npm/egret.cmd clean ./eGameClient'
}

echo '==============清理客户端=============='
}
}

stage('构建客户端') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell 'C:/Users/Administrator/AppData/Roaming/npm/egret.cmd publish ./eGameClient -compile --runtime html5'
}

echo '==============构建客户端=============='
}
}

stage('修正manifest') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell './dynamic.exe 5 ./eGameClient/bin-release/web'
}

echo '==============修正manifest=============='
}
}

stage('复制发布项目') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell './dynamic.exe 6 ./eGameClient/bin-release/web'
}

echo '==============修正manifest=============='
}
}

stage('移除旧release') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
powershell 'Remove-Item -Force -Recurse ../eGameClientH5Release/js'
powershell 'Remove-Item -Force -Recurse ../eGameClientH5Release/resource'
powershell 'Remove-Item -Force -Recurse ../eGameClientH5Release/index*'
powershell 'Remove-Item -Force -Recurse ../eGameClientH5Release/mani*'
}

echo '==============移除旧release=============='
}
}

stage('复制新的release') {
agent { label 'window10' }

steps {
dir(DYN_DIR) {
bat 'call copy-release.bat'
}

echo '==============复制新的release=============='
}
}

stage('上传到git') {
agent { label 'window10' }

steps {
dir(REALES_DIR) {
powershell 'git add *'
powershell 'git commit --allow-empty -m "daobaoji"'
powershell 'git push'
}

echo '==============上传到git=============='
}
}

stage('更新繁体web') {
agent { label 'web-fanti' }

steps {
dir(WEB_DIR) {
sh 'git pull'
}

echo '==============更新繁体web=============='
}
}
}
}