赞
踩
本文介绍利用Git
+Docker
+Jenkins
+Maven
实现一键CI/CD持续集成部署,需要的软件有Git
、Docker
、Jenkins
、Maven
;Jenkins
需要集成Git
和Maven
插件,实现代码拉取和代码打包工作;本文思路就是利用Git
从Git
平台拉取源代码,然后利用Maven
打包,然后使用Docker
构建镜像,将镜像上传到自己的DockerHub
私服,其它服务器从DockerHub
私服上拉取镜像进行部署操作;简单流程图如下:
PS:如何搭建Docker镜像私服可以参考:Docker Hub私服Registry搭建与使用
Jenkins
执行make preview
命令,参照Makefile的相关内容;
TAG?=latest default: build build: mvn -DskipTests clean package run: mvn spring-boot:run image: clean build docker build --no-cache -t s3.ok.un-net.com:5000/oneclick/client-gateway:$(TAG) . push: image docker push s3.ok.un-net.com:5000/oneclick/client-gateway:$(TAG) rmi: docker images|grep none|awk '{print $3}'|xargs docker rmi clean: rm -rf ./target/ # release scdx: ./hack/build_scdx.sh lndx: ./hack/build_lndx.sh # dev preview: push #注意权限问题 chmod a+x ./hack/*.sh ./hack/update_local.sh
PS
:需要注意命令chmod a+x ./hack/*.sh
前面是一个tab
键,如果是空格,不是tab
键会导致错误:命令前面是tab分割符,不能用空格,否则会出现makefile:2: *** 遗漏%分隔符 。 停止。
可以用命令vi Makefile
编辑文件,然后进行:set list
操作查看
FROM tomcat:latest #TOMCAT环境变量 ENV CATALINA_BASE: /usr/local/tomcat \ CATALINA_HOME: /usr/local/tomcat \ CATALINA_TMPDIR: /usr/local/tomcat/temp \ JRE_HOME: /usr # add create user info MAINTAINER unnet "love1208tt@foxmail.com" # 将webapp下的全部删除 RUN rm -rf /usr/local/tomcat/webapps/* #启动入口 ENTRYPOINT ["catalina.sh","run"] COPY ./target/client-gateway-0.0.1-SNAPSHOT.war ${CATALINA_HOME}/webapps/ROOT.war EXPOSE 9080 8080 8090
PS:FROM tomcat:latest
基础镜像不同,Dockerfile
文件的内容也要相应修改,这个Dockerfile
适应我配置的war包,tomcat
的离线镜像包地址:链接: https://pan.baidu.com/s/1ggTE-Bf8spXVS36jswiVSg 提取码: mvpr 复制这段内容后打开百度网盘手机App,操作更方便哦
./hack/update_local.sh
#!/bin/bash # 进入当前Shell程序的目录 cd `dirname $0` echo "Current Script Path: `dirname $0`" mkdir -p /usr/local/services/client-gateway/bin cp -rf ./scripts/*.sh /usr/local/services/client-gateway/bin #./build_preview.sh sh /usr/local/services/client-gateway/bin/stop.sh #cp -rf ../target/client-gateway-1.0.0.jar /usr/local/services/client-gateway/bin sh /usr/local/services/client-gateway/bin/start.sh
./hack/scripts/start.sh
#!/bin/bash
#cd `dirname $0`
#
#mkdir -p ../logs
#touch ../logs/client-gateway.log
#
#nohup java -jar /usr/local/services/client-gateway/bin/client-gateway-1.0.0.jar >>../logs/client-gateway.log 2>&1 &
ssh root@s1.ok.un-net.com " docker pull s3.ok.un-net.com:5000/oneclick/client-gateway; docker run --name client-gateway -v /etc/localtime:/etc/localtime:ro -v /usr/local/docker/unnet-log/client-log/:/unnet-log/oneclick/ -d -p 8070:8080 s3.ok.un-net.com:5000/oneclick/client-gateway"
ssh root@s2.ok.un-net.com " docker pull s3.ok.un-net.com:5000/oneclick/client-gateway; docker run --name client-gateway -v /etc/localtime:/etc/localtime:ro -v /usr/local/docker/unnet-log/client-log/:/unnet-log/oneclick/ -d -p 8070:8080 s3.ok.un-net.com:5000/oneclick/client-gateway"
./hack/scripts/stop.sh
#!/bin/bash
#ps aux | grep "/usr/local/services/client-gateway/bin/client-gateway-1.0.0.jar" | grep -v grep | awk '{print $2}' | xargs kill -9
ssh root@s1.ok.un-net.com "docker rm -f client-gateway ; docker rmi -f s3.ok.un-net.com:5000/oneclick/client-gateway ;"
ssh root@s2.ok.un-net.com "docker rm -f client-gateway ; docker rmi -f s3.ok.un-net.com:5000/oneclick/client-gateway ;"
PS
:Git+Docker+Jenkins+Maven一键CI/CD
持续集成部署记录大概内容,这样应该就可以持续集成部署了;记录备忘,后期有问题了再更新;Makefile和Dockerfile文件目录结构如图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。