Files
deploy-go/check-env.sh
T
2026-11-05 03:22:15 +00:00

38 lines
1.5 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
# 部署步驟 1:檢查環境,工具與 Go 版本缺少或不足就安裝(curl、tar、systemctl、Go)。
# check-env.sh
# 需要 GO_VERSION;其他環境變數見 common.sh。
set -euo pipefail
. "$(dirname "$0")/common.sh"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
go_want=${GO_VERSION:?沒有指定 Go 版本:請在 Gitea 設定 Actions 變數 GO_VERSION(例如 1.27.1)}
go_want=${go_want#go}
go_dl=${GO_DL_URL:-https://go.dev/dl}
missing=()
for tool in curl tar; do command -v "$tool" >/dev/null || missing+=("$tool"); done
[ ${#missing[@]} -eq 0 ] || install_pkgs "${missing[@]}" ca-certificates
command -v systemctl >/dev/null || { echo "這台主機沒有 systemctl(systemd),無法部署服務" >&2; exit 1; }
go_have=$(go env GOVERSION 2>/dev/null | sed 's/^go//' || true)
if [ -z "$go_have" ] || older "$go_have" "$go_want"; then
log "Go 版本不足(目前:${go_have:-沒有},需要:$go_want),下載安裝到 $go_root/go"
case "$(uname -m)" in
x86_64) goarch=amd64 ;;
aarch64 | arm64) goarch=arm64 ;;
*) echo "不支援的架構:$(uname -m)" >&2; exit 1 ;;
esac
curl -fsSL -o "$work/go.tgz" "$go_dl/go$go_want.linux-$goarch.tar.gz"
gosudo=$sudo
[ ! -w "$go_root" ] || gosudo=
$gosudo rm -rf "$go_root/go"
$gosudo tar -C "$go_root" -xzf "$work/go.tgz"
go_have=$(go env GOVERSION | sed 's/^go//')
older "$go_have" "$go_want" && { echo "安裝後 Go 版本仍不足:$go_have" >&2; exit 1; }
fi
log "環境檢查通過:Go $go_have"