@echo off 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 ) :: 检查是否有sshpass或可替代工具 where sshpass >nul 2>&1 if %errorlevel% neq 0 ( where expect >nul 2>&1 if %errorlevel% neq 0 ( echo. echo [提示] 为实现密码自动输入,建议安装以下工具之一: echo 1. sshpass - 用于SSH密码自动输入 echo 安装方法:在Git Bash中执行 pacman -S sshpass echo 2. expect - 用于自动化交互脚本 echo 可通过Cygwin或MSYS2安装 echo. echo [当前方案] 脚本会将密码复制到剪贴板,提示输入密码时按Ctrl+V或右键粘贴 echo. pause ) ) :: 创建临时压缩包 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! :: 检查是否有sshpass工具 where sshpass >nul 2>&1 if !errorlevel! equ 0 ( :: 有sshpass工具,使用自动密码输入 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 ( :: 没有sshpass工具,检查是否为Windows环境 if exist "%windir%\System32\clip.exe" ( :: Windows环境,将密码复制到剪贴板 echo !current_pass!| clip echo [信息] 密码已复制到剪贴板,可直接粘贴 echo [密码] !current_pass! echo [提示] 如果提示输入密码,可以按Ctrl+V或右键粘贴,或手动输入上述密码 ) else ( echo [提示] 如果提示输入密码,请输入:!current_pass! ) set scp_cmd=scp -o StrictHostKeyChecking=no set ssh_cmd=ssh -o StrictHostKeyChecking=no :: 尝试使用expect脚本(如果可用) where expect >nul 2>&1 if !errorlevel! equ 0 ( echo [信息] 检测到expect工具,尝试自动输入密码 :: 创建临时expect脚本 echo set timeout 30 > temp_expect.exp echo spawn scp -o StrictHostKeyChecking=no dist_temp.zip !current_user!@!current_host!:/tmp/ >> temp_expect.exp echo expect "password:" >> temp_expect.exp echo send "!current_pass!\r" >> temp_expect.exp echo expect eof >> temp_expect.exp set scp_cmd=expect temp_expect.exp set use_expect=true ) ) :: 上传文件到远程服务器 echo [信息] 正在上传文件... if defined use_expect ( :: 使用expect脚本 !scp_cmd! if exist temp_expect.exp del temp_expect.exp ) else ( :: 使用普通scp命令 !scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/ ) if !errorlevel! neq 0 ( echo [错误] 文件上传到 !current_name! 失败 echo [提示] 请检查网络连接和SSH配置 if defined current_pass ( echo [密码提示] 服务器密码: !current_pass! ) exit /b 1 ) echo [成功] 文件上传到 !current_name! 完成 :: 在远程服务器上解压并部署 echo [信息] 正在远程服务器上解压部署... if defined use_expect ( :: 为SSH创建expect脚本 echo set timeout 30 > temp_ssh_expect.exp echo spawn ssh -o StrictHostKeyChecking=no !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 'Web-AMR项目部署完成,文件列表:' && ls -la !current_path! \|\| echo '文件列表显示失败,但部署成功'" >> temp_ssh_expect.exp echo expect "password:" >> temp_ssh_expect.exp echo send "!current_pass!\r" >> temp_ssh_expect.exp echo expect eof >> temp_ssh_expect.exp expect temp_ssh_expect.exp if exist temp_ssh_expect.exp del temp_ssh_expect.exp ) else ( !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 'Web-AMR项目部署完成,文件列表:' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'" ) if !errorlevel! neq 0 ( echo [错误] !current_name! 远程部署失败 exit /b 1 ) echo [成功] !current_name! Web-AMR项目部署完成! exit /b 0