Files
bot d0cdfdc139
ci / test (push) Successful in 1m22s
初始版本:從 feather-vault 搬出
2026-11-05 02:54:22 +00:00

61 lines
3.1 KiB
Bash
Executable File
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
# shellcheck disable=SC2034 # 變數是給 source 它的步驟腳本用的
# 部署各步驟共用的設定與小工具(被 source,不單獨執行)。環境變數:
# DEPLOY_NAME 應用程式名稱(預設是儲存庫名稱);執行檔、服務、套件、資料目錄的預設值都由它組出
# DEPLOY_BIN 執行檔路徑(預設 /usr/local/bin/<名稱>)
# DEPLOY_SERVICE systemd 服務名稱(預設 <名稱>)
# DEPLOY_HEALTH_URL 健康檢查網址(預設 http://127.0.0.1:8080/api/health)
# DEPLOY_TIMEOUT 健康檢查最多等幾秒(預設 60)
# DEPLOY_PACKAGE go install 的套件(預設 <模組>/cmd/<名稱>)
# GITEA_URL Gitea 網址(預設 GITHUB_SERVER_URL;在 Actions 以外執行必填)
# GITEA_REPO Gitea 儲存庫 <擁有者>/<名稱>(預設 GITHUB_REPOSITORY;在 Actions 以外執行必填)。
# 模組路徑是「Gitea 主機/擁有者/名稱」,GOPROXY 是「Gitea 網址/api/packages/擁有者/go」
# DEPLOY_GOPROXY GOPROXY(預設由 GITEA_URL、GITEA_REPO 組出;不用主機自己的 GOPROXY)
# GO_VERSION 需要的 Go 版本,例如 1.27.1(check-env 必填)
# GO_DL_URL Go 下載網址(預設 https://go.dev/dl);GO_ROOT 安裝位置(預設 /usr/local)
# DEPLOY_ADDR 服務不存在、要安裝時的監聽位址(預設 0.0.0.0:8080)
# DEPLOY_DATA 服務不存在、要安裝時的資料目錄(預設 /var/lib/<名稱>)
# GITEA_TOKEN 套件庫需要驗證時的權杖(寫進臨時的 netrc,結束即刪除)
# 不是 root 時用 sudo -n。
# runner 的非互動 shell 不讀 ~/.profile,README 安裝 Go 的位置(/usr/local/go/bin)不在 PATH 裡
go_root=${GO_ROOT:-/usr/local}
PATH=$PATH:$go_root/go/bin:$HOME/go/bin:/usr/local/bin
export PATH
gitea_url=${GITEA_URL:-${GITHUB_SERVER_URL:?請設定 GITEA_URL(Gitea 網址)}}
gitea_repo=${GITEA_REPO:-${GITHUB_REPOSITORY:?請設定 GITEA_REPO(<擁有者>/<名稱>)}}
gitea_host=${gitea_url#*://}; gitea_host=${gitea_host%%/*}
module=$gitea_host/$gitea_repo
name=${DEPLOY_NAME:-${gitea_repo#*/}}
bin=${DEPLOY_BIN:-/usr/local/bin/$name}
staged="$bin.new" # install-binary 放新版的位置(和執行檔同目錄,activate 用改名原子替換)
prev="$bin.prev"
service=${DEPLOY_SERVICE:-$name}
health=${DEPLOY_HEALTH_URL:-http://127.0.0.1:8080/api/health}
timeout=${DEPLOY_TIMEOUT:-60}
package=${DEPLOY_PACKAGE:-$module/cmd/$name}
proxy=${DEPLOY_GOPROXY:-${gitea_url%/}/api/packages/${gitea_repo%%/*}/go}
sudo=
[ "$(id -u)" = 0 ] || sudo="sudo -n"
log() { echo "[deploy] $*"; }
# $1 比 $2 舊就回傳 0
older() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | head -n 1)" = "$1" ]; }
install_pkgs() {
log "安裝缺少的工具:$*"
if command -v apt-get >/dev/null; then
$sudo apt-get update -qq && $sudo apt-get install -y -qq "$@"
elif command -v dnf >/dev/null; then
$sudo dnf install -y -q "$@"
elif command -v yum >/dev/null; then
$sudo yum install -y -q "$@"
else
echo "找不到 apt-get、dnf、yum,請手動安裝:$*" >&2
return 1
fi
}