deploy.bat
在windows系统下面对项目进行打包和部署
下面是deploy.bat的示例:
bat
@echo off
chcp 65001 >nul
setlocal
:: ======================
:: 打包源目录(绝对路径或相对当前脚本路径)
set DIST_DIR=docs\.vitepress\dist
:: 服务器连接信息
set SERVER_USER=root
set SERVER_HOST=8.140.57.77
:: 服务器部署路径
set SERVER_PATH=/www/doc
:: ======================
echo 🔧 进入 doc 目录执行打包...
cd /d %~dp0
call npm run build
if %errorlevel% neq 0 (
echo ❌ Build failed. Exiting script.
pause
exit /b %errorlevel%
)
echo ✅ Build completed. Preparing to upload files to server...
:: 检查是否安装 scp(用于上传文件)
where scp >nul 2>nul
if errorlevel 1 (
echo ❌ Cannot find 'scp' command. Make sure Git or OpenSSH is installed.
pause
exit /b 1
)
:: 检查远程部署目录是否存在,如果不存在则创建
echo 📁 Checking if remote deploy directory exists: "%SERVER_PATH%"
ssh %SERVER_USER%@%SERVER_HOST% "if [ ! -d %SERVER_PATH% ]; then mkdir -p %SERVER_PATH%; fi"
if %errorlevel% neq 0 (
echo ❌ Failed to verify or create remote directory.
pause
exit /b %errorlevel%
)
:: 清空远程部署目录的旧文件
echo 🧹 Cleaning remote directory: "%SERVER_PATH%"
ssh %SERVER_USER%@%SERVER_HOST% "rm -rvf %SERVER_PATH%/*"
if %errorlevel% neq 0 (
echo ❌ Failed to clean remote directory.
pause
exit /b %errorlevel%
)
:: 上传 dist 目录内容到服务器
echo 🚀 Uploading "%DIST_DIR%\*" to "%SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%"
scp -r %DIST_DIR%\* %SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%
if %errorlevel% neq 0 (
echo ❌ Upload failed. Please check your SSH configuration.
pause
exit /b %errorlevel%
)
:: 设置部署目录权限,确保可被 Nginx 访问(目录 o+x,文件 o+r)
echo 🔧 Fixing remote file permissions in "%SERVER_PATH%" for Nginx access...
ssh %SERVER_USER%@%SERVER_HOST% "find %SERVER_PATH% -type d -exec chmod o+x {} \; && find %SERVER_PATH% -type f -exec chmod o+r {} \;"
if %errorlevel% neq 0 (
echo ❌ Failed to fix file permissions.
pause
exit /b %errorlevel%
)
echo 🎉 Deployment completed successfully!
pause
endlocal