🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1、安装插件 Git Parameter Plug-In 2、配置jenkins ![](https://box.kancloud.cn/a64d90851875227bab7c2f6a9fa43f3e_1231x623.png) ![](https://box.kancloud.cn/f1d3b131cd96c5cd68d67673227764ba_1185x637.png) ![](https://box.kancloud.cn/e36ad7d93d67d3457ae21130b04572bd_1209x575.png) ![](https://box.kancloud.cn/98a3e0753c01c4e42ac9e06c283f32f2_1078x626.png) 脚本如下 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 ![](https://box.kancloud.cn/4780f28a37e60bd85233aa7e958e9570_803x601.png) [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咯)