当前位置:   article > 正文

微服务Git+Docker+Jenkins+Maven一键CI/CD持续集成部署_cicd 一键上线

cicd 一键上线

一、介绍

本文介绍利用Git+Docker+Jenkins+Maven实现一键CI/CD持续集成部署,需要的软件有GitDockerJenkinsMavenJenkins需要集成GitMaven插件,实现代码拉取和代码打包工作;本文思路就是利用GitGit平台拉取源代码,然后利用Maven打包,然后使用Docker构建镜像,将镜像上传到自己的DockerHub私服,其它服务器从DockerHub私服上拉取镜像进行部署操作;简单流程图如下:
image.png

PS:如何搭建Docker镜像私服可以参考:Docker Hub私服Registry搭建与使用

二、Jenkin任务及配置

  • 新建任务
    image.png
  • 任务名称
    image.png
  • 配置任务
    image.png
    image.png
    image.png

Jenkins执行make preview命令,参照Makefile的相关内容;

三、Makefile文件相关内容

  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

PS:需要注意命令chmod a+x ./hack/*.sh前面是一个tab键,如果是空格,不是tab键会导致错误:命令前面是tab分割符,不能用空格,否则会出现makefile:2: *** 遗漏%分隔符 。 停止。
可以用命令vi Makefile编辑文件,然后进行 :set list操作查看
image.png

四、Dockerfile

  • 根据Dockerfile文件构建镜像
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • ./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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • ./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 ;"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

PS:Git+Docker+Jenkins+Maven一键CI/CD持续集成部署记录大概内容,这样应该就可以持续集成部署了;记录备忘,后期有问题了再更新;Makefile和Dockerfile文件目录结构如图:

image.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/130701
推荐阅读
相关标签
  

闽ICP备14008679号