Jenkins配置回滚
# 1. 安装工具
第一步:Jenkins服务器安装
yum install sshpass - y
1
第二步:在 PreSteps 流程增加执行shell[每个job只需配置一台服务器]
注意:/home/jenkins/prohosts/proPW 为 proHost 主机的 jyapp 用户 密码。
# 2. 脚本修改
代码路径-变量(proPath)
其中任意一台业务服务器IP地址-变量(proHost)
其中任意一台业务服务器jyapp用户密码,配置文件在Jenkins服务器/home/jenkins/prohosts/proPW
#!/bin/bash
#代码路径
proPath="/home/jyapp/${projectName}"
backFile="${projectName}"_bak_"${fullPackageName}_${BUILD_NUMBER}"
cTime=$(date "+%Y-%m-%d-%H-%M")
zpkPath="/var/www/zpk/chuangyun/${projectName}/${cTime}"
#每个job配置一台(每个job需要修改)
proHost="10.50.172.17"
#制品库服务器
zpkHost="10.50.200.32"
pull () {
/usr/bin/sshpass -f "/home/jenkins/prohosts/proPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${proHost} tar -zcf /tmp/${backFile}.tar.gz ${proPath}/*
[ $? -ne 0 ] && echo "远程代码备份完成................"
echo "拉取远程备份开始................"
/usr/bin/sshpass -f "/home/jenkins/prohosts/proPW" scp -P10022 -o "StrictHostKeyChecking no" jyapp@${proHost}:/tmp/${backFile}.tar.gz /tmp/
tar -zxf /tmp/${backFile}.tar.gz -C /tmp && rm -f /tmp/${backFile}.tar.gz
cd /tmp${proPath} && tar -zcf /tmp/${backFile}.tar.gz *
echo "备份的文件大小: $(/usr/bin/du -sh /tmp/${backFile}.tar.gz)"
md5sum /tmp/${backFile}.tar.gz
echo "拉取远程备份结束................"
}
push () {
echo "创建远程制品库目录"
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${zpkHost} sudo -S mkdir -p ${zpkPath}
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${zpkHost} sudo -S chown -R jyapp.jyapp /var/www/zpk/chuangyun
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" scp -r -P10022 -o "StrictHostKeyChecking no" /tmp/${backFile}.tar.gz jyapp@${zpkHost}:${zpkPath}
[ $? -eq 0 ] && { echo "推送备份文件到制品库完成.................."; echo "本次回滚下载地址为: http://${zpkHost}:7070/chuangyun/${projectName}/${cTime}/${backFile}.tar.gz"; } || echo "推送备份文件到制品库失败++++++++++"
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${zpkHost} echo -ne "制品库备份的文件大小: "
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${zpkHost} /usr/bin/du -sh ${zpkPath}/${backFile}.tar.gz
/usr/bin/sshpass -f "/home/jenkins/prohosts/zpkPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${zpkHost} md5sum ${zpkPath}/${backFile}.tar.gz
}
clear () {
/usr/bin/sshpass -f "/home/jenkins/prohosts/proPW" ssh -p 10022 -o "StrictHostKeyChecking no" jyapp@${proHost} /bin/rm -f /tmp/${backFile}.tar.gz
[ $? -eq 0 ] && echo "删除远端备份文件成功......................." || echo "删除远端备份文件失败......................."
/bin/rm -f /tmp/${backFile}.tar.gz
[ $? -eq 0 ] && echo "删除本地备份文件成功......................." || echo "删除本地备份文件失败......................."
}
main () {
pull
push
clear
}
main
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
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
上次更新: 2022/09/30, 15:27:27