Files
publish-go/go-module-upload.sh
2026-11-05 02:54:21 +00:00

26 lines
1.4 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
# 把 build-go 的 go-module-zip.sh 產生的模組 zip 上傳到 Gitea 的 Go 套件庫。由 publish-go 的 entrypoint.sh 呼叫。
# GITEA_TOKEN=<權杖,需要 write:package> go-module-upload.sh <zip 路徑>
# GITEA_URL(預設 GITHUB_SERVER_URL)、GITEA_OWNER(預設 GITHUB_REPOSITORY_OWNER)
set -euo pipefail
zip=${1:?用法:go-module-upload.sh <zip 路徑>}
: "${GITEA_TOKEN:?請設定 GITEA_TOKEN(需要 write:package 權限)}"
[ -f "$zip" ] || { echo "找不到 $zip" >&2; exit 1; }
base=${GITEA_URL:-${GITHUB_SERVER_URL:?請設定 GITEA_URL}}
owner=${GITEA_OWNER:-${GITHUB_REPOSITORY_OWNER:?請設定 GITEA_OWNER}}
# zip 裡的路徑是 <模組路徑>@<版本>/...,從第一個項目取出模組與標籤
modtag=$(python3 -I -c 'import sys, zipfile; n = zipfile.ZipFile(sys.argv[1]).namelist()[0]; print(n[:n.index("/", n.index("@"))])' "$zip" | tr -d '\r')
module=${modtag%@*}
tag=${modtag##*@}
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
code=$(curl -sS -o "$work/resp" -w '%{http_code}' -X PUT -H "Authorization: token $GITEA_TOKEN" \
--upload-file "$zip" "$base/api/packages/$owner/go/upload")
cat "$work/resp"; echo
case "$code" in
2*) echo "已發佈 $module@$tag:$base/$owner/-/packages/go/$(printf %s "$module" | sed 's#/#%2f#g')/$tag" ;;
409) echo "$module@$tag 已經存在,Go 套件庫不能覆寫同一個版本" >&2; exit 1 ;;
*) echo "上傳失敗(HTTP $code)" >&2; exit 1 ;;
esac