K8sDevOpsDevOps

jenkins mvn gitlab pipeline流水线构建

2022-03-09  本文已影响0人  andrewkk
Dockerfile 放置gitlab仓库中管理
FROM java
MAINTAINER xxxx@qq.com
WORKDIR /workspace
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ADD xxx-admin/target/xx-admin.jar /workspace/
ENTRYPOINT ["java", "-Duser.timezone=GMT+08","-Xms4096m","-Xmx4096m", "-jar", "/workspace/xxx-admin.jar"]

image.png
pipeline脚本
node{
    stage('get clone'){
        checkout([$class: 'GitSCM', branches: [[name: '$branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitlab.xxxx.com/xx/xxx.git']]])
    }

    stage('mvn build'){
        sh "mvn clean package -Dmaven.test.skip=true"
    }

    stage('docker push'){
        sh "pwd && cd hxkj-admin && ls -l && pwd"
        echo "docker push 推送最新打包好的镜像 ......" 
    }
    def dockerfile = 'Dockerfile'
    def customImage = docker.build("10.16.0.00:5000/xx/xx-admin:latest", "-f ./xxx-admin/${dockerfile} .")
    customImage.push()

    stage('deploy container prod xxx-admin-162'){
        sh '''
           ssh 10.1.0.xxx "bash /mnt/data/xxx-admin-prod.sh"
        '''
        echo "==========发布完成============"
    }
}


shell发布脚本
#!/bin/sh
IMAGE_NAME=xx/xx-admin
CONTAIN_TAG_NAME=xx-admin
REGISTRY_TAG_NAME=10.16.0.xxx:5000/$IMAGE_NAME
TAG=latest
CONTAINERID=`docker ps -a | grep "$CONTAIN_TAG_NAME"  | awk '{print $1}' `
                if [ "$CONTAINERID" == "" ]; then
                        echo "=== 容器Id不存在 ==="
                else
                        echo "=== 容器ID存在 ==="
                        echo "=== $CONTAIN_TAG_NAME 容器实例ID为 : $CONTAINERID ==="
                        echo "=== 停止该容器 ... "
                        docker stop  $CONTAINERID
                        docker rm  $CONTAINERID
                        sleep 2
                fi
echo  "=== 删除上一个版本旧的镜像 ==="
docker rmi -f $REGISTRY_TAG_NAME:$TAG
echo  "===清理所有tag标签是none的镜像 ==="
docker images|grep none|awk '{print $3 }'|xargs docker rmi
echo "=== 拉取新镜像 ==="
docker pull $REGISTRY_TAG_NAME
echo "=== 当前构建的镜像为:"
docker images | grep $IMAGE_NAME
echo "=== 启动容器 ==="
docker run -d --name $CONTAIN_TAG_NAME -p 80:xxx -e "SPRING_PROFILES_ACTIVE=produat" -v /data/logs/:/home/spring-logs/ -v /data/cert/:/data/cert/ -v /data/files/:/data/files/ $REGISTRY_TAG_NAME
echo "=== 当前容器 ==="
docker ps -a | grep $CONTAIN_TAG_NAME
echo "=== 启动服务成功! ==="
docker tag $REGISTRY_TAG_NAME $REGISTRY_TAG_NAME:$TAG-`date +%Y%m%d%H%M`
docker push $REGISTRY_TAG_NAME:$TAG-`date +%Y%m%d%H%M`
echo "====== 打个tag标签 push-->> 私库 备份完成====="
docker images | grep $IMAGE_NAME
echo "====== 检查仓库 ======"
curl 10.16.0.00:5000/v2/xx/xxx-admin/tags/list

image.png

后续根据私有镜像备份好的版本去回滚镜像即可 不需要每次备份jar 回滚待更新


image.png
上一篇下一篇

猜你喜欢

热点阅读