1、安裝插件
Git Parameter Plug-In
2、配置jenkins




腳本如下
GITDIR="/var/lib/jenkins/workspace"
PRONAME="t_yyg"
DSTDIR="/opt/deploy"
#DATE="`date +%F_%H-%M`"
USER="www"
cat > /home/www/ip.list <<EOF
10.2.11.237
10.2.11.233
EOF
function deploy(){
cd ${GITDIR}/${PRONAME} && git pull origin tag ${Tag} && git checkout ${Tag}
cd $GITDIR && tar zcf ${PRONAME}_${Tag}.tar.gz ${PRONAME}/*
mv ${PRONAME}_${Tag}.tar.gz /opt/deploy/
for IP in `cat /home/www/ip.list`;do
scp /opt/deploy/${PRONAME}_${Tag}.tar.gz ${USER}@${IP}:${DSTDIR}
ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${Tag}.tar.gz && mv ${PRONAME} ${PRONAME}_${Tag}"
ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${Tag} /var/www/html/${PRONAME}"
ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
done
}
function rollback(){
for IP in `cat /home/www/ip.list`;do
ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${Tag} /var/www/html/${PRONAME}"
ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
done
}
case $Operation in
Deploy)
deploy
;;
Rollback)
rollback
;;
esac
三)測試:
1)在本地提交,然后push到遠程,登錄gitlab打tag為v1.1
[root@static yyg]# vim a.html
[root@static yyg]# vim b.html
[root@static yyg]# git add .
[root@static yyg]# git commit . -m "add a b"
[master c1af6ba] add a b
2 files changed, 2 insertions(+)
create mode 100644 a.html
create mode 100644 b.html
[root@static yyg]# git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 325 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To gitlab@git.91als.net:root/yyg.git
51d123f..c1af6ba master -> master
2)在本地提交,然后push到遠程,登錄gitlab打tag為v1.2
[root@static yyg]# vim test.html
[root@static yyg]# git add .
[root@static yyg]# git commit -m "add test.html"
[master b793078] add test.html
1 file changed, 1 insertion(+)
create mode 100644 test.html
[root@static yyg]# git push origin master
然后通過jenkins發布版本
第一次發布v1.0版本
[root@static html]# ll
total 0
lrwxrwxrwx. 1 www www 22 Jan 19 15:33 t_yyg -> /opt/deploy/t_yyg_v1.0
第二次發布v1.1版本
[root@static html]# ll
total 0
lrwxrwxrwx. 1 www www 22 Jan 19 15:35 t_yyg -> /opt/deploy/t_yyg_v1.1
lrwxrwxrwx. 1 www www 23 Jan 18 17:14 yyg -> /opt/deploy/yyg_66f0c74
lrwxrwxrwx. 1 www www 10 Jan 18 13:34 zrlog -> /opt/zrlog
[root@static html]# cd t_yyg/
[root@static t_yyg]# ll
total 1624
-rw-r--r--. 1 www www 11 Jan 18 15:35 a.html
drwxr-xr-x. 5 www www 4096 Jan 18 15:33 app
drwxr-xr-x. 4 www www 4096 Jan 18 15:33 app2
-rw-r--r--. 1 www www 1172529 Jan 18 15:33 app.apk
-rw-r--r--. 1 www www 11 Jan 18 15:35 b.html
(發現多了a.html和b.html代碼)
第三次發布v1.2版本
[root@static html]# ll
total 0
lrwxrwxrwx. 1 www www 22 Jan 19 15:37 t_yyg -> /opt/deploy/t_yyg_v1.2
lrwxrwxrwx. 1 www www 23 Jan 18 17:14 yyg -> /opt/deploy/yyg_66f0c74
lrwxrwxrwx. 1 www www 10 Jan 18 13:34 zrlog -> /opt/zrlog
[root@static html]# cd t_yyg/
[root@static t_yyg]# ll|grep test.html
-rw-r--r--. 1 www www 18 Jan 18 15:37 test.html
(發現多了一個test.html)
此時發現代碼有問題,需要回退到上一個tag(或指定tag)
比如回退到v1.1

[root@static html]# ll
total 0
lrwxrwxrwx. 1 www www 22 Jan 19 15:39 t_yyg -> /opt/deploy/t_yyg_v1.1
lrwxrwxrwx. 1 www www 23 Jan 18 17:14 yyg -> /opt/deploy/yyg_66f0c74
lrwxrwxrwx. 1 www www 10 Jan 18 13:34 zrlog -> /opt/zrlog
[root@static html]# cd t_yyg/
[root@static t_yyg]# ll |grep test.html
(發現沒有test.html,說明版本回退到v1.1咯)
- 第一章:Jenkins的基本介紹
- 第二章:Jenkins服務器安裝
- 第一節:在Centos6.8系統上安裝Jenkins服務器
- 第三章:Jenkins服務器配置
- 第一節:jenkins更換初始登錄密碼
- 第二節:全局工具配置
- 第三節:安裝插件
- 第四節:jenkins中配置git
- 第五節:jenkins中配置Publish Over SSH
- 第六節:jenkins郵件配置
- 第七節:Jenkins進階-獲取git tags代碼
- 第八節:Jenkins權限配置錯誤,導致無法登錄
- 第九節: jenkins通過釘釘發送消息
- 第四章:使用 Sonar 進行代碼質量管理
- 第一節:sonar概述
- 第二節:Sonar的安裝
- 第三節:sonar安裝插件
- 第四節:sonar報錯
- 第五節:代碼分析
- 第五章: 持續集成
- 第一節:GitLab觸發jenkins構建項目
- 第六章:jenkins 2.0 pipeline 從入門到精通
- 第一節:jenkins2 hello pipeline
- 第二節:jenkins pipeline基本概念
- 第七章:jenkins部署實戰
- 第一節:jenkins部署php代碼(一)
- 第二節:jenkins部署java代碼(一)
- 第三節:jenkins部署java代碼(pom.xml)二
- 第四節:jenkins部署php代碼(根據tags)
- 第五節:jenkins部署android代碼
- 第八章:jenkins項目構建配置
- 第一節: jenkins參數化構建過程