Files
yunzerwebsiteallinone/go/update-server.bat
T
2026-10-31 19:17:37 +08:00

129 lines
3.9 KiB
Batchfile

@echo off
chcp 65001 >nul
setlocal EnableExtensions
title Update Server - Go API incremental deploy
rem ==================================================================
rem Sync only CHANGED files from this go\ folder to the server.
rem (Batch file is ASCII-only on purpose: cmd.exe mis-parses UTF-8
rem Chinese text in .bat files. The deploy tool itself prints Chinese.)
rem
rem Usage:
rem update-server.bat diff -> confirm -> upload -> ask to restart
rem update-server.bat dry diff only, upload nothing
rem update-server.bat restart only restart the service on the server
rem ==================================================================
rem ============================ CONFIG ============================
set "DEPLOY_HOST=10.31.100.2"
set "DEPLOY_USER=root"
set "DEPLOY_DIR=/www/wwwroot/api.yunzer.cn"
rem Key auth (this server has password auth disabled in sshd)
set "DEPLOY_KEY=%~dp010.31.100.2_id_ed25519"
rem Fallback: if password auth is enabled later, blank DEPLOY_KEY and set the password here
rem set "DEPLOY_KEY="
set "DEPLOY_PASS=xxxxxx"
rem Restart command (systemd unit: go-api.service)
set "RESTART_CMD=systemctl restart go-api.service"
rem Extra exclude patterns, comma separated, * wildcard supported
set "EXTRA_EXCLUDE="
rem ================================================================
cd /d "%~dp0"
set "DEPLOY_EXE=%~dp0bin\deploy.exe"
set "ACTION=%~1"
if "%ACTION%"=="" set "ACTION=deploy"
rem %~dp0 ends with a backslash; a trailing backslash before the closing quote
rem escapes it and swallows the following arguments - strip it.
set "LOCAL_DIR=%~dp0"
set "LOCAL_DIR=%LOCAL_DIR:~0,-1%"
echo ==================================================================
echo Server : %DEPLOY_USER%@%DEPLOY_HOST%:%DEPLOY_DIR%
echo Local : %LOCAL_DIR%
echo Action : %ACTION%
echo ==================================================================
echo.
rem ---------------- 1/3 build the deploy tool ----------------
where go >nul 2>nul
if not errorlevel 1 (
echo [1/3] Building deploy tool...
go build -o "bin\deploy.exe" ./cmd/deploy
if errorlevel 1 (
echo [ERROR] Failed to build the deploy tool
goto :fail
)
) else (
if not exist "%DEPLOY_EXE%" (
echo [ERROR] go command not found and bin\deploy.exe is missing
goto :fail
)
echo [1/3] go not found, using existing bin\deploy.exe
)
rem ---------------- auth args ----------------
if defined DEPLOY_KEY (
if not exist "%DEPLOY_KEY%" (
echo [ERROR] Private key not found: %DEPLOY_KEY%
echo Blank DEPLOY_KEY in the config block to use password auth
goto :fail
)
set "AUTH_ARGS=-key "%DEPLOY_KEY%""
) else (
set "AUTH_ARGS=-pass %DEPLOY_PASS%"
)
rem ---------------- 2/3 run ----------------
if /i "%ACTION%"=="restart" goto :do_restart
set "EXTRA_ARGS="
if /i "%ACTION%"=="dry" set "EXTRA_ARGS=-dry-run"
echo [2/3] Comparing files...
echo.
"%DEPLOY_EXE%" -host "%DEPLOY_HOST%" -user "%DEPLOY_USER%" -dir "%DEPLOY_DIR%" -local "%LOCAL_DIR%" %AUTH_ARGS% %EXTRA_ARGS% %EXTRA_EXCLUDE%
if errorlevel 3 goto :cancelled
if errorlevel 1 goto :fail
if /i "%ACTION%"=="dry" goto :done
rem ---------------- 3/3 restart ----------------
echo.
set "ANSWER="
set /p "ANSWER=Restart the service so the changes take effect? (Y/N) [Y]: "
if not defined ANSWER set "ANSWER=Y"
if /i not "%ANSWER%"=="Y" (
echo Skipped. Run "update-server.bat restart" or restart it manually later.
goto :done
)
:do_restart
echo.
echo [3/3] Restarting: %RESTART_CMD%
"%DEPLOY_EXE%" -host "%DEPLOY_HOST%" -user "%DEPLOY_USER%" %AUTH_ARGS% -exec "%RESTART_CMD%"
if errorlevel 1 goto :fail
:done
echo.
echo [DONE]
if not defined DEPLOY_NOPAUSE pause
exit /b 0
:cancelled
echo.
echo [CANCELLED] Nothing was uploaded, service was NOT restarted.
if not defined DEPLOY_NOPAUSE pause
exit /b 0
:fail
echo.
echo [FAILED] See the message above
if not defined DEPLOY_NOPAUSE pause
exit /b 1