實驗中使用的Git平臺:
git
GitOscdocker
實驗目的:shell
向GitOsc上傳新代碼後,自動經過Maven打包,而後構建相應的Tomcat鏡像,並啓動容器tomcat
1、準備GitOsc:bash
1.準備項目文件maven
2.準備GitOscide
注:ui
提示輸入用戶名和密碼的時候,用戶名使用完整的郵箱名spa
3.上傳原始文件3d
git add *
git commit -m "AddFile"
git push origin master
2、配置Jenkins:
3、在GitOsc上配置PUSH鉤子:
http://ghx:63919fc15e0b82c9b8c3ac47af8dabbd@180.172.232.87:9999/job/test-hello/build?token=test-token
//必須使用Jenkins的公網IP,注意映射9999端口和50000端口
4、修改hello項目的源代碼,並push到GitOsc上:
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 p_w_picpaths$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 p_w_picpaths 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.最後向臨時文件中傳入累計數,供下次任務使用