jenkins+maven+github持续集成

Jenkins用的是最新的版本V2.3,运行在Linux上,maven版本是3.3.9,git版本是2.6.4,jdk版本是1.7.0_79,tomcat版本是7.0.68,ant版本是1.9.6,版本下载地址再也不提供。git

1.Linux上安装jdk,maven,ant,git,再也不表述。须要注意的是,要将ssh公钥添加到github中,具体以下:
[jenkins@CentOS ~]# ssh-keygen -t rsa -C "youremail@163.com"
#敲入三个回车
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 yuanwq@163.com
The key's randomart image is:

#查看公钥,复制:
[jenkins@CentOS ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
将公钥添加到github中(用的是开源中国的github码云)

这里写图片描述
开源中国生成公钥帮助连接github

2.将Jenkins.war放在tomcat下,启动。
3.安装Jenkins插件,ant、maven、git、github、ssh,再也不表述。
4.在Jenkins的系统管理中,设置jdk、ant、maven、git的执行路径,添加ssh账号。

git
maven
ssh

5.新建job,选择“构建一个maven项目”,设置git的地址

源码

6.设置maven goals,添加post steps,这里执行shell脚本,进行部署web。

maven goals
这里写图片描述

7.保存,点击当即构建,构建成功后会在workplace中生成war包。

war

8.能够使用Jenkins的插件Deploy Plugin进行部署,也能够使用shell脚本进行部署,这里使用shell脚本进行部署。
(Jenkins和web服务器在同一台机器上,若在不一样机器上,请参考ssh无密登陆)

mydeploy.sh 部署脚本参考:web

#! /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