Files
Genarrative/scripts/deploy/production-web-deploy.sh

124 lines
3.2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
用法:
./scripts/deploy/production-web-deploy.sh --source-dir build/<version> [--version <version>] [--release-root /opt/genarrative/releases] [--web-link /srv/genarrative/web] [--current-link /opt/genarrative/current]
说明:
校验 web.tar.gz.sha256 后,把 web.tar.gz 解压到 /opt/genarrative/releases/<version>/web
并更新 /srv/genarrative/web 软链接。如果同版本目录已存在 api-server则同步更新 /opt/genarrative/current。
EOF
}
require_argument() {
local value="$1"
local label="$2"
if [[ -z "${value}" ]]; then
echo "[production-web-deploy] 缺少参数: ${label}" >&2
exit 1
fi
}
SOURCE_DIR=""
VERSION=""
RELEASE_ROOT="/opt/genarrative/releases"
CURRENT_LINK="/opt/genarrative/current"
WEB_LINK="/srv/genarrative/web"
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--source-dir)
SOURCE_DIR="${2:?缺少 --source-dir 的值}"
shift 2
;;
--version)
VERSION="${2:?缺少 --version 的值}"
shift 2
;;
--release-root)
RELEASE_ROOT="${2:?缺少 --release-root 的值}"
shift 2
;;
--current-link)
CURRENT_LINK="${2:?缺少 --current-link 的值}"
shift 2
;;
--web-link)
WEB_LINK="${2:?缺少 --web-link 的值}"
shift 2
;;
*)
echo "[production-web-deploy] 未知参数: $1" >&2
usage >&2
exit 1
;;
esac
done
require_argument "${SOURCE_DIR}" "--source-dir"
if [[ ! -d "${SOURCE_DIR}" ]]; then
echo "[production-web-deploy] 发布目录不存在: ${SOURCE_DIR}" >&2
exit 1
fi
SOURCE_DIR="$(cd "${SOURCE_DIR}" && pwd)"
VERSION="${VERSION:-$(basename "${SOURCE_DIR}")}"
if [[ ! "${VERSION}" =~ ^[0-9A-Za-z._-]+$ ]]; then
echo "[production-web-deploy] --version 只能包含数字、字母、点、下划线和短横线: ${VERSION}" >&2
exit 1
fi
if [[ ! -f "${SOURCE_DIR}/web.tar.gz" || ! -f "${SOURCE_DIR}/web.tar.gz.sha256" ]]; then
echo "[production-web-deploy] 缺少 web.tar.gz 或 web.tar.gz.sha256: ${SOURCE_DIR}" >&2
exit 1
fi
echo "[production-web-deploy] 校验 Web 压缩包"
(
cd "${SOURCE_DIR}"
sha256sum -c web.tar.gz.sha256
)
RELEASE_DIR="${RELEASE_ROOT}/${VERSION}"
WEB_TARGET="${RELEASE_DIR}/web"
mkdir -p "${RELEASE_DIR}"
rm -rf "${WEB_TARGET}"
echo "[production-web-deploy] 解压 Web 到: ${WEB_TARGET}"
tar -xzf "${SOURCE_DIR}/web.tar.gz" -C "${RELEASE_DIR}"
test -d "${WEB_TARGET}"
if [[ -f "${SOURCE_DIR}/release-manifest.json" ]]; then
cp "${SOURCE_DIR}/release-manifest.json" "${RELEASE_DIR}/release-manifest.web.json"
fi
mkdir -p "$(dirname "${CURRENT_LINK}")" "$(dirname "${WEB_LINK}")"
ln -sfn "${WEB_TARGET}" "${WEB_LINK}"
if [[ -x "${RELEASE_DIR}/api-server" ]]; then
ln -sfn "${RELEASE_DIR}" "${CURRENT_LINK}"
else
echo "[production-web-deploy] ${RELEASE_DIR}/api-server 不存在,仅更新 Web 软链接,保持 current 不变。"
fi
if command -v nginx >/dev/null 2>&1; then
echo "[production-web-deploy] nginx -t"
nginx -t
fi
if [[ ! -f "${WEB_TARGET}/index.html" ]]; then
echo "[production-web-deploy] Web smoke test 失败,缺少 index.html: ${WEB_TARGET}" >&2
exit 1
fi
echo "[production-web-deploy] 完成: ${WEB_TARGET}"