Jenkins打包maven项目并推送私有仓库

  1. Jenkins打包maven项目并推送到私有仓库

在原来maven 项目编译打包部署的基础上改为直接执行脚本

Jenkins打包maven项目并推送私有仓库_第1张图片
#!/bin/bash
  
# jenkins配置的构建任务名称
PROJECT_NAME="qxx-jenkins"
  
# jenkins默认编译后jar包位置
JAR_PATH="/root/.jenkins/workspace/qxxDocker/target/"
  
# jar包名称,需要与pom.xml中的jar包坐标信息保持一致
JAR_NAME="qxx-0.0.1-SNAPSHOT.jar"
  
# 应用启动端口
PORT=8080
  
# Dockerfile maintainer
EMAIL="[email protected]"
  
# docker image名称
IMAGE_NAME="qxx"


# registry image名称
REGISTRY_IMAGE="qxx-registry"

# Dockerfile
DOCKERFILE_NAME="Dockerfile"
  
# echo输出标识,方便日志查看
LOG_TAG="CHIP_AUTO"



# -----------------------------------------------------
# 删除本地旧的docker镜像
# -----------------------------------------------------
function deleteOldImages(){
    echo "[$LOG_TAG]===========$PROJECT_NAME end,begin to execute shell==========="
    #获取当前镜像
    gatewayImages=`docker images|grep -i $IMAGE_NAME`
    #输出转义后的内容
    echo -e "[$LOG_TAG]Current existed $IMAGE_NAME images:\n$gatewayImages"
    #判断镜像是否为空
    if [ -n "$gatewayImages" ];then
        #获取镜像ID
        gatewayImagesId=`docker images|grep -i $IMAGE_NAME|awk '{print $3}'`
        #判断镜像ID是否为空
        if [ -n "$gatewayImagesId" ];then
            #转数组
            imageIdArr=($gatewayImagesId)
            #遍历数组
            for ele in ${imageIdArr[@]}
                do
                    imageId=$ele
                    echo "[$LOG_TAG]Delete image id:$imageId"
                    #移除旧镜像
                    docker rmi -f $imageId
                    #判断上一条删除旧镜像是否成功
                    if [ $? -eq 0 ];then
                        echo "[$LOG_TAG]Delete image id:$imageId success!"
                    else
                        echo "[$LOG_TAG]Delete image id:$imageId fail!"
                    fi
                done
        fi
    else
        echo "[$LOG_TAG]No existed $IMAGE_NAME images!"
    fi
}



# -----------------------------------------------------
# 打包docker镜像,推送至私仓
# -----------------------------------------------------
function packDockerImage(){
    #判断目录是否存在
    if [ -d $JAR_PATH ];then
        #进入目录
        cd $JAR_PATH
        echo "[$LOG_TAG]Current path:`pwd`"
        #获取Dockerfile文件
        dockerfile=`ls -l|grep $DOCKERFILE_NAME`
        if [ -n "$dockerfile" ];then
            #Dockerfile存在
            echo "[$LOG_TAG]Dockerfile has existed!"
        else
            #Dockerfile不存在
            #创建Dockerfile
            touch $DOCKERFILE_NAME
            #追加内容到文件中
            echo "FROM java:8-jre" >> $DOCKERFILE_NAME
            echo "MAINTAINER $EMAIL" >> $DOCKERFILE_NAME
            echo "ADD $JAR_NAME /$JAR_NAME" >> $DOCKERFILE_NAME
            echo "EXPOSE $PORT" >> $DOCKERFILE_NAME
            echo "ENTRYPOINT [\"/usr/bin/java\",\"-jar\",\"$JAR_NAME\"]" >> $DOCKERFILE_NAME
        fi
        #打包镜像
        docker build -t $IMAGE_NAME .
        if [ $? -eq 0 ];then
            #打包成功
            echo "[$LOG_TAG]$IMAGE_NAME image build success!"
            imagesId=`docker images|grep -i $IMAGE_NAME|awk '{print $3}'`
            #遍历镜像ID
            if [ -n "$imagesId" ];then
                #转数组
                imageIdArr=($imagesId)
                #遍历
                for ele in ${imageIdArr[@]}
                    do
                        imageId=$ele
                        echo "[$LOG_TAG]Latest $IMAGE_NAME image id:$imageId"
                    done
            fi
            #给镜像打标签
            docker tag $IMAGE_NAME $REGISTRY_IMAGE:latest
            #将本地的镜像上传到镜像仓库
            docker push $REGISTRY_IMAGE:latest
            #判断上一条命令是否执行成功
            if [ $? -eq 0 ];then
                echo "[$LOG_TAG]Execute->docker push $REGISTRY_IMAGE:latest success!"
            else
                echo "[$LOG_TAG]Execute->docker push $REGISTRY_IMAGE:latest fail!"
            fi
        else
            echo "[$LOG_TAG]$IMAGE_NAME image build fail!"
        fi
    else
        echo "[$LOG_TAG]Path:$JAR_PATH not existed!"
    fi
}



#删除旧镜像
deleteOldImages

#推送私仓
packDockerImage
2.查看镜像是否推送成功
Jenkins打包maven项目并推送私有仓库_第2张图片
3.其余服务器安装docker配置私仓后拉取镜像
Jenkins打包maven项目并推送私有仓库_第3张图片
4.启动容器(SpringBoot项目)
docker run -e spring.profiles.active=test -e JAVA_TOOL_OPTIONS="-Xms2064m -Xmx2064m" -p 8080:8080 -d d7871e877b6c
//获取JVM的堆大小
int mb = 1024 * 1024;
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
long xmx = memoryBean.getHeapMemoryUsage().getMax() / mb;
long xms = memoryBean.getHeapMemoryUsage().getInit() / mb;

你可能感兴趣的:(jenkins,maven,运维)