由於前一段時間發佈了幾篇關於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
<?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
#!/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
#!/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腳本:
如有不明白的地方,可以參考我的部署系列文章或線上交流!