-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsvnExport.sh
More file actions
378 lines (372 loc) · 19 KB
/
Copy pathsvnExport.sh
File metadata and controls
378 lines (372 loc) · 19 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash -e
################################################
# Todo:自动从SVN配置库获取代码,导出差异文件。
# Author:归根落叶
# Blog:http://www.ispenn.com
################################################
#Function: printLog()
#Author: 归根落叶
#Todo: 打印日志
#Param: logInfo(日志信息)
logPath="`pwd`/logs" #日志存放路径
function printLog(){
local errorCode=$?
local logInfo=$1
if [ ! -d ${logPath} ];then
mkdir -p ${logPath}
fi
if [ $# -ne 1 ];then
echo `date +"%Y-%m-%d %H:%M:%S"` "[ERROR] Usage:printLog logInfo" | tee --append ${logPath}/svnRuntimeLog-`date +"%Y-%m-%d"`.txt
exit 1
fi
if [ ${errorCode} -ne 0 ];then
echo `date +"%Y-%m-%d %H:%M:%S"` "[ERROR] ${logInfo}" | tee --append ${logPath}/svnRuntimeLog-`date +"%Y-%m-%d"`.txt
return 1
else
echo `date +"%Y-%m-%d %H:%M:%S"` "${logInfo}" >> ${logPath}/svnRuntimeLog-`date +"%Y-%m-%d"`.txt
fi
}
#Function: svnDo()
#Author: 归根落叶
#Todo: 操作SVN
#Param: userName(用户名)
# passWord(密码)
# operation[co(签出)|up(更新)|add(新增)|ci(提交)|export(导出)|copy(打分支|标签)|diff(对比两个版本)|info(获取信息)|log(打印日志)|gr(获取版本号)]
# svnPath(路径1)
# [ tagsPath(路径2)|localPath(路径3)
# revision(版本)
# logFile(日志文件)
# getRevisionNum(获取倒数第几次更新的版本号) ]
function svnDo(){
local userName=$1
local passWord=$2
local op=$3
case ${op} in
"co")
if [ $# -ne 6 ];then
echo "[ERROR] Usage:svnDo userName passWord co svnPath localPath revision"
printLog "[ERROR] Usage:svnDo userName passWord co svnPath localPath revision"
exit 1
fi
local svnPath=$4
local localPath=$5
local revision=$6
echo "检出SVN[${svnPath}]"
svn co --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" "${localPath}" --revision ${revision}
printLog "检出SVN[${svnPath}]" || local errorCode=$?
return ${errorCode};;
"up")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord up localPath revision"
printLog "[ERROR] Usage:svnDo userName passWord up localPath revision"
exit 1
fi
local localPath=$4
local revision=$5
echo "更新SVN[${localPath}]"
svn up --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}" --revision ${revision}
printLog "更新SVN[${localPath}]" || local errorCode=$?
return ${errorCode};;
"add")
if [ $# -ne 4 ];then
echo "[ERROR] Usage:svnDo userName passWord add localPath"
printLog "[ERROR] Usage:svnDo userName passWord add localPath"
exit 1
fi
local localPath=$4
echo "SVN新增文件[${localPath}]"
svn add --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}"
printLog "SVN新增文件[${localPath}]" || local errorCode=$?
return ${errorCode};;
"ci")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord ci localPath logFile"
printLog "[ERROR] Usage:svnDo userName passWord ci localPath logFile"
exit 1
fi
local localPath=$4
local logFile=$5
echo "提交到SVN[${localPath}]"
svn ci --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}" --file "${logFile}"
printLog "提交到SVN[${localPath}]" || local errorCode=$?
return ${errorCode};;
"export")
if [ $# -ne 6 ];then
echo "[ERROR] Usage:svnDo userName passWord export svnPath localPath revision"
printLog "[ERROR] Usage:svnDo userName passWord export svnPath localPath revision"
exit 1
fi
local svnPath=$4
local localPath=$5
local revision=$6
if [ -f upList.txt ];then
cat upList.txt |
while read filePath; do
local exPath=${filePath%/*}/
if [[ (-d "${localPath}/${exPath}") && (! -d "${localPath}/${filePath}") ]];then
svn export --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}/${filePath}@" "${localPath}/${filePath}" --revision ${revision}
printLog "SVN导出文件[${svnPath}/${filePath}@${revision}]" || local errorCode=$?
elif [ ! -d "${localPath}/${filePath}" ];then
mkdir -p "${localPath}/${exPath}"
printLog "创建目录[${localPath}/${exPath}]"
svn export --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}/${filePath}@" "${localPath}/${filePath}" --revision ${revision}
printLog "SVN导出文件[${svnPath}/${filePath}@${revision}]" || local errorCode=$?
fi
done
rm -f upList.txt
else
printLog "没有文件需要更新,如果只是删除文件,下次更新会一起删除。" || local errorCode=$?
fi
return ${errorCode};;
"copy")
if [ $# -ne 7 ];then
echo "[ERROR] Usage:svnDo userName passWord copy svnPath tagsPath revision logFile"
printLog "[ERROR] Usage:svnDo userName passWord copy svnPath tagsPath revision logFile"
exit 1
fi
local svnPath=$4
local tagsPath=$5
local revision=$6
local logFile=$7
echo "SVN打分支/标签[${tagsPath}]"
svn copy --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" "${tagsPath}" --revision ${revision} --file ${logFile}
printLog "SVN打分支/标签[${tagsPath}]" || local errorCode=$?
return ${errorCode};;
"diff")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord diff svnPath revision"
printLog "[ERROR] Usage:svnDo userName passWord diff svnPath revision"
exit 1
fi
local svnPath=$4
local revision=$5
echo "SVN对比两个版本差异[${svnPath}@${revision}]"
svn diff --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" --revision ${revision} --summarize > diff.txt
printLog "SVN对比两个版本差异[${svnPath}@${revision}]" || local errorCode=$?
rm -f delList.txt
rm -f upList.txt
cat diff.txt |
while read row; do
local op=`echo ${row} | awk '{print $1}'`
if [ ${op} == "D" ];then
echo ${row} | awk '{$1="";print $0}' | awk -F "${svnPath}" '{print $2}' >> delList.txt
else
echo ${row} | awk '{$1="";print $0}' | awk -F "${svnPath}" '{print $2}' >> upList.txt
fi
done
echo "noneLine" >> delList.txt
rm -f diff.txt
return ${errorCode};;
"info")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord info svnPath revision"
printLog "[ERROR] Usage:svnDo userName passWord info svnPath revision"
exit 1
fi
local tagsPath=$4
local revision=$5
echo "SVN获取[${tagsPath}]的信息"
svn info --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${tagsPath}" --revision ${revision}
printLog "SVN获取[${tagsPath}]的信息" || local errorCode=$?
return ${errorCode};;
"log")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord log svnPath revision"
printLog "[ERROR] Usage:svnDo userName passWord log svnPath revision"
exit 1
fi
local svnPath=$4
local revision=$5
svn log --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" --revision ${revision}
printLog "SVN获取[${tagsPath}]的日志" || local errorCode=$?
return ${errorCode};;
"gr")
if [ $# -ne 5 ];then
echo "[ERROR] Usage:svnDo userName passWord gr svnPath getRevisionNum"
printLog "[ERROR] Usage:svnDo userName passWord gr svnPath getRevisionNum"
exit 1
fi
local svnPath=$4
local getRevisionNum=$5
svn log --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" | grep "^r[0-9]" | awk "NR==${getRevisionNum}" | sed -n -r "s/r([0-9]*).*/\1/p"
printLog "SVN获取[${svnPath}]的版本号" || local errorCode=$?
return ${errorCode};;
*)
echo "[ERROR] Usage:svnDo userName passWord operation"
printLog "[ERROR] Usage:svnDo userName passWord operation"
exit 1
esac
}
#Function: editConf()
#Author: 归根落叶
#Todo: 修改配置文件
#Param: filePath(导出文件路径)
function editConf(){
if [ $# -ne 1 ];then
echo "[ERROR] Usage:editConf filePath"
printLog "[ERROR] Usage:editConf filePath"
exit 1
fi
local filePath=$1
local file1="application/config/nosql.php"
if [ -f ${filePath}/${file1} ];then
echo "修改配置文件[${filePath}/${file1}]"
sed -r -i "8,15s/mongodb:\/\/.*',/mongodb:\/\/127.0.0.1:27017',/" ${filePath}/${file1}
sed -r -i "8,15s/'database'.*,/'database' => 'testdb',/" ${filePath}/${file1}
printLog "修改配置文件[${filePath}/${file1}]"
fi
}
#Function: jsGrunt()
#Author: 归根落叶
#Todo: Grunt自动压缩js、css、图片
#Param: filePath(源文件路径),localPath(SVN本地工作目录)
function jsGrunt(){
if [ $# -ne 2 ];then
echo "[ERROR] Usage:jsGrunt filePath localPath"
printLog "[ERROR] Usage:jsGrunt filePath localPath"
exit 1
fi
local filePath=$1
local localPath=$2
local ver=`date +"%Y%m%d%H%M%S"`
local workDir=`pwd`
local modulesPath="/usr/local/lib/node_modules/LiveApp"
if [[ (-d ${filePath}) && (`ls ${filePath} | wc -l` -gt 0) ]];then
ls ${filePath} > tempdir.txt
cat tempdir.txt |
while read tdir;do
local jsnum=`find ${filePath}/${tdir} -name '*.js' | wc -l`
local cssnum=`find ${filePath}/${tdir} -name '*.css' | wc -l`
if [[ ${jsnum} -gt 0 || ${cssnum} -gt 0 ]];then
if [[ (-f ${localPath}/${tdir}/package.json) && (-f ${localPath}/${tdir}/Gruntfile.js) ]];then
rm -f ${localPath}/${tdir}/node_modules
ln -s ${modulesPath} ${localPath}/${tdir}/node_modules
cd ${localPath}/${tdir}
echo "Grunt压缩模板${tdir}的js、css和图片"
grunt
printLog "Grunt压缩模板${tdir}的js、css和图片"
if [ -d ./assets ];then
if [ -f ./app/tpl.php ];then
sed -r -i "s/init.min.js\?ver=[0-9\.]*\"/init.min.js\?ver=${ver}\"/g" ./app/tpl.php
sed -r -i "s/app.min.css\?ver=[0-9\.]*\"/app.min.css\?ver=${ver}\"/g" ./app/tpl.php
cp -rf ./app/ ${workDir}/${filePath}/${tdir}/app/
printLog "自动更新版本号[./app/tpl.php]"
fi
cp -rf ./assets ${workDir}/${filePath}/${tdir}/
if [ -f ./assets/scripts/init.min.js ];then
sed -r -i "18,20s/\/\/.*preload/preload/" ./assets/scripts/init.min.js
sed -r -i "18,20s/[^\/\/]base/\/\/base/" ./assets/scripts/init.min.js
cp -f ./assets/scripts/init.min.js ${workDir}/${filePath}/${tdir}/assets/scripts/init.min.js
printLog "自动切换到正式环境[./assets/scripts/init.min.js]"
fi
else
if [ -f ./tpl.php ];then
sed -r -i "s/init.min.js\?ver=[0-9\.]*\"/init.min.js\?ver=${ver}\"/g" ./tpl.php
sed -r -i "s/app.min.css\?ver=[0-9\.]*\"/app.min.css\?ver=${ver}\"/g" ./tpl.php
cp -f ./tpl.php ${workDir}/${filePath}/${tdir}/tpl.php
printLog "自动更新版本号[./tpl.php]"
fi
cp -rf ./dist ${workDir}/${filePath}/${tdir}/
if [ -f ./dist/scripts/init.min.js ];then
sed -r -i "18,20s/\/\/.*preload/preload/" ./dist/scripts/init.min.js
sed -r -i "18,20s/[^\/\/]base/\/\/base/" ./dist/scripts/init.min.js
cp -f ./dist/scripts/init.min.js ${workDir}/${filePath}/${tdir}/dist/scripts/init.min.js
printLog "自动切换到正式环境[./dist/scripts/init.min.js]"
fi
fi
rm -f ./node_modules
##################### 提交压缩文件到SVN开始 非正式环境不需要可删除 #####################
svnDo ${userName} ${passWord} add "`pwd`"
echo "[Auto]模板${tdir} 压缩js、css和图片" > svnLog.txt
svnDo ${userName} ${passWord} ci "`pwd`" svnLog.txt
rm -f svnLog.txt
##################### 提交压缩文件到SVN结束 非正式环境不需要可删除 #####################
cd ${workDir}
fi
fi
done
rm -f tempdir.txt
rm -rfv `find ${filePath}/ -name '.svn' -type d`
fi
}
#SVN配置库代码路径
svnPath="https://hostname/svn/project/trunk"
#Tag路径
tagsPath="https://hostname/svn/project/tags/release"
#svn用户名
userName="userName"
#svn密码
passWord="passWord"
#环境(test|pre|PRO)
env="test"
oldVersion=`svnDo ${userName} ${passWord} gr ${svnPath} 2`
printLog "获取上一次更新的版本号"
newVersion=`svnDo ${userName} ${passWord} gr ${svnPath} 1`
printLog "获取最新的版本号"
newTagsVersion=`svnDo ${userName} ${passWord} gr ${tagsPath} 1` #获取tag版本号,如果不需要打标签,需要删除
if [[ (${oldVersion} -gt 0) && (${newVersion} -gt 0) ]];then #判断是否是数字
if [ -f tagsVersion ];then
oldVersion=`cat tagsVersion` && [[ ${oldVersion} -gt 0 ]] || printLog "获取上次更新的版本号出错"
fi
if [[ ${oldVersion} -eq ${newVersion} ]];then
echo "没有新版本更新,目前新版本号为[${newVersion}]"
printLog "没有新版本更新,目前新版本号为[${newVersion}]" && exit 1
fi
updir="ali_upgrade/${oldVersion}-${newVersion}" #升级包导出路径
downdir="ali_downgrade/${newVersion}-${oldVersion}" #还原包导出路径
echo "从版本[${oldVersion}]升级到新版本[${newVersion}]"
printLog "从版本[${oldVersion}]升级到新版本[${newVersion}]"
##################### 打SVN标签开始 非正式环境不需要可删除 #####################
##################### 下面这行代码为检查是否存在标签,强制先更新预发布环境,非正式环境可删除 #####################
svnDo ${userName} ${passWord} info ${tagsPath}/tag_${newVersion} "HEAD"
echo "auto_tags:生产环境打包,SVN版本号[${newVersion}]" > svnLog.txt
svnDo ${userName} ${passWord} log ${svnPath} ${oldVersion}:${newVersion} >> svnLog.txt
if [[ ${newVersion} -gt ${newTagsVersion} ]];then
svnDo ${userName} ${passWord} copy ${svnPath} ${tagsPath}/tag_${newVersion} ${newVersion} svnLog.txt
fi
rm -f svnLog.txt
##################### 打SVN标签结束 非正式环境不需要可删除 #####################
printLog "删除临时文件"
rm -rf ${updir}
rm -rf ${downdir}
mkdir -p ${updir}
mkdir -p ${downdir}
printLog "升级,导出版本差异文件"
svnDo ${userName} ${passWord} diff ${svnPath} "${oldVersion}:${newVersion}"
svnDo ${userName} ${passWord} export ${svnPath} ${updir} "HEAD"
editConf ${updir}
mv delList.txt delList${env}Up.txt
##################### 自动压缩开始 不需要可删除 #####################
jsGrunt "${updir}/template" "./LiveAPP/template"
##################### 自动压缩结束 不需要可删除 #####################
cp -rfv ${updir}/ upgrade${env}_${oldVersion}-${newVersion}/
tar zcvf upgrade${env}.tar.gz upgrade${env}_${oldVersion}-${newVersion}/
tar zcvf ali_upgrade/upgrade_${oldVersion}-${newVersion}.tar.gz upgrade${env}_${oldVersion}-${newVersion}/
rm -rf upgrade${env}_${oldVersion}-${newVersion}
printLog "打升级包完成"
##################### 打还原包开始 不需要可删除 #####################
printLog "还原,导出版本差异文件"
svnDo ${userName} ${passWord} diff ${svnPath} "${newVersion}:${oldVersion}"
svnDo ${userName} ${passWord} export ${tagsPath}/tag_${oldVersion} ${downdir} "HEAD"
editConf ${downdir}
mv delList.txt delList${env}Down.txt
cp -rfv ${downdir}/ downgrade${env}_${newVersion}-${oldVersion}/
tar zcvf downgrade${env}.tar.gz downgrade${env}_${newVersion}-${oldVersion}/
rm -rf downgrade${env}_${newVersion}-${oldVersion}
printLog "打还原包完成"
##################### 打还原包结束 不需要可删除 #####################
##################### 打SVN标签开始 非正式环境不需要可删除 #####################
newVersion=`svnDo ${userName} ${passWord} gr ${svnPath} 1`
newTagsVersion=`svnDo ${userName} ${passWord} gr ${tagsPath} 1`
echo "auto_tags:生产环境打包,SVN版本号[${newVersion}]" > svnLog.txt
svnDo ${userName} ${passWord} log ${svnPath} ${oldVersion}:${newVersion} >> svnLog.txt
if [[ ${newVersion} -gt ${newTagsVersion} ]];then
svnDo ${userName} ${passWord} copy ${svnPath} ${tagsPath}/tag_${newVersion} ${newVersion} svnLog.txt
fi
rm -f svnLog.txt
##################### 打SVN标签结束 非正式环境不需要可删除 #####################
echo ${newVersion} > tagsVersion
exit 0
else
printLog "版本号不正确:OldVersion#${oldVersion} NewVersion#${newVersion}" && exit 1
fi