zoukankan      html  css  js  c++  java
  • 【CI/CD】Jenkins部署SpringBoot项目jar包,自定义参数脚本

    由于项目需要部署SpringBoot项目,本次说明下Jenkins中关于部署脚本的内容。

    下载Persistent Parameter Plugin

     构建任务添加自定义参数

    如下参数对应部署脚本中要传入的构建参数。

     

     

     构建任务中编译命令

     构建任务中部署命令

    更多种的Jenkins参数化用法参考:

     https://www.cnblogs.com/tylerzhou/p/11427566.html?utm_source=tuicool&utm_medium=referral

    如下部署脚本中包含

    1、检测服务端口是否存在,存在则停服务;

    2、检测部署目录是否存在,不存在的话新建目录,并开始部署;

    3、备份部署路径下的内容到备份文件夹;

    4、复制已拷贝到服务器临时目录的代码、配置文件到部署路径;

    5、启动服务,并检测服务起来;

        #key-value
        #${DEPLOY_TMP_PATH}  /data/ci
        #${SERVICE_PATH} /data/project/jar/ksports-uc-service
        #${SERVICE_NAME} ksports-uc-service
        #${SERVICE_PORT} application.yml 2210
        #${PACKAGE_NAME} ksports-uc-server-1.0.0-SNAPSHOT.jar
    
        #deploy
        deploy() {
            echo "[3]start to backup!"
            if [ -d ${SERVICE_PATH} ];then
                fileName_dir=${SERVICE_NAME}.Build10.$(date +%Y%m%d-%H:%M:%S)
                if [ -d /data/ci/backup/ ];then
                    echo "[3.1]start to rm -rf /data/ci/backup"
                    rm -rf /data/ci/backup
                fi
                if [ ! -d /data/ci/backup/$fileName_dir ];then
                    echo "[3.2]start to mkdir -p /data/ci/backup/$fileName_dir"
                    mkdir -p /data/ci/backup/$fileName_dir
                fi
                if [ -d ${SERVICE_PATH} ];then
                    echo "[3.1]start to cp -f ${SERVICE_PATH}/* /data/ci/backup/"
                    cp -f ${SERVICE_PATH}/* /data/ci/backup/$fileName_dir
                fi
            fi
            
            #return to deploy tmp
            echo "[4]start to copy unzip and replace config file!"
            cd $CURR_DIR
            cp -f ${PACKAGE_NAME} ${SERVICE_PATH}
            cd ${SERVICE_PATH}
            #note if need to unzip,Add the script below here!
            
            #note if need to replace config file ,Add the script below here!
            
            echo  "[5]start to start service!"
            cd ${SERVICE_PATH}
            chmod +x *.sh
            #note change your xxx.sh and command
            ./onekey.sh start
            for((i=1;i<=10;i++)); 
              do
                sleep 5
                isServerExist=$(netstat -ntlp|grep ${SERVICE_PORT}|grep -v "grep"|wc -l)
                if [ "$isServerExist" -ge 1 ]; then
                    echo "[5.1]${SERVICE_NAME} server start success!"
                    break
                else
                    echo "[5.2]${SERVICE_NAME} server start...  plese wait!"
                fi
            done
            if [ "$isServerExist" == "0" ]; then
                echo "[5.3]${SERVICE_NAME} server start ERROR!"
                exit 1
            fi
        }
        
    
        echo "[0]cd ${DEPLOY_TMP_PATH}"
        source /etc/profile
        cd ${DEPLOY_TMP_PATH}
        CURR_DIR=$(pwd)
    
        #stop service
        echo "[1]start to stop service!" 
        isServerExist=$(netstat -ntlp |grep ${SERVICE_PORT}|grep -v "grep"|wc -l)
        #When the process is exist,then isServerExist=1 or isServerExist=0
        #Note that the above commands query the process name according to the listening port number. Some services do not have listening ports, The command needs to be changed to query the process name.
        if [ "$isServerExist" = "0" ]; then
            echo "[1.1]${SERVICE_NAME} is not exist!"    
        elif [ "$isServerExist" -ge 1 ]; then
            echo "[1.2]${SERVICE_NAME} is exist.Start to stop it!"
            netstat -ntlp|grep ${SERVICE_PORT} |awk '{print $7}'|sed -e 's//java//g'|xargs kill -9
        fi
    
        # main 
        echo "[2]judge whether the target directory exists"
        if [ ! ${SERVICE_PATH} ];then
            echo "[2.1]ERROR,${SERVICE_PATH} is not set,plese check it!"
            exit 1
        elif [ -f ${SERVICE_PATH} ];then
            echo "[2.2]ERROR,${SERVICE_PATH} is a file!"
            exit 1
        elif [ ! -d ${SERVICE_PATH} ];then
            echo "[2.3]ERROR,${SERVICE_PATH} is not exist.Fist deploy,creat folder"
            mkdir -p ${SERVICE_PATH}
            pwd
            echo "[2.3.1]creat SUCCESS.start to deploy your project"
            deploy
        else
            echo "[2.4]SUCCESS,${SERVICE_PATH} is exist.start to deploy your project"
            deploy
        fi

     

    配置好后,部署成功。

    FAQ记录

    1、Jenkins虽然在最终的脚本执行中替换了参数,但是在Jenkins打印的控制台日志中,脚本还是显示的替换前的参数,一度让我认为替换没有成功。

      

  • 相关阅读:
    C#和SqlServer中处理时间格式问题
    ReadOnly之后获取文本框的值
    asp.net给Reaper和GridView添加序号
    SQL点滴31—SQL语句中@@IDENTITY和@@ROWCOUNT区别
    asp.net获取服务器信息
    Repeater嵌套
    开博说明 拓荒者
    Git的深入理解与GitHub托管服务的使用
    Hadoop学习笔记一:单节点安装
    VirtualBox克隆后无法找到网卡的问题
  • 原文地址:https://www.cnblogs.com/chooperman/p/14329625.html
Copyright © 2011-2022 走看看