shell本地打包自动上传远程服务器

该脚本主要通过Expect模块实现SSH登录远程服务器,进行文件删除、目录创建以及文件上传操作。它首先安装必要的包,然后执行Vue项目的打包,通过SCP将打包后的文件上传到远程服务器的ApacheTomcat目录下,实现自动化部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备:

需要安装expect

yum install -y expect

yum install -y tcl-devel

打包上传脚本

#!/bin/bash
# by 

readonly name='deploy2server'
readonly version='0.0.1'

readonly serve_name='root'
readonly serve_passowrd='密码'
readonly serve_host='服务器IP地址'
readonly serve_path="/opt/apache-tomcat/webapps"

# 初始化
cd $(dirname $0)
_work_path=$(pwd)   # 取到脚本目录

# _project_root_path="/opt/codeproject/web-modules"
_project_root_path=$(pwd)
_siteName_list=("sitename" )


# def 远程服务器操作
auto_ssh(){
    __host=$1
    __name=$2
    __password=$3
    __todo=$4
    /usr/bin/expect <<EOD
    set timeout 86400
    spawn ssh ${__name}@${__host} ${__todo}
    expect {
        "*yes/no*" {send "yes\r"; exp_continue}
        "*password:" {send "$__password\r"; exp_continue}
        "*Password:" {send "$__password\r"}
    }
EOD
}
# def 上传远程服务器
auto_scp() {
    __host=$1
    __name=$2
    __password=$3
    __src=$4
    __dest=$5
    /usr/bin/expect <<EOD
    set timeout 86400
    spawn scp -r ${__src} ${__name}@${__host}:${__dest}
    expect {
        "*yes/no*" {send "yes\r"; exp_continue}
        "*password:" {send "$__password\r"; exp_continue}
        "*Password:" {send "$__password\r"}
    }
EOD
}

# 执行打包上传
auto_build() {
     __vue_project_path=$1
     __remote_site_rootpath=$2

    # 打包 vue
    echo -e "\033[32m vue 打包 \033[0m"

    cd ${__vue_project_path}

    # 删除上一次的依赖和打包的dist目录 
    rm -rf ./dist ./node_modules
    # 重新安装依赖
    npm install
    # 重新打包
    npm run build
    # 打包成功执行下一步
    if [ -d dist ]; then
    
        # 删除远程指定目录下的static、index.html文件
        # echo -e "\033[32m 删除远程指定目录下的static、index.html文件 \033[0m"
        # auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "rm -rf ${__remote_site_rootpath}/static && rm -rf ${__remote_site_rootpath}/assets && rm -rf ${__remote_site_rootpath}/index.html "
        # 删除远程远程服务器下的站点下对应的dist目录同名的文件和目录
        del_remote_file_command_joint $(pwd)/dist ${__remote_site_rootpath}

        # 上传打包的文件到远程服务器
        echo -e "\033[32m vue 上传服务器 \033[0m"
        auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "mkdir -p ${__remote_site_rootpath}"
        # 复制dist目录到远程服务器上
        auto_scp ${serve_host} ${serve_name} ${serve_passowrd} $(pwd)/dist ${__remote_site_rootpath}
        # 清理删除远程服务器上的dist目录
        auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "mv ${__remote_site_rootpath}/dist/* ${__remote_site_rootpath}  && rm -rf ${__remote_site_rootpath}/dist "
        # OK!!
        echo -e "\033[32m 完成 \033[0m"
        # 函数返回状态1
        return 1
    else
        # 函数返回状态0
        echo -e "\033[32m 打包失败 \033[0m"
        return 0
    fi

}

del_remote_file_command_joint() {
    _dist_path=$1
    _remote_path=$2

    # 在当前目录下创建文本文件temp,如果文件存在则清空文件
    $(> temp)

    for file in $1/*
    do
    # 提取目录下的文件、目录的名称,拼接删除命令
        # echo "---->"${file}
        echo -n "rm -rf "${_remote_path}/${file##*/}" && " >> temp
    # if test -f $file
    # then
    #     echo $file
    #     arr=(${arr[*]} $file)
    # else
    #     getdir $file
    # fi
    done
    # echo ${arr[@]}
        
    # str取temp文本里的字符串
    str=$(cat temp)
    # 将字符串最后的3个&& 字符去掉
    str=${str::-3}
    echo ${str}

    # 执行远程删除命令
    auto_ssh ${serve_host} ${serve_name} ${serve_passowrd} "${str}"

    # 删除临时temp文件
    rm -rf temp
}


#!/bin/bash
beginTime=`date +%s`

for(( i=0;i<${#_siteName_list[@]};i++))
do
    {   
        echo $i  "${_siteName_list[i]} 开始执行,当前时间:" `date "+%Y-%m-%d %H:%M:%S"`
        
        echo ${_siteName_list[i]}

        _vue_project_path=${_project_root_path}/-${_siteName_list[i]}-site/src/main/front-end
        
        _remote_dest_path="${serve_path}/${_siteName_list[i]}"

        echo ${_vue_project_path} ${_remote_dest_path}

        # 执行打包上传
        auto_build ${_vue_project_path} ${_remote_dest_path}
        
        echo $i  "${_siteName_list[i]} 执行完成,当前时间:" `date "+%Y-%m-%d %H:%M:%S"`
        echo "-----------------------------------------------------------"

    # 结尾的&确保每个进程后台执行
    }&
done

# wait关键字确保每一个子进程都执行完成
wait
endTime=`date +%s`
echo "总共耗时:" $(($endTime-$beginTime)) "秒"

参考文章:
Shell-使用&和wait让你的脚本并行执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值