初始版本:從 feather-vault 搬出
ci / test (push) Successful in 37s

This commit is contained in:
bot
2026-11-05 02:54:21 +00:00
commit d5f1bcf7a6
10 changed files with 176 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
.git
.gitea
test
*.md
go.mod
icon.*
+19
View File
@@ -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" .
+8
View File
@@ -0,0 +1,8 @@
FROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
# 建置模組 zip:Go 與 Node 由 ci-env/toolchain.sh 依 GO_VERSION、NODE_VERSION 下載到快取(不放進映像檔);打包用 python3;不需要 Chromium 的系統依賴
RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends ca-certificates curl git make xz-utils python3 \
&& rm -rf /var/lib/apt/lists/*
# 腳本打包進映像檔(action 在獨立的 repo,呼叫端的 workspace 裡沒有這些檔案);發佈新版時要換標籤,runner 才會重建映像檔
COPY . /action
ENTRYPOINT ["/bin/bash", "/action/entrypoint.sh"]
+33
View File
@@ -0,0 +1,33 @@
# build-go
建立版本標籤(只在本機)、執行建置前置指令,把原始碼與建置結果打包成 Gitea Go 套件庫要的模組 zip。下一步接 `publish-go` 上傳。
## 用法
版本由呼叫端的 Gitea Actions 變數決定(例如 `vars.ACTION_BUILD_GO_VERSION`),不要用分支名稱:runner 依 ref 重用舊映像檔,改了腳本不會生效,所以每次改動都發新標籤。
```yaml
- uses: https://gitea.jsc.idv.tw/actions/build-go@${{ vars.ACTION_BUILD_GO_VERSION }}
with:
version: ...
```
## 輸入
|名稱|必填|預設|說明|
|---|---|---|---|
|`version`|是||要發佈的版本(例如 v0.5.4 或 v0.5.5-dev.2)|
|`prebuild`|||打包前執行的 shell 指令,用來產生不在 git 裡的建置結果(例如 make web worker-embed)|
|`include`|||要放進模組 zip 的建置結果路徑(相對於儲存庫根目錄,檔案或目錄,空白或換行分隔),少了任何一個就失敗|
## 輸出
|名稱|說明|
|---|---|
|`zip`|模組 zip 的路徑(相對於 workspace),傳給 publish-go|
## 開發
`ci.yaml` 跑 shellcheck、、建置映像檔。
圖示:`icon.svg`(Gitea 儲存庫頭像用 `icon.png`)。
+18
View File
@@ -0,0 +1,18 @@
name: build-go
description: "建置 Go 模組(Docker action):在本機建版本標籤(不推送)、執行建置前置指令、打包成 Go 模組 zip(放在 workspace 的 dist/go-module)。下一步用 publish-go 上傳。需要 fetch-depth: 0 的 checkout;要工具鏈時呼叫的工作流要有 env: GO_VERSION、NODE_VERSION"
inputs:
version:
description: 要發佈的版本(例如 v0.5.4 或 v0.5.5-dev.2)
required: true
prebuild:
description: "打包前執行的 shell 指令,用來產生不在 git 裡的建置結果(例如 make web worker-embed)"
default: ""
include:
description: "要放進模組 zip 的建置結果路徑(相對於儲存庫根目錄,檔案或目錄,空白或換行分隔),少了任何一個就失敗"
default: ""
outputs:
zip:
description: 模組 zip 的路徑(相對於 workspace),傳給 publish-go
runs:
using: docker
image: Dockerfile
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# 建置:本機建標籤(不推送)→ 建置前端與 worker → 打包成 Go 模組 zip,放在 workspace 的 dist/go-module(下一個步驟的容器才拿得到),路徑寫進輸出 zip
set -euo pipefail
cd "${GITHUB_WORKSPACE:-$PWD}"
# 工具鏈與快取環境(HOME、GOPATH、Go、Node);Go、Node 依 GO_VERSION、NODE_VERSION 下載到快取(沒設的不裝)
. "$(dirname "${BASH_SOURCE[0]}")/toolchain.sh"
version=${INPUT_VERSION:?缺少 version}
git fetch --tags --quiet origin || true
if git ls-remote --exit-code --tags origin "refs/tags/$version" >/dev/null 2>&1; then
echo "遠端已有標籤 $version,不重複發佈" >&2; exit 1
fi
git rev-parse -q --verify "refs/tags/$version" >/dev/null || git tag "$version" HEAD
if [ -n "${INPUT_PREBUILD:-}" ]; then
echo "== 建置前置:$INPUT_PREBUILD"
bash -euo pipefail -c "$INPUT_PREBUILD"
fi
rm -rf dist/go-module && mkdir -p dist/go-module
zip=$(ZIP_DIR="$PWD/dist/go-module" EMBED_PATHS="${INPUT_INCLUDE:-}" bash "$(dirname "${BASH_SOURCE[0]}")/go-module-zip.sh" "$version")
echo "zip=${zip#"$PWD"/}" >> "${GITHUB_OUTPUT:-/dev/null}"
echo "已建置 $zip"
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# 把標籤(或 commit)對應的原始碼打包成 Go 模組 zip,在標準輸出印出 zip 的路徑。由 build-go 的 entrypoint.sh 呼叫。
# go-module-zip.sh <標籤> 標籤要已存在於本機(可以還沒推送)
# go-module-zip.sh <commit 或分支> <版本> 不建標籤也能打包,<版本> 用來命名,例如 v0.0.1
# 模組 zip 要含不在 git 裡的建置結果(例如編譯好的前端、go:embed 的執行檔):EMBED_PATHS 是換行或空白分隔的路徑清單(相對於儲存庫根目錄,檔案或目錄),
# 打包時從工作目錄複製進去,少了任何一個就失敗。
# zip 放在新的暫存目錄(ZIP_DIR 可改),呼叫端用完自行刪除;進度與錯誤訊息都輸出到標準錯誤。
set -euo pipefail
ref=${1:?用法:go-module-zip.sh <標籤,例如 v0.0.1> | <commit> <版本>}
tag=${2:-$ref}
module=$(sed -n 's/^module //p' go.mod)
if [ $# -lt 2 ]; then
git rev-parse -q --verify "refs/tags/$tag" >/dev/null || { echo "找不到標籤 $tag" >&2; exit 1; }
else
git rev-parse -q --verify "$ref^{commit}" >/dev/null || { echo "找不到 $ref" >&2; exit 1; }
fi
# shellcheck disable=SC2206 # 依空白與換行拆成清單
embed=(${EMBED_PATHS:-})
for f in "${embed[@]}"; do [ -e "$f" ] || { echo "$f 不存在:先執行建置前置(prebuild)" >&2; exit 1; }; done
out=${ZIP_DIR:-$(mktemp -d)}
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
dir="$module@$tag"
mkdir -p "$work/$dir"
git archive "$ref" | tar -x -C "$work/$dir"
for f in "${embed[@]}"; do
rm -rf "${work:?}/$dir/$f" && mkdir -p "$work/$dir/$(dirname "$f")" && cp -r "$f" "$work/$dir/$f"
done
# 壓縮檔裡的路徑必須是完整的 <模組路徑>@<版本>/...(Gitea 用它決定套件名稱),所以不能用 python -m zipfile
python3 -I - "$work" "$dir" "$out/mod.zip" <<'PY'
import os, sys, zipfile
work, d, dest = sys.argv[1:4]
with zipfile.ZipFile(dest, "w", zipfile.ZIP_DEFLATED) as z:
for root, _, files in os.walk(os.path.join(work, d)):
for f in sorted(files):
full = os.path.join(root, f)
z.write(full, os.path.relpath(full, work))
PY
echo "$out/mod.zip"
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512"><rect width="512" height="512" rx="104" fill="#2563eb"/><path fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" d="M256 96 392 172V332L256 408 120 332V172Z"/><path fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" d="M120 172 256 252 392 172M256 252V408"/></svg>

After

Width:  |  Height:  |  Size: 431 B

+30
View File
@@ -0,0 +1,30 @@
#!/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