GitLab+Jenkins+Docker 完成SpringBoot项目的持续集成
部署介绍
传统的项目持续集成,通常由gitlab+jenkins完成项目的持续集成部署。随着容器化技术的日益成熟,很多项目都采用容器化部署管理,本章节则会给大家介绍怎么完成docker项目的持续集成部署
部署架构
部署准备
项目docker化
以springboot项目test-service为例说明,首先需要新建一个Dockerfile文件,内容如下:
FROM java:8
ENV LANG=C.UTF-8
ENV TZ=Asia/Shanghai
ADD test-service.jar app.jar
CMD ["sh","-c","java -Xmx512m -Xms256m -XX:SurvivorRatio=4 -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -jar -XX:InitiatingHeapOccupancyPercent=70 -Xloggc:/var/log/java_gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps app.jar "]
制作生成镜像制品和执行运行容器的脚本
docker images | grep test-service/latest &> /dev/null
if [ $? -ne 0 ]
then
echo "test-service/latest is not existed,we will docker build it!!!"
docker build -t test-service/latest .
else
echo "test-service/latest is existed!!!"
docker rmi -f test-service/latest
docker build -t test-service/latest .
fi
docker ps -a| grep test-service &> /dev/null
if [ $? -ne 0 ]
then
echo "test-service is not up,we will start up it!!!"
docker run -d -p 8099:8099 --name test-service test-service/latest
else
docker rm -f metadata-service
docker run -d -p 8099:8099 --name test-service metadata-service/latest
echo "test-service is up!!!"
fi
Jenkins部署配置
构建环境
其中clean的参数说明如下图所示:
构建配置
执行的脚本如下:
cd /home/data/
rm -rf test-service
mkdir test-service
cp /var/lib/docker/volumes/jenkins_data/_data/workspace/test-service/test-service/target/test-service.jar /home/data/test-service
cp /var/lib/docker/volumes/jenkins_data/_data/workspace/test-service/test-service/Dockerfile /home/data/test-service
cp /var/lib/docker/volumes/jenkins_data/_data/workspace/test-service/test-service/dockerBuild.sh /home/data/test-service
cd /home/data/test-service
chmod -R 777 dockerBuild.sh
./dockerBuild.sh