• [转]利用Jenkins的Pipeline实现集群自动化部署SpringBoot项目


    环境准备

    • Git: 安装部署使用略。
    • Jenkins: 2.46.2版本安装部署略(修改jenkins执行用户为root,省得配置权限)
    • JDK: 安装部署略。
    • Maven: 安装部署略。
    • 服务器免密登陆

    Jenkinsfile文件编写

    node {
       def mvnHome
       def workspace = pwd()
       stage('Preparation') { // for display purposes
          // Get some code from a GitHub repository
          git 'http://192.168.161.118:8080/git/demo.git'
          // Get the Maven tool.
          // ** NOTE: This 'M3' Maven tool must be configured
          // ** in the global configuration.           
          mvnHome = tool 'M3'
       }
       stage('Build') {
          // Run the maven build
          if (isUnix()) {
             sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
     
          } else {
             bat(/"${mvnHome}inmvn" -Dmaven.test.failure.ignore clean package/)
          }
       }
       stage('Deploy') {
          sh "'/scripts/deploy.sh' ${workspace} deploy"
       }
    }
    

    Jenkinsfile文件就放在你自己的Git仓库的更目录! 如图:输入图片说明

    在Jenkins中创建Pipeline项目

    输入图片说明 输入图片说明 输入图片说明 如上3图,3步,jenkins的pipeline项目创建完成。

    编写部署Shell脚本

    #!/bin/bash
    #集群IP列表,多个用空格分开
    #NODE_LIST="192.168.161.118 192.168.161.117"
    NODE_LIST="192.168.161.245"
    #应用部署到的远程服务器目录
    REMOTE_DIR="/home/project" 
    #需要部署的项目名称(需和maven的project名一样,多个用空格分开)
    #NEED_DEPLOY_PROJECT="user-server user-mgr info-mgr"
    NEED_DEPLOY_PROJECT="user-mgr"
    # Date/Time Veriables
    LOG_DATE='date "+%Y-%m-%d"'
    LOG_TIME='date "+%H:%M:%S"' 
    CDATE=$(date "+%Y%m%d")
    CTIME=$(date "+%H%M%S") 
    #Shell Env
    SHELL_NAME="deploy.sh"
    SHELL_DIR="/deploy/log"
    SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log" 
    #Code Env
    JAR_DIR="/deploy/jar"
    CONFIG_DIR="/deploy/config"
    LOCK_FILE="/tmp/deploy.lock" 
    usage(){
        echo  $"Usage: $0 [projectJarPath] [ deploy | rollback ]"
    } 
    init() {
        create_dir $SHELL_DIR;
        create_dir $JAR_DIR;
        create_dir $CONFIG_DIR;
    }
    create_dir() {
       if [ ! -d $1 ]; then  
           mkdir -p $1
       fi
    }
    shell_lock(){
        touch ${LOCK_FILE}
    }
    shell_unlock(){
        rm -f ${LOCK_FILE}
    }
    write_log(){
        LOGINFO=$1
        echo "`eval ${LOG_DATE}` `eval ${LOG_TIME}` : ${SHELL_NAME} : ${LOGINFO}"|tee -a ${SHELL_LOG}
    }
    #拷贝jenkins的工作空间构建的jar包到特定目录,备份,为以后回滚等等操作
    copy_jar() { 
        TARGET_DIR=${JAR_DIR}/${CDATE}${CTIME}
        write_log "Copy jenkins workspace jar file to ${TARGET_DIR}"
        mkdir -p $TARGET_DIR
        for project in $NEED_DEPLOY_PROJECT;do
          mkdir -p $TARGET_DIR/${project}
          find $1 -name ${project}*.jar -exec cp {} $TARGET_DIR/${project}/ ;
        done 
    } 
    #拷贝应用的jar包到远程服务器
    scp_jar(){
        SOURCE_DIR=${JAR_DIR}/${CDATE}${CTIME}
        write_log "Scp jar file to remote machine..."
        for node in $NODE_LIST;do
          scp -r ${SOURCE_DIR}/* $node:${REMOTE_DIR}
          write_log "Scp to ${node} complete."
        done
    } 
    # 杀掉远程服务器上正在运行的项目
    cluster_node_remove(){
        write_log "Kill all runing project on the cluster..."
        for project in $NEED_DEPLOY_PROJECT;do
          for node in $NODE_LIST;do
            pid=$(ssh $node "ps aux|grep ${project}|grep -v grep|awk '{print $2}'"|awk '{print $2}')
            if [ ! -n "$pid" ]; then  
          write_log "${project} is not runing..."
        else  
              ssh $node "kill -9 $pid"
          write_log "Killed ${project} at ${node}..."
        fi  
          done
        done
    } 
    #在远程服务器上启动项目
    cluster_deploy(){
        write_log "Up all project on the cluster..."
        for project in $NEED_DEPLOY_PROJECT;do
          for node in $NODE_LIST;do
            ssh $node "cd ${REMOTE_DIR}/${project};nohup java -jar ${project}*.jar >/dev/null 2>&1 &" 
        write_log "Up ${project} on $node complete..."
          done
        done
    } 
    #回滚(暂未实现)
    rollback(){
        echo rollback
    } 
    #入口
    main(){
        if [ -f ${LOCK_FILE} ];then
            write_log "Deploy is running"  && exit;
        fi
        WORKSPACE=$1
        DEPLOY_METHOD=$2
        init;
        case $DEPLOY_METHOD in
        deploy)
            shell_lock;
            copy_jar $WORKSPACE;
            scp_jar;
            cluster_node_remove;
            cluster_deploy;
            shell_unlock;
            ;;
        rollback)
            shell_lock;
            rollback;
            shell_unlock;
            ;;
        *)
            usage;
        esac 
    }
    main $1 $2
    

    PS: deploy.sh放在/scripts目录,和JenkinsFile中写的路径一致就好

    运行看效果

    输入图片说明 部署成功。。。 输入图片说明 Jenkins的Console Output也都打印成功。 输入图片说明 浏览器访问部署的应用也一切正常。完事!

    PS:本实践适合小型集群部署。(经验有限,欢迎大神指导)

  • 相关阅读:
    Ubuntu创建用户,给予sudo权限
    Walking Robot Simulation
    python remove与del 的区别
    ML Foundation
    python any()和all()
    python3无穷大,无穷小
    eclipse maven无法下载依赖时
    eclipse自动生成spring项目时各种报错最后处理方案
    Python+Selenium+Unittest实现PO模式web自动化框架(3)
    Python+Selenium+Unittest实现PO模式web自动化框架(2)
  • 原文地址:https://www.cnblogs.com/sidesky/p/10679490.html
Copyright © 2020-2023  润新知