您的位置:首页 > 运维架构 > Docker

Git+Jenkins+Docker实现web项目的自动部署

2017-02-06 11:01 1086 查看
实验中使用的Git平台:

GitOsc

实验目的:

向GitOsc上传新代码后,自动通过Maven打包,然后构建相应的Tomcat镜像,并启动容器

一、准备GitOsc:
1.准备项目文件



2.准备GitOsc



注:
提示输入用户名和密码的时候,用户名使用完整的邮箱名

3.上传原始文件
git add *
git commit -m "AddFile"
git push origin master




二、配置Jenkins:







































三、在GitOsc上配置PUSH钩子:



http://ghx:63919fc15e0b82c9b8c3ac47af8dabbd@180.172.232.87:9999/job/test-hello/build?token=test-token
//必须使用Jenkins的公网IP,注意映射9999端口和50000端口

四、修改hello项目的源代码,并pushGitOsc上:
1.修改源代码




2.上传新文件
git add *




git commit -m "Add newfile"
git push

效果:














shell脚本:
Registry_URL=192.168.1.200:5000
tmpfile=/root/tempfile
if [ -e $tmpfile ] ; then
sum=`cat $tmpfile`
i=`expr $sum + 1`
else
sum=0
i=`expr $sum + 1`
fi
docker build -tmaven-hello:test $WORKSPACE/hello-maven
docker create --namemaven-hello maven-hello:test
docker cpmaven-hello:/hello/target/hello.war $WORKSPACE/hello-tomcat
docker build -t$Registry_URL/tomcat-hello:test${i} $WORKSPACE/hello-tomcat
docker push$Registry_URL/tomcat-hello:test${i}
if docker ps -a |grep -itomcat-hello ; then
docker rm -f tomcat-hello
fi
docker run -d -p 8888:8080--name tomcat-hello $Registry_URL/tomcat-hello:test${i}
if docker images$Registry_URL/tomcat-hello | grep test${sum}  ; then
docker rmi -f$Registry_URL/tomcat-hello:test${sum}
fi
if docker ps -a |grep -imaven-hello ; then
docker rm -f maven-hello
fi
if docker images maven-hello |grep test  ; then
docker rmi -f maven-hello:test
fi
echo $i >$tmpfile


脚本作用:

1.设置一个临时文件,记录该任务执行次数,用于做Tag
2.构建顺序:
使用源代码构建maven镜像,同时制作成war包->从maven容器中导出war包->构建新的tomcat镜像->push到私有仓库
3.删除原来的tomcat容器,并通过最新的tomcat镜像运行一个新的tomcat容器
4.依次删除不必要的容器&镜像:
删除旧的tomcat镜像->删除刚创建的maven容器->删除刚构建的maven镜像
5.最后向临时文件中传入累计数,供下次任务使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Git Jenkins Docker