@echo off
|
chcp 65001 >nul
|
setlocal enabledelayedexpansion
|
|
:: ================= Config =================
|
set "USER=dell"
|
set "PASS=xd123456@.."
|
set "IP=1.15.17.182"
|
set "PORT=10000"
|
set "DIST_DIR=dist"
|
set "REMOTE_DIR=/home/vue/newpro-product"
|
set "SSH_OPTS=-o StrictHostKeyChecking=no -o ConnectTimeout=10"
|
:: ===========================================
|
|
echo ==========================================
|
echo Vue Dist Auto Deploy Script
|
echo ==========================================
|
echo.
|
|
:: Check if sshpass is available
|
where sshpass >nul 2>&1
|
if %errorlevel% neq 0 (
|
echo [WARN] sshpass not found, trying to install...
|
echo.
|
|
where bash >nul 2>&1
|
if %errorlevel% equ 0 (
|
echo [INFO] Git Bash detected, trying pacman install...
|
bash -c "pacman -S --noconfirm sshpass" 2>nul
|
if !errorlevel! equ 0 (
|
echo [OK] sshpass installed via pacman!
|
goto :check_dist
|
)
|
)
|
|
set "SSHPASS_DIR=%~dp0bin"
|
if not exist "!SSHPASS_DIR!" mkdir "!SSHPASS_DIR!"
|
|
echo [INFO] Downloading sshpass...
|
powershell -NoProfile -Command "$url='https://github.com/xhcoding/sshpass-win32/releases/download/v1.0.3/sshpass.exe'; $out='!SSHPASS_DIR!\sshpass.exe'; Write-Host \"Downloading to: $out\"; try { Invoke-WebRequest -Uri $url -OutFile $out -UseBasicParsing; exit 0 } catch { Write-Host $_.Exception.Message; exit 1 }"
|
if !errorlevel! neq 0 (
|
echo [ERROR] Failed to download sshpass!
|
echo [HINT] Please install sshpass manually or use Git Bash: pacman -S sshpass
|
goto :error_exit
|
)
|
|
set "PATH=!SSHPASS_DIR!;%PATH%"
|
echo [OK] sshpass downloaded to: !SSHPASS_DIR!
|
echo.
|
)
|
|
:check_dist
|
if not exist "%DIST_DIR%" (
|
echo [ERROR] Dist directory not found: %DIST_DIR%
|
echo [HINT] Please run 'npm run build' first!
|
goto :error_exit
|
)
|
|
:: Count files and calculate size
|
set "FILE_COUNT=0"
|
set "TOTAL_SIZE=0"
|
for /r "%DIST_DIR%" %%F in (*) do (
|
set /a "FILE_COUNT+=1"
|
set /a "TOTAL_SIZE+=%%~zF"
|
)
|
set /a "TOTAL_SIZE_MB=TOTAL_SIZE/1024/1024"
|
|
echo [INFO] Dist directory: %DIST_DIR%
|
echo [INFO] Files count: %FILE_COUNT%
|
echo [INFO] Total size: %TOTAL_SIZE_MB% MB
|
echo [INFO] Target: %USER%@%IP%:%PORT%
|
echo [INFO] Remote dir: %REMOTE_DIR%
|
echo.
|
|
:: Compress dist to zip
|
set "ZIP_FILE=dist.zip"
|
echo [STEP 1/3] Compressing dist to %ZIP_FILE%...
|
if exist "%ZIP_FILE%" del "%ZIP_FILE%"
|
powershell -NoProfile -Command "Compress-Archive -Path '%DIST_DIR%\*' -DestinationPath '%ZIP_FILE%' -CompressionLevel Optimal"
|
if %errorlevel% neq 0 (
|
echo [ERROR] Compression failed!
|
goto :error_exit
|
)
|
for %%F in ("%ZIP_FILE%") do set "ZIP_SIZE_MB=%%~zF"
|
set /a "ZIP_SIZE_MB=ZIP_SIZE_MB/1024/1024"
|
echo [OK] Compressed: %ZIP_SIZE_MB% MB
|
echo.
|
|
:: Upload zip file
|
echo [STEP 2/3] Uploading %ZIP_FILE%...
|
sshpass -p "%PASS%" scp -P %PORT% %SSH_OPTS% "%ZIP_FILE%" %USER%@%IP%:%REMOTE_DIR%/
|
if %errorlevel% neq 0 (
|
echo [ERROR] Upload failed! Check network or credentials.
|
goto :error_exit
|
)
|
echo [OK] Upload completed!
|
echo.
|
|
:: Extract on remote server
|
echo [STEP 3/3] Extracting on remote server...
|
sshpass -p "%PASS%" ssh -p %PORT% %SSH_OPTS% %USER%@%IP% "cd '%REMOTE_DIR%' && rm -rf dist && unzip -o '%ZIP_FILE%' -d dist && rm -f '%ZIP_FILE%'"
|
if %errorlevel% neq 0 (
|
echo [ERROR] Extraction failed!
|
goto :error_exit
|
)
|
echo [OK] Extraction completed!
|
|
:: Cleanup local zip
|
del "%ZIP_FILE%"
|
|
echo.
|
echo ==========================================
|
echo [SUCCESS] Deploy completed!
|
echo ==========================================
|
goto :end
|
|
:error_exit
|
echo.
|
echo ==========================================
|
echo [FAILED] Deploy failed!
|
echo ==========================================
|
|
:end
|
pause
|