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,版本下载地址不再提供。

1.Linux上安装jdk,maven,ant,git,不再表述。需要注意的是,要将ssh公钥添加到github中,具体如下:
[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 yuanwq@163.com
The key's randomart image is:

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

jenkins+maven+github持续集成_第1张图片
开源中国生成公钥帮助链接

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

jenkins+maven+github持续集成_第2张图片
jenkins+maven+github持续集成_第3张图片
jenkins+maven+github持续集成_第4张图片

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

jenkins+maven+github持续集成_第5张图片

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

jenkins+maven+github持续集成_第6张图片
jenkins+maven+github持续集成_第7张图片

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

jenkins+maven+github持续集成_第8张图片

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

mydeploy.sh 部署脚本参考:

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

你可能感兴趣的:(jenkins,持续集成)