chore: pass web port through jenkins deploy
This commit is contained in:
@@ -5,7 +5,7 @@ set -euo pipefail
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
用法:
|
||||
./scripts/jenkins-deploy-release.sh --source-dir /path/to/build/123 --deploy-dir /var/lib/jenkins/deploy/Genarrative [--clear-database] [--hook-with-sudo]
|
||||
./scripts/jenkins-deploy-release.sh --source-dir /path/to/build/123 --deploy-dir /var/lib/jenkins/deploy/Genarrative --web-port 25001 [--clear-database] [--hook-with-sudo]
|
||||
|
||||
说明:
|
||||
1. 如果部署目录已有旧版本且存在 stop.sh,则先执行旧版本 stop.sh。
|
||||
@@ -17,6 +17,7 @@ usage() {
|
||||
参数:
|
||||
--source-dir <path> 必填,待部署的发布目录,例如 build/123
|
||||
--deploy-dir <path> 必填,固定部署目录,例如 /var/lib/jenkins/deploy/Genarrative
|
||||
--web-port <port> 必填,本次部署后静态网站监听端口
|
||||
--clear-database 可选,启动新版本时追加 --clear-database
|
||||
--hook-with-sudo 可选,仅对 start.sh/stop.sh 使用 sudo -n 执行
|
||||
EOF
|
||||
@@ -32,6 +33,28 @@ require_argument() {
|
||||
fi
|
||||
}
|
||||
|
||||
validate_port() {
|
||||
local value="$1"
|
||||
local label="$2"
|
||||
local numeric_value
|
||||
|
||||
if [[ ! "${value}" =~ ^[0-9]+$ ]]; then
|
||||
echo "[jenkins-deploy] ${label} 必须是数字端口: ${value}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ((${#value} > 5)); then
|
||||
echo "[jenkins-deploy] ${label} 必须在 1-65535 之间: ${value}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
numeric_value=$((10#${value}))
|
||||
if ((numeric_value < 1 || numeric_value > 65535)); then
|
||||
echo "[jenkins-deploy] ${label} 必须在 1-65535 之间: ${value}" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
normalize_env_file() {
|
||||
local env_file="$1"
|
||||
local temp_file="${env_file}.tmp.$$"
|
||||
@@ -54,8 +77,34 @@ normalize_release_env_files() {
|
||||
normalize_env_file "${release_dir}/web/.env.local"
|
||||
}
|
||||
|
||||
write_env_override() {
|
||||
local env_file="$1"
|
||||
local key="$2"
|
||||
local value="$3"
|
||||
local temp_file="${env_file}.tmp.$$"
|
||||
|
||||
mkdir -p "$(dirname "${env_file}")"
|
||||
if [[ -f "${env_file}" ]]; then
|
||||
# 先移除旧的同名变量,再追加 Jenkins 本次部署参数,确保 sudo 启动时也能被 start.sh 读取。
|
||||
awk -v target_key="${key}" '
|
||||
BEGIN {
|
||||
pattern = "^[[:space:]]*(export[[:space:]]+)?" target_key "="
|
||||
}
|
||||
$0 !~ pattern {
|
||||
print
|
||||
}
|
||||
' "${env_file}" >"${temp_file}"
|
||||
else
|
||||
: >"${temp_file}"
|
||||
fi
|
||||
|
||||
printf "%s=%s\n" "${key}" "${value}" >>"${temp_file}"
|
||||
mv "${temp_file}" "${env_file}"
|
||||
}
|
||||
|
||||
SOURCE_DIR=""
|
||||
DEPLOY_DIR=""
|
||||
WEB_PORT=""
|
||||
CLEAR_DATABASE="0"
|
||||
HOOK_WITH_SUDO="0"
|
||||
DEPLOY_ITEMS=(
|
||||
@@ -84,6 +133,10 @@ while [[ $# -gt 0 ]]; do
|
||||
DEPLOY_DIR="${2:?缺少 --deploy-dir 的值}"
|
||||
shift 2
|
||||
;;
|
||||
--web-port)
|
||||
WEB_PORT="${2:?缺少 --web-port 的值}"
|
||||
shift 2
|
||||
;;
|
||||
--clear-database)
|
||||
CLEAR_DATABASE="1"
|
||||
shift
|
||||
@@ -102,6 +155,8 @@ done
|
||||
|
||||
require_argument "${SOURCE_DIR}" "--source-dir"
|
||||
require_argument "${DEPLOY_DIR}" "--deploy-dir"
|
||||
require_argument "${WEB_PORT}" "--web-port"
|
||||
validate_port "${WEB_PORT}" "--web-port"
|
||||
|
||||
run_hook() {
|
||||
local hook_dir="$1"
|
||||
@@ -179,6 +234,7 @@ if [[ -f "${DEPLOY_DIR}/stop.sh" ]]; then
|
||||
fi
|
||||
|
||||
normalize_release_env_files "${DEPLOY_DIR}"
|
||||
write_env_override "${DEPLOY_DIR}/.env.local" "GENARRATIVE_WEB_PORT" "${WEB_PORT}"
|
||||
|
||||
echo "[jenkins-deploy] 启动新版本: ${DEPLOY_DIR}"
|
||||
if [[ "${CLEAR_DATABASE}" == "1" ]]; then
|
||||
|
||||
Reference in New Issue
Block a user