由于前一段时间发布了几篇关于web程序基于jenkins部署发布的博文,由于是基于svn作为版本库讲解的。这段时间有一些朋友问我git作为版本库的与jenkins结合实现像选择svn tag指定程序版本的实现发布程序的解决方案。
有与时间原因,本篇不在过多赘述细节,读者可以动手揣摩!
直接先给大家看一下我目前正在使用的部署到生产环境下的jenkins截图!
下面给出配置截图:
经过测试git parameter plugin不能满足我们的需求,所以我们只能自己在jenkins中创建tag list。添加写一下如图步骤:
自己写一个可扩展下拉菜单,通过程序生成指定git版本下的程序的tags列表
下面是程序的源码管理,在git repositories url 中添加我们程序的git地址
下图两种方式都行:
注意我们这个时候选择的是master版本,意味着我的程序都是稳定版本(在开发过程中使用git-flow流程来控制版本开发流程)。
签出到指定的tag:这一步很重要:
到此我们已经实现了jenkins与git的集成结合!这也是前段时间发布的部署系列svn与git的不同之处!
jenkins下git的插件安装网上很多这里不再介绍!
需要注意一点可以根据自己的需求开发出git tag list的功能!
下面给出我按照自己的需求实现的一个:
先给出程序结构图:
git.php
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 | <?php $giturl = $_REQUEST [ "giturl" ]; $arr = explode ( "/" , $giturl ); $names = $arr [ count ( $arr )-1]; $namearr = explode ( "." , $names ); $name = $namearr [0]; $rebuild =true; $tagpath = "/var/www/tags/list/$name" ; if ( file_exists ( $tagpath )) { $content = file_get_contents ( $tagpath ); $tags = explode ( "\n" , $content ); //unlink($tagpath); unset( $tags [ count ( $tags )-1]); if ( count ( $tags )>0) { $tags = array_reverse ( $tags ); echo "tags=" .implode( "," , $tags ); } else { echo "tags= no tags" ; } $ctime = filectime ( $tagpath ); $now =time(); $ti = $now - $ctime ; if ( $ti <60) { $rebuild =false; } } else { echo "tags= Is loading tags ; please try again later" ; } if ( $rebuild ) { $cmd = "echo $name $giturl > /var/www/tags/info/$name" ; exec ( $cmd , $info , $rebt ); } ?> |
inotify.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/bin/bash #create by lhb #date 2014-02-08 #monitor file change workplace= /var/www/tags src=$workplace /info/ lists=$workplace /list /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' -- format '%T %w%f %e' -e close_write $src \ | while read file do fn=` echo $ file | awk '{print $3}' ` arr=(` cat $fn | awk '{print $1,$2}' `) pro_name=${arr[0]} pro_url=${arr[1]} #echo $pro_name #echo $pro_url /bin/bash $workplace /get_tags .sh $pro_name $pro_url > $lists/$pro_name chown www-data:www-data $lists/$pro_name echo $? done |
get_tags.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/bin/bash #create by lhb #date 2014-02-08 #get git code tag list codepath= /tmp/ $1 [ -d "$codepath" ] || { cd /tmp/ /usr/bin/git clone $2 &> /dev/null } cd $codepath /usr/bin/git pull &> /dev/null /usr/bin/git tag rm -rf $codepath |
执行shell脚本:
如有不明白的地方,可以参考我的部署系列文章或线上交流!
本文出自 “宅鸟乐园” 博客,请务必保留此出处http://birdinroom.blog.51cto.com/7740375/1404930
联系客服