[code]1.Linux上安裝jdk,maven,ant,git,不再表述。需要注意的是,要將ssh公鑰添加到github中,具體如下:
[code][jenkins@CentOS ~]# ssh-keygen -t rsa -C "[email protected]" #敲入三個回車 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: bb:a5:2e:45:86:58:c5:46:e6:3d:8d:cc:46:e8:93:d2 [email protected] The key's randomart image is: #查看公鑰,復制: [jenkins@CentOS ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
[code]將公鑰添加到github中(用的是開源中國的github碼雲)
[code]2.將Jenkins.war放在tomcat下,啟動。 3.安裝Jenkins插件,ant、maven、git、github、ssh,不再表述。 4.在Jenkins的系統管理中,設置jdk、ant、maven、git的執行路徑,添加ssh帳號。
[code]5.新建job,選擇“構建一個maven項目”,設置git的地址
[code]6.設置maven goals,添加post steps,這裡執行shell腳本,進行部署web。
[code]7.保存,點擊立即構建,構建成功後會在workplace中生成war包。
[code]8.可以使用Jenkins的插件Deploy Plugin進行部署,也可以使用shell腳本進行部署,這裡使用shell腳本進行部署。 (Jenkins和web服務器在同一台機器上,若在不同機器上,請參考ssh無密登錄)
[code]#! /bin/bash . /etc/init.d/functions warpath=/home/jenkins/.jenkins/jobs/new/workspace/target webpath=/home/jenkins/tomcat-web warfile=`ls $warpath/*.war` filename=`basename $warfile` #echo $warfile $filename war=$webpath/webapps/$filename if [ -f $war ]; then cp $war $webpath/bak/$filename`date +%Y%m%d%H%M%S`.bak cd $webpath/bak/ num=`ls -lrt yuanwq* |wc -l` echo $num if [ $num -ge 5 ]; then # cd $webpath/bak/ ls -lrt yuanwq* |awk 'NR==1' | awk '{print $9}'|xargs rm -rf fi fi #cp $warfile $webpath/webapps/$filename && action "cp success" /bin/true #啟動tomcat status=`ps -ef |grep -v grep |grep tomcat-web |wc -l` if (($status==1)); then ps -ef |grep -v grep |grep tomcat-web |awk '{print $2}'|xargs kill -9 fi # #刪除webapps目錄下的所有文件 cd $webpath/webapps if [ $? -eq 0 ]; then rm -rf ./* fi #拷貝war包 cp $warfile $webpath/webapps/$filename && action "cp success" /bin/true cd $webpath/bin ./startup.sh && action "web is starting" /bin/true #sh $webpath/bin/startup.sh && action "web is starting" /bin/true