Jenkins+docker部署springboot项目

一、安装Jenkins(之后学习)

二、启动Jenkins

nohup java -jar jenkins.war --httpPort=8090 > jenkins.log 2>&1 &

三、配置mavan,git,jdk

20200105164912

四、新建一个任务

20200105165052

五、配置

1、 设置项目的源码路径

20200105165306

2、 设置构建方式,和执行的shell脚本

20200105165334

3、 附上我自己写的构建脚本
#!/bin/bash 

export BUILD_ID=dontKillMe
BASE_PATH=/root/.jenkins/workspace/
SOURCE_PATH=/root/.jenkins/workspace/
SERVER_NAME=$1

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

IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')

echo "最新构建代码 $SOURCE_PATH/$SERVER_NAME/target/ 迁移至 $BASE_PATH ...."

mv $SOURCE_PATH/$SERVER_NAME/target/$2 $BASE_PATH/$SERVER_NAME/make/app.jar

#修改文件的权限
chmod 777 $BASE_PATH/$SERVER_NAME/make/app.jar
echo "迁移完成"

# 构建docker镜像
if [ -n "$IID" ]; then
		echo "存在$SERVER_NAME镜像,IID=$IID"
		docker stop $CID
		docker rm  $CID
		docker rmi $IID    # 删除原来的容器
else
		echo "不存在$SERVER_NAME镜像,开始构建镜像"
fi
cd $BASE_PATH/$SERVER_NAME/make	
docker build -t $SERVER_NAME .
RUNNAME=$SERVER_NAME
if [ "$4" ];then
RUNNAME=$4

else
    echo "dss "

fi
docker run  -e JAVA_OPTS='-Xms128m -Xmx128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m'  --name $RUNNAME  -d -p $3:$3 $SERVER_NAME

NID=$(docker ps  | grep "$SERVER_NAME" | awk '{print $1}')

if [ -n "$NID" ]; then
                echo "$SERVER_NAME容器创建完成"
else
               echo "$SERVER_NAME容器创建失败"
fi
  • Dockerfile文件
FROM java:8
VOLUME /tmp
ADD app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

六、立即构建

1、 立即构建(启动)

20200105170018

2、 构建日志

20200105170106