web-map/自动部署脚本.bat

249 lines
7.0 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@echo off
setlocal enabledelayedexpansion
chcp 65001 > nul
title Web-AMR项目部署脚本
echo ==================== Web-AMR项目部署开始 ====================
:: 检查package.json是否存在
if not exist "package.json" (
echo [错误] 当前目录下未找到package.json文件请确保在项目根目录下执行此脚本
pause
exit /b 1
)
:: 询问是否需要打包
:ask_build
echo.
echo [选择] 是否需要重新打包项目?
echo 1. 是,执行 npm run build
echo 2. 否,使用现有的 dist 文件夹
echo.
set /p build_choice="请输入选择 (1/2): "
if "%build_choice%"=="1" goto do_build
if "%build_choice%"=="2" goto check_dist
echo [错误] 请输入有效选择 (1 或 2)
goto ask_build
:do_build
echo.
echo [步骤1] 开始构建项目...
echo 执行命令npm run build
call npm run build
if %errorlevel% neq 0 (
echo [错误] 构建失败,退出部署
pause
exit /b 1
)
echo [成功] 项目构建完成
goto choose_server
:check_dist
:: 检查dist文件夹是否存在
if not exist "dist" (
echo [错误] 未找到dist文件夹请选择重新打包
goto ask_build
)
echo [信息] 使用现有的 dist 文件夹
:choose_server
:: 服务器配置
set "server1_host=192.168.189.206"
set "server1_path=/var/www/web-map"
set "server1_user=vwedadmin"
set "server1_pass=Hangzhou@123456"
set "server1_name=生产服务器206"
set "server1_use_key=true"
set "server2_host=192.168.189.187"
set "server2_path=/var/www/web-map"
set "server2_user=ma"
set "server2_pass=N26BxdUxkM"
set "server2_name=测试服务器187"
set "server2_use_key=false"
set "server3_host=192.168.189.80"
set "server3_path=/var/www/web-map"
set "server3_user=ma"
set "server3_pass=2213@Aiit"
set "server3_name=开发服务器80"
set "server3_use_key=true"
:: 选择部署方式
:ask_deploy_mode
echo.
echo [选择] 请选择部署方式:
echo 1. 单个部署 - %server1_name% (%server1_host%)
echo 2. 单个部署 - %server2_name% (%server2_host%)
echo 3. 单个部署 - %server3_name% (%server3_host%)
echo 4. 批量部署 - 部署到所有服务器
echo 5. 自定义批量部署
echo.
set /p deploy_choice="请输入选择 (1-5): "
if "%deploy_choice%"=="1" (
set deploy_servers=1
goto start_deploy
)
if "%deploy_choice%"=="2" (
set deploy_servers=2
goto start_deploy
)
if "%deploy_choice%"=="3" (
set deploy_servers=3
goto start_deploy
)
if "%deploy_choice%"=="4" (
set deploy_servers=1,2,3
goto start_deploy
)
if "%deploy_choice%"=="5" goto custom_deploy
echo [错误] 请输入有效选择 (1-5)
goto ask_deploy_mode
:custom_deploy
echo.
echo [自定义批量部署] 请选择要部署的服务器多选用逗号分隔1,3
echo 1. %server1_name% (%server1_host%)
echo 2. %server2_name% (%server2_host%)
echo 3. %server3_name% (%server3_host%)
echo.
set /p deploy_servers="请输入服务器编号: "
goto start_deploy
:start_deploy
:: 检查scp命令是否可用
where scp >nul 2>&1
if %errorlevel% neq 0 (
echo [错误] 未找到scp命令请安装OpenSSH客户端
echo Windows 10用户可以通过以下方式安装
echo 设置 -^> 应用 -^> 可选功能 -^> 添加功能 -^> OpenSSH客户端
pause
exit /b 1
)
:: 创建临时压缩包
echo.
echo [信息] 正在创建临时压缩包...
powershell -Command "Compress-Archive -Path 'dist\*' -DestinationPath 'dist_temp.zip' -Force"
if %errorlevel% neq 0 (
echo [错误] 创建压缩包失败
pause
exit /b 1
)
echo [成功] 临时压缩包创建完成
:: 部署到选定的服务器
set success_count=0
set total_count=0
for %%s in (%deploy_servers%) do (
set /a total_count+=1
call :deploy_to_server %%s
if !errorlevel! equ 0 (
set /a success_count+=1
)
)
:: 清理本地临时文件
if exist "dist_temp.zip" del dist_temp.zip
echo [成功] 本地临时文件清理完成
echo.
echo ==================== 部署结果汇总 ====================
echo 总计部署服务器数:%total_count%
echo 成功部署数:%success_count%
if %success_count% equ %total_count% (
echo [成功] 所有服务器部署完成!
echo.
echo [访问提示] Web-AMR项目部署路径/var/www/web-map
echo 请确认nginx或apache配置指向正确的路径
) else (
echo [警告] 部分服务器部署失败,请检查错误信息
)
echo.
echo 脚本执行完成!
pause
exit /b 0
:: 部署到指定服务器的函数
:deploy_to_server
setlocal enabledelayedexpansion
set server_num=%1
if "%server_num%"=="1" (
set current_host=!server1_host!
set current_path=!server1_path!
set current_user=!server1_user!
set current_pass=!server1_pass!
set current_name=!server1_name!
set current_use_key=!server1_use_key!
) else if "%server_num%"=="2" (
set current_host=!server2_host!
set current_path=!server2_path!
set current_user=!server2_user!
set current_pass=!server2_pass!
set current_name=!server2_name!
set current_use_key=!server2_use_key!
) else if "%server_num%"=="3" (
set current_host=!server3_host!
set current_path=!server3_path!
set current_user=!server3_user!
set current_pass=!server3_pass!
set current_name=!server3_name!
set current_use_key=!server3_use_key!
) else (
echo [错误] 无效的服务器编号:%server_num%
exit /b 1
)
echo.
echo [部署] 开始部署到 !current_name! (!current_host!)...
echo 目标路径:!current_path!
echo 用户名:!current_user!
:: 根据认证方式设置命令前缀
if "!current_use_key!"=="true" (
set scp_cmd=scp -o StrictHostKeyChecking=no
set ssh_cmd=ssh -o StrictHostKeyChecking=no
echo [信息] 使用SSH密钥认证免密登录
) else (
:: 检查是否有sshpass工具
where sshpass >nul 2>&1
if !errorlevel! equ 0 (
set scp_cmd=sshpass -p "!current_pass!" scp -o StrictHostKeyChecking=no
set ssh_cmd=sshpass -p "!current_pass!" ssh -o StrictHostKeyChecking=no
echo [信息] 使用sshpass自动输入密码
) else (
set scp_cmd=scp -o StrictHostKeyChecking=no
set ssh_cmd=ssh -o StrictHostKeyChecking=no
echo [提示] 如果提示输入密码,请输入:!current_pass!
echo [建议] 安装sshpass工具可以实现自动输入密码
)
)
:: 上传文件到远程服务器
echo [信息] 正在上传文件...
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
if !errorlevel! neq 0 (
echo [错误] 文件上传到 !current_name! 失败
exit /b 1
)
echo [成功] 文件上传到 !current_name! 完成
:: 在远程服务器上解压并部署
echo [信息] 正在远程服务器上解压部署...
!ssh_cmd! !current_user!@!current_host! "mkdir -p !current_path! && rm -r !current_path!/* 2>/dev/null || true && cd !current_path! && unzip -o /tmp/dist_temp.zip && rm /tmp/dist_temp.zip && echo '部署完成,文件列表:' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'"
if !errorlevel! neq 0 (
echo [错误] !current_name! 远程部署失败
exit /b 1
)
echo [成功] !current_name! 部署完成!
exit /b 0