commit c2221c428176a4e94d792130d47efa71cbdde0d2 Author: bot Date: Thu Nov 5 02:54:21 2026 +0000 初始版本:從 feather-vault 搬出 diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1bd89cc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.git +.gitea +test +*.md +go.mod +icon.* diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..58e8169 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,19 @@ +name: ci +on: + push: + branches: [main] + pull_request: +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true +jobs: + test: + runs-on: docker-ubuntu + steps: + - uses: actions/checkout@v4 + - name: shellcheck + run: | + curl -fsSL https://github.com/koalaman/shellcheck/releases/download/v0.10.0/shellcheck-v0.10.0.linux.x86_64.tar.xz | tar -xJ -C "$RUNNER_TEMP" --strip-components=1 + "$RUNNER_TEMP/shellcheck" -x -S warning ./*.sh + - name: 建置映像檔 + run: docker build -t "action-test:$GITHUB_SHA" . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d12f226 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM ubuntu:24.04 +ENV DEBIAN_FRONTEND=noninteractive +# 上傳套件用 curl,讀 zip 的第一個項目用 python3,推標籤用 git;沒有 Go、Node,映像檔很小 +RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends ca-certificates curl git python3 \ + && rm -rf /var/lib/apt/lists/* +# 腳本打包進映像檔(action 在獨立的 repo,呼叫端的 workspace 裡沒有這些檔案);發佈新版時要換標籤,runner 才會重建映像檔 +COPY . /action +ENTRYPOINT ["/bin/bash", "/action/entrypoint.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..28858c4 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# publish-go + +上傳 `build-go` 打包的 zip 到 Gitea 的 Go 套件庫,成功才推送版本標籤;套件已存在(409)視為已上傳,接著推標籤,所以失敗後可以安全重跑。 + +## 用法 + +版本由呼叫端的 Gitea Actions 變數決定(例如 `vars.ACTION_PUBLISH_GO_VERSION`),不要用分支名稱:runner 依 ref 重用舊映像檔,改了腳本不會生效,所以每次改動都發新標籤。 + +```yaml +- uses: https://gitea.jsc.idv.tw/actions/publish-go@${{ vars.ACTION_PUBLISH_GO_VERSION }} + with: + version: ... + zip: ... + token: ... +``` + +## 輸入 + +|名稱|必填|預設|說明| +|---|---|---|---| +|`version`|是||要發佈的版本,要和 build-go 的相同| +|`zip`|是||build-go 輸出的模組 zip 路徑| +|`token`|是||推送標籤與上傳套件用的權杖(需要 write:repository、write:package)| + +## 開發 + +`ci.yaml` 跑 shellcheck、、建置映像檔。 + +圖示:`icon.svg`(Gitea 儲存庫頭像用 `icon.png`)。 diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..a4947bc --- /dev/null +++ b/action.yml @@ -0,0 +1,15 @@ +name: publish-go +description: "發佈(Docker action,bash+git+curl+python3):把 build-go 打包的模組 zip 上傳到 Gitea 的 Go 套件庫,成功才推送版本標籤。套件已存在(409)視為已上傳,接著推標籤" +inputs: + version: + description: 要發佈的版本,要和 build-go 的相同 + required: true + zip: + description: build-go 輸出的模組 zip 路徑 + required: true + token: + description: 推送標籤與上傳套件用的權杖(需要 write:repository、write:package) + required: true +runs: + using: docker + image: Dockerfile diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..9048768 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# 發佈:上傳 build-go 產生的 zip → 成功才推送標籤。失敗不留遠端標籤;重跑時套件已存在(409)也視為已上傳,接著推標籤 +set -euo pipefail +cd "${GITHUB_WORKSPACE:-$PWD}" +git config --global --add safe.directory "$PWD" +version=${INPUT_VERSION:?缺少 version}; token=${INPUT_TOKEN:?缺少 token}; zip=${INPUT_ZIP:?缺少 zip(build-go 的輸出)} +[ -f "$zip" ] || { echo "找不到 $zip:先執行 build-go" >&2; exit 1; } +git rev-parse -q --verify "refs/tags/$version" >/dev/null || { echo "本機沒有標籤 $version:先執行 build-go" >&2; exit 1; } +if ! out=$(GITEA_TOKEN="$token" GITEA_URL="$GITHUB_SERVER_URL" GITEA_OWNER="${GITHUB_REPOSITORY%%/*}" bash "$(dirname "${BASH_SOURCE[0]}")/go-module-upload.sh" "$zip" 2>&1); then + echo "$out" >&2 + case "$out" in *已經存在*) echo "套件 $version 已上傳過,繼續推送標籤" >&2 ;; *) exit 1 ;; esac +else + echo "$out" +fi +remote="${GITHUB_SERVER_URL%%://*}://oauth2:${token}@${GITHUB_SERVER_URL#*://}/${GITHUB_REPOSITORY}.git" +git push "$remote" "refs/tags/$version" +echo "已推送標籤 $version" diff --git a/go-module-upload.sh b/go-module-upload.sh new file mode 100755 index 0000000..b550a74 --- /dev/null +++ b/go-module-upload.sh @@ -0,0 +1,25 @@ +#!/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 +# GITEA_URL(預設 GITHUB_SERVER_URL)、GITEA_OWNER(預設 GITHUB_REPOSITORY_OWNER) +set -euo pipefail +zip=${1:?用法:go-module-upload.sh } +: "${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 diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..74c7209 Binary files /dev/null and b/icon.png differ diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..a2cb947 --- /dev/null +++ b/icon.svg @@ -0,0 +1 @@ + \ No newline at end of file