Files
build-go/toolchain.sh
T
2026-11-05 02:54:21 +00:00

31 lines
1.7 KiB
Bash
Raw 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
# 工具鏈與快取環境:被 source(不單獨執行),呼叫端已 set -euo pipefail 並 cd 到 workspace。
# 效果:HOME/GOPATH/GOCACHE 指到 workspace 的 .ci-home,GO_VERSION、NODE_VERSION 有設的就依版本下載到快取並加進 PATH(沒設的不裝)。
# 快取(Go 模組、npm、rod 下載的 Chromium 等)放在 workspace,讓 workflow 的 actions/cache 存取
export HOME="$PWD/.ci-home" GOPATH="$PWD/.ci-home/go" GOCACHE="$PWD/.ci-home/go-build" npm_config_cache="$PWD/.ci-home/npm"
mkdir -p "$HOME"
git config --global --add safe.directory "$PWD"
tc="$PWD/.ci-home/toolchains"
fetch_toolchain() { # <名稱> <網址> <解壓參數> <檢查的執行檔>;下載到暫存目錄再改名,中途失敗不留半套
local dir="$tc/$1" tmp
[ -x "$dir/bin/$4" ] && return 0
echo "下載 $1"
tmp=$(mktemp -d "$tc/.tmp.XXXXXX")
curl -fsSL "$2" | tar "$3" -C "$tmp" --strip-components=1
rm -rf "$dir" && mv "$tmp" "$dir"
}
mkdir -p "$tc"
# Go 與 Node 的版本通常來自 Gitea 的 Actions 變數(workflow 的 env 傳進來),依版本裝在快取裡,換版本只要改變數
if [ -n "${GO_VERSION:-}" ]; then
fetch_toolchain "go-$GO_VERSION" "https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz" -xz go
export PATH="$tc/go-$GO_VERSION/bin:$PATH"
if [ -f go.mod ]; then
want=$(sed -n 's/^go //p' go.mod)
[ "$(go env GOVERSION)" = "go$want" ] || { echo "GO_VERSION($GO_VERSION)和 go.mod($want)不同,請更新變數" >&2; exit 1; }
fi
fi
if [ -n "${NODE_VERSION:-}" ]; then
fetch_toolchain "node-$NODE_VERSION" "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" -xJ node
export PATH="$tc/node-$NODE_VERSION/bin:$PATH"
fi