jenkins+docker+springboot 持续化集成

1. 安装 Jenkins + maven + jdk + git

2. spring boot 项目添加Dockerfile

jenkins+docker+springboot 持续化集成_第1张图片
这里没有使用dockermaven插件,直接原生 Dockerfile,保持代码侵入最低。

FROM 172.16.0.57:5000/openjdk:8-jdk-alpine
MAINTAINER [email protected]
VOLUME /var/log/tiannake/hibernate
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
ENV LANG zh_CN.UTF-8
#ARG JAR_FILE
#ADD ${JAR_FILE} app.jar
ADD target/hibernate.jar app.jar
ENTRYPOINT ["java","-Xms1024m","-Xmx1024m","-Dspring.profiles.active=test","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

FROM 172.16.0.57:5000/openjdk:8-jdk-alpine 这个是我的docker私服里面的基础镜像,去掉 172.16.0.57:5000则从 dockerhuber上下载该基础镜像。

VOLUME /var/log/tiannake/hibernate 这个是我springboot 日志打印的路径。

ADD target/hibernate.jar app.jar 这个我项目编译后统一路径在 target下面,编译的finalName是hibernate.jar

3. 配置 Jenkins 全局系统工具

jenkins+docker+springboot 持续化集成_第2张图片
jenkins+docker+springboot 持续化集成_第3张图片

4. 配置 Jenkins 新建一个maven项目

jenkins+docker+springboot 持续化集成_第4张图片
jenkins+docker+springboot 持续化集成_第5张图片
jenkins+docker+springboot 持续化集成_第6张图片

最重要的是编译后的shell脚本
先删除容器,镜像后创建镜像,启动容器。

#!/bin/bash 

#export BUILD_ID=dontKillMe这一句很重要,这样指定了,项目启动之后才不会被Jenkins杀掉。
export BUILD_ID=dontKillMe

echo "动态参数配置 begin===============>"
CDNAME=hibernate-2.0
echo "image and container name is $CDNAME"

# springboot启动的端口号
BootPort=8101
echo "the spring boot port is $BootPort"

# dockerfile里面指定的 Volume 即日志打印的路径
dockerLogVolume=/var/log/tiannake/hibernate
echo "the dockerLogVolume is $dockerLogVolume"

# dockerfile里面指定的 Volume 映射外面磁盘的路径
linuxLogPath=/log/docker/tannaike
echo "the linuxLogPath is $linuxLogPath"
echo "动态参数配置 end"


echo "begin create the container..."
echo "stop and delete container"
CID=$(docker ps -a | grep "$CDNAME" | awk '{print $1}')

if [ -n "$CID" ]; then
echo "has container,CID=$CID"
docker stop $CID
docker rm $CID
fi

echo "delete image"
IMD=$(docker image ls | grep "$CDNAME" | awk '{print $1}')

if [ -n "$IMD" ]; then
echo "has image,IMD=$IMD"
docker rmi $IMD
fi

echo "pwd..."
pwd

echo "build docker image"
# mvn dockerfile:build
docker build -f Dockerfile -t $CDNAME:latest .

echo "current docker images:"
docker images | grep $CDNAME

echo "start container ===============> "
docker run -d -p $BootPort:$BootPort -v $linuxLogPath:$dockerLogVolume --name $CDNAME $CDNAME:latest


CID=$(docker ps | grep "$CDNAME" | awk '{print $1}')

if [ -n "$CID" ]; then

echo "============ start container,CID=$CID ============"
echo "============ start container SUCCESS!============"

else

echo "============ start container FAIL !!!!!============"

fi

你可能感兴趣的:(jenkins,docker,springboot,Dockerfile,持续集成,运维)