Spring boot docker Jenkins automatic deployment and image upload

Keywords: Java Docker jenkins snapshot SpringBoot

springboot + docker + jenkins automatic deployment project. jenkins, mysql and redis are all operated by docker without using virtual machine. They are running on Alibaba cloud server (centos7)

1. Don't talk about the preparatory work

2. Create a new Dockerfile in the root directory of the project

The contents of the Dockerfile file are:

#base image 
FROM openjdk:12
#author
MAINTAINER demo <demo@qq.com>

VOLUME /tmp
#Specify the configuration file and the path of the jar package on the server
ENTRYPOINT ["java","-Dspring.profiles.active=prod","-jar","/lcy/work/tools/tools.jar"]
#Exposed port
EXPOSE 8092

3. Find a directory on the server and create a new. sh file

#!/bin/sh
echo '================Start building images=============='
#Image name
IMAGE_NAME='registry.cn-beijing.aliyuncs.com/???/tools'
#Packed at jenkins address
SOURCE_PATH='/lcy/jenkins/workspace/tools'
#Address of Dockerfile to execute jar package
BASE_PATH='/lcy/work/tools'
echo IMAGE_NAME=$IMAGE_NAME

echo '================copy JAR package==================='
echo $SOURCE_PATH/target/tools-0.0.1-SNAPSHOT.jar
cp $SOURCE_PATH/target/tools-0.0.1-SNAPSHOT.jar $BASE_PATH/tools.jar
chmod -R 777 $BASE_PATH/tools.jar
echo '================copy complete===================='

echo '================current docker Edition=============='
docker -v

echo '================Build image start================'
docker build -t $IMAGE_NAME -f $SOURCE_PATH/Dockerfile .
echo '================End of building image================'
#Enter the address to push the image, and copy it according to the address prompted by the image warehouse
echo '================Push image start================'
docker login --username=??? --password=??? registry-vpc.cn-beijing.aliyuncs.com
docker push $IMAGE_NAME
echo '================Push image end================'

echo '================Get container id=================='
CID=$(docker ps | grep "$IMAGE_NAME" | awk '{print $1}')
echo container id=$CID

echo '================Get image id=================='
IID=$(docker images | grep "$IMAGE_NAME" | awk '{print $3}')
echo image id=$IID

if [ -n "$CID" ]; then
    echo existence $IMAGE_NAME container,Stop container and delete
    docker stop tools
    docker rm tools
else
    echo non-existent $IMAGE_NAME container,Start up
    docker run -p 8092:8092 -d --name tools -v $BASE_PATH:$BASE_PATH $IMAGE_NAME
fi

4. Configure jenkins, execute the new. sh file (remember to give permission), and other configurations will not be mentioned, such as github configuration

5. Operation results

This article is based on the platform of blog one article multiple sending OpenWrite release!

Posted by MyWebAlias on Mon, 04 May 2020 02:13:46 -0700