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

This commit is contained in:
bot
2026-11-05 02:54:23 +00:00
commit bae9657059
11 changed files with 218 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
.git
.gitea
test
*.md
go.mod
icon.*
+24
View File
@@ -0,0 +1,24 @@
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
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: 腳本黑箱測試
run: go vet ./... && go test ./test/
- name: 建置映像檔
run: docker build -t "action-test:$GITHUB_SHA" .
+8
View File
@@ -0,0 +1,8 @@
FROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
# 只需要 bash、git(認證沿用 checkout 寫在 workspace 的設定)
RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends ca-certificates git \
&& rm -rf /var/lib/apt/lists/*
# 腳本打包進映像檔(action 在獨立的 repo,呼叫端的 workspace 裡沒有這些檔案);發佈新版時要換標籤,runner 才會重建映像檔
COPY . /action
ENTRYPOINT ["/bin/bash", "/action/entrypoint.sh"]
+28
View File
@@ -0,0 +1,28 @@
# sync-branch
目標分支不存在或已包含該提交就什麼都不做;有衝突就失敗。
## 用法
版本由呼叫端的 Gitea Actions 變數決定(例如 `vars.ACTION_SYNC_BRANCH_VERSION`),不要用分支名稱:runner 依 ref 重用舊映像檔,改了腳本不會生效,所以每次改動都發新標籤。
```yaml
- uses: https://gitea.jsc.idv.tw/actions/sync-branch@${{ vars.ACTION_SYNC_BRANCH_VERSION }}
with:
source: ...
target: ...
```
## 輸入
|名稱|必填|預設|說明|
|---|---|---|---|
|`source`|是||來源分支名稱(例如 main)|
|`target`|是||目標分支名稱(例如 dev)|
|`sha`||`${{ github.sha }}`|要合併進去的來源提交,預設是目前的 github.sha|
## 開發
`ci.yaml` 跑 shellcheck、`test/` 的腳本黑箱測試(`go test ./test/`)、建置映像檔。
圖示:`icon.svg`(Gitea 儲存庫頭像用 `icon.png`)。
+15
View File
@@ -0,0 +1,15 @@
name: sync-branch
description: "把來源分支的提交合併進目標分支並推送(Docker action,bash+git)。目標分支不存在、或已包含該提交就什麼都不做;有衝突就失敗(不強推、不自動解衝突)。要在 checkout(fetch-depth: 0,且 token 能推送目標分支)之後使用"
inputs:
source:
description: "來源分支名稱(例如 main)"
required: true
target:
description: "目標分支名稱(例如 dev)"
required: true
sha:
description: "要合併進去的來源提交,預設是目前的 github.sha"
default: ${{ github.sha }}
runs:
using: docker
image: Dockerfile
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# 把輸入轉成 sync.sh 讀的環境變數;工作目錄是掛進來的 workspace(checkout 已設好可推送的認證)
set -euo pipefail
cd "${GITHUB_WORKSPACE:-$PWD}"
git config --global --add safe.directory "$PWD"
export SYNC_SOURCE=${INPUT_SOURCE:?缺少 source} SYNC_TARGET=${INPUT_TARGET:?缺少 target} SYNC_SHA=${INPUT_SHA:?缺少 sha}
exec bash "$(dirname "${BASH_SOURCE[0]}")/sync.sh"
+3
View File
@@ -0,0 +1,3 @@
module gitea.jsc.idv.tw/actions/sync-branch
go 1.27.1
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 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="#4f46e5"/><circle fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" cx="160" cy="124" r="36"/><circle fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" cx="160" cy="388" r="36"/><circle fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" cx="352" cy="124" r="36"/><path fill="none" stroke="#fff" stroke-width="28" stroke-linecap="round" stroke-linejoin="round" d="M160 160v192M352 160c0 100-192 30-192 130"/></svg>

After

Width:  |  Height:  |  Size: 668 B

Executable
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# 把 SYNC_SHA(來源分支 SYNC_SOURCE 的提交)合併進 SYNC_TARGET 並推送;工作目錄是 fetch-depth: 0 的 checkout,origin 可推送
set -euo pipefail
src=${SYNC_SOURCE:?缺少 SYNC_SOURCE}; dst=${SYNC_TARGET:?缺少 SYNC_TARGET}; sha=${SYNC_SHA:?缺少 SYNC_SHA}
if ! git ls-remote --exit-code --heads origin "$dst" >/dev/null; then
echo "目標分支 $dst 不存在,不同步"
exit 0
fi
git fetch --quiet origin "$dst"
if git merge-base --is-ancestor "$sha" "origin/$dst"; then
echo "$dst 已經包含 $src($sha),不需要同步"
exit 0
fi
git config user.name "ci-sync-branch"
git config user.email "ci-sync-branch@users.noreply.invalid"
git checkout -q -B "$dst" "origin/$dst"
if ! git merge --no-edit -m "合併 $src 進 $dst(自動同步 ${sha::7})" "$sha"; then
git merge --abort || true
echo "::error::合併 $src 進 $dst 有衝突,請手動處理"
exit 1
fi
git push origin "HEAD:refs/heads/$dst"
echo "已把 $src($sha)合併進 $dst"
+103
View File
@@ -0,0 +1,103 @@
// .gitea/actions/sync-branch/sync.sh 的黑箱測試:用暫時的 bare 儲存庫當 origin,檢查合併、重跑、目標分支不存在與衝突。
package test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func gitIn(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v:%v\n%s", args, err, out)
}
return strings.TrimSpace(string(out))
}
func commitFile(t *testing.T, dir, name, content string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
gitIn(t, dir, "add", ".")
gitIn(t, dir, "commit", "-qm", "c "+name)
gitIn(t, dir, "push", "-q", "origin", "HEAD")
}
// syncSetup 建出 origin 與工作目錄:main 有一個提交;withDev 時 dev 從它分出去並多一個提交。
func syncSetup(t *testing.T, withDev bool) (origin, work string) {
t.Helper()
root := t.TempDir()
origin, work = filepath.Join(root, "origin.git"), filepath.Join(root, "work")
gitIn(t, root, "init", "-q", "--bare", origin)
gitIn(t, root, "clone", "-q", origin, work)
gitIn(t, work, "checkout", "-q", "-b", "main")
commitFile(t, work, "a.txt", "1")
if withDev {
gitIn(t, work, "checkout", "-q", "-b", "dev")
commitFile(t, work, "dev.txt", "d")
gitIn(t, work, "checkout", "-q", "main")
}
return origin, work
}
func runSync(t *testing.T, work string) (string, error) {
t.Helper()
script, _ := filepath.Abs("../sync.sh")
cmd := exec.Command("bash", script)
cmd.Dir = work
cmd.Env = append(os.Environ(), "SYNC_SOURCE=main", "SYNC_TARGET=dev", "SYNC_SHA="+gitIn(t, work, "rev-parse", "HEAD"),
"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestSyncBranchMergesSourceIntoTargetOnce(t *testing.T) {
origin, work := syncSetup(t, true)
commitFile(t, work, "b.txt", "2") // main 往前一個提交
out, err := runSync(t, work)
if err != nil {
t.Fatalf("同步失敗:%v\n%s", err, out)
}
if got := gitIn(t, origin, "ls-tree", "--name-only", "dev"); !strings.Contains(got, "b.txt") || !strings.Contains(got, "dev.txt") {
t.Fatalf("dev 應該同時有 main 的 b.txt 與自己的 dev.txt:\n%s", got)
}
head := gitIn(t, origin, "rev-parse", "dev")
if out, err := runSync(t, work); err != nil || !strings.Contains(out, "不需要同步") {
t.Fatalf("第二次應該不需要同步:%v\n%s", err, out)
}
if gitIn(t, origin, "rev-parse", "dev") != head {
t.Fatal("已經包含時不該再多一個提交")
}
}
func TestSyncBranchDoesNothingWithoutTarget(t *testing.T) {
_, work := syncSetup(t, false)
out, err := runSync(t, work)
if err != nil || !strings.Contains(out, "不存在") {
t.Fatalf("目標分支不存在應該成功並說明:%v\n%s", err, out)
}
}
func TestSyncBranchFailsOnConflictWithoutPushing(t *testing.T) {
origin, work := syncSetup(t, true)
gitIn(t, work, "checkout", "-q", "dev")
commitFile(t, work, "a.txt", "dev 版")
gitIn(t, work, "checkout", "-q", "main")
commitFile(t, work, "a.txt", "main 版")
before := gitIn(t, origin, "rev-parse", "dev")
out, err := runSync(t, work)
if err == nil || !strings.Contains(out, "衝突") {
t.Fatalf("有衝突應該失敗並說明:%v\n%s", err, out)
}
if gitIn(t, origin, "rev-parse", "dev") != before {
t.Fatal("衝突時不該推送")
}
}