test: share bare repository across print workflows
Test Multi Jobs / first (push) Successful in 1s
Test Multi Jobs / second (push) Successful in 1s
Prepare / prepare (push) Successful in 11s

This commit is contained in:
csh
2026-07-20 09:25:36 +08:00
parent d73e06301a
commit 94d2d5b042
4 changed files with 222 additions and 27 deletions
+76 -25
View File
@@ -6,46 +6,97 @@ on:
- main - main
workflow_dispatch: workflow_dispatch:
concurrency:
group: prepare-${{ github.repository }}
cancel-in-progress: false
env:
WORKSPACE_ROOT: "/data/workspace"
jobs: jobs:
prepare: prepare:
runs-on: standard-ubuntu-22 runs-on: standard-ubuntu-22
steps: steps:
- name: 准备复用仓库(强制全量拉取 + 丢弃本地改动) - name: 准备共享仓库和持久 Worktree
run: | run: |
set -e set -euo pipefail
REPO_DIR=/tmp/reused-repo
REPO_NAME="${{ github.event.repository.name }}"
REPO_URL="${{ github.server_url }}/${{ github.repository }}.git" REPO_URL="${{ github.server_url }}/${{ github.repository }}.git"
BRANCH="${{ github.ref_name }}" HEAD_SHA="${{ github.sha }}"
REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"
WORKTREE_ROOT="${WORKSPACE_ROOT}/${REPO_NAME}/worktrees"
echo "===== [$(date '+%F %T')] 开始准备仓库 =====" case "$HEAD_SHA" in
echo "复用目录: $REPO_DIR" ''|*[!0-9a-fA-F]*)
echo "无效的触发提交 SHA: $HEAD_SHA" >&2
exit 1
;;
esac
echo "===== [$(date '+%F %T')] 开始准备共享仓库 ====="
echo "共享仓库: $REPOSITORY_DIR"
echo "Worktree 根目录: $WORKTREE_ROOT"
echo "仓库地址: $REPO_URL" echo "仓库地址: $REPO_URL"
echo "目标分支: $BRANCH" echo "触发提交: $HEAD_SHA"
if [ ! -d "$REPO_DIR/.git" ]; then mkdir -p "$(dirname "$REPOSITORY_DIR")" "$WORKTREE_ROOT"
echo "首次使用,克隆仓库..."
git clone "$REPO_URL" "$REPO_DIR" if [ ! -d "$REPOSITORY_DIR" ]; then
echo "首次使用,初始化 bare 仓库..."
git init --bare "$REPOSITORY_DIR"
elif [ "$(git --git-dir="$REPOSITORY_DIR" rev-parse --is-bare-repository)" != "true" ]; then
echo "共享仓库目录不是 bare 仓库: $REPOSITORY_DIR" >&2
exit 1
fi fi
cd "$REPO_DIR" if git --git-dir="$REPOSITORY_DIR" remote get-url origin >/dev/null 2>&1; then
git --git-dir="$REPOSITORY_DIR" remote set-url origin "$REPO_URL"
else
git --git-dir="$REPOSITORY_DIR" remote add origin "$REPO_URL"
fi
echo "----- 丢弃本地所有更改 -----" echo "----- 从远端集中 fetch 一次 -----"
git reset --hard git --git-dir="$REPOSITORY_DIR" fetch \
git clean -fdx --prune --prune-tags --tags --force origin \
'+refs/heads/*:refs/remotes/origin/*'
echo "----- 强制拉取最新(含 tag,删除已消失的远端 ref-----" if ! git --git-dir="$REPOSITORY_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
git remote set-url origin "$REPO_URL" echo "共享仓库中不存在触发提交: $HEAD_SHA" >&2
git fetch origin --prune --prune-tags --tags --force exit 1
fi
echo "----- 强制对齐到远端分支 -----" ensure_worktree() {
git checkout -B "$BRANCH" "origin/$BRANCH" role=$1
git reset --hard "origin/$BRANCH" worktree_dir="${WORKTREE_ROOT}/${role}"
git clean -fdx
echo "----- 当前状态 -----" if [ -e "$worktree_dir" ]; then
git log -1 --oneline if [ ! -f "$worktree_dir/.git" ]; then
echo "本地 tags:" echo "Worktree 路径已存在但不是 linked worktree: $worktree_dir" >&2
git tag return 1
fi
common_dir=$(git -C "$worktree_dir" rev-parse --path-format=absolute --git-common-dir)
if [ "$common_dir" != "$(realpath "$REPOSITORY_DIR")" ]; then
echo "Worktree 不属于共享仓库: $worktree_dir" >&2
return 1
fi
echo "复用持久 Worktree: $worktree_dir"
return 0
fi
echo "首次创建持久 Worktree: $worktree_dir"
git --git-dir="$REPOSITORY_DIR" worktree add --detach "$worktree_dir" "$HEAD_SHA"
}
ensure_worktree "print1"
ensure_worktree "print2"
echo "----- 当前共享仓库状态 -----"
git --git-dir="$REPOSITORY_DIR" log -1 --oneline "$HEAD_SHA"
git --git-dir="$REPOSITORY_DIR" worktree list
echo "===== Prepare 完成 ====="
- name: 模拟准备耗时 - name: 模拟准备耗时
run: | run: |
+45 -1
View File
@@ -6,14 +6,58 @@ on:
types: types:
- completed - completed
concurrency:
group: print1-${{ github.repository }}
cancel-in-progress: false
env:
WORKSPACE_ROOT: "/data/workspace"
jobs: jobs:
print: print:
runs-on: standard-ubuntu-22 runs-on: standard-ubuntu-22
if: ${{ github.event.workflow_run.conclusion == 'success' }} if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps: steps:
- name: 将 Print One Worktree 对齐到上游提交
run: |
set -euo pipefail
REPO_NAME="${{ github.event.repository.name }}"
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
WORKTREE_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/worktrees/print1"
case "$HEAD_SHA" in
''|*[!0-9a-fA-F]*)
echo "无效的上游提交 SHA: $HEAD_SHA" >&2
exit 1
;;
esac
if [ ! -f "$WORKTREE_DIR/.git" ]; then
echo "Prepare 未创建 Print One Worktree: $WORKTREE_DIR" >&2
exit 1
fi
if ! git -C "$WORKTREE_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
echo "Worktree 共享仓库中不存在上游提交: $HEAD_SHA" >&2
exit 1
fi
git -C "$WORKTREE_DIR" checkout --detach --force "$HEAD_SHA"
git -C "$WORKTREE_DIR" reset --hard "$HEAD_SHA"
git -C "$WORKTREE_DIR" clean -ffdx
ACTUAL_SHA=$(git -C "$WORKTREE_DIR" rev-parse HEAD)
if [ "$ACTUAL_SHA" != "$HEAD_SHA" ]; then
echo "Worktree 提交不匹配: 期望 $HEAD_SHA,实际 $ACTUAL_SHA" >&2
exit 1
fi
echo "Print One 使用仓库: $WORKTREE_DIR"
git -C "$WORKTREE_DIR" log -1 --oneline
- name: 每 10 秒打印一次数字 - name: 每 10 秒打印一次数字
run: | run: |
echo "[$(date '+%F %T')] Print One 启动 (上游 Prepare 结论: ${{ github.event.workflow_run.conclusion }})" WORKTREE_DIR="${WORKSPACE_ROOT}/${{ github.event.repository.name }}/worktrees/print1"
echo "[$(date '+%F %T')] Print One 启动 (仓库提交: $(git -C "$WORKTREE_DIR" rev-parse --short HEAD))"
for i in $(seq 1 5); do for i in $(seq 1 5); do
echo "[$(date '+%F %T')] Print One -> $i" echo "[$(date '+%F %T')] Print One -> $i"
sleep 10 sleep 10
+45 -1
View File
@@ -6,14 +6,58 @@ on:
types: types:
- completed - completed
concurrency:
group: print2-${{ github.repository }}
cancel-in-progress: false
env:
WORKSPACE_ROOT: "/data/workspace"
jobs: jobs:
print: print:
runs-on: standard-ubuntu-22 runs-on: standard-ubuntu-22
if: ${{ github.event.workflow_run.conclusion == 'success' }} if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps: steps:
- name: 将 Print Two Worktree 对齐到上游提交
run: |
set -euo pipefail
REPO_NAME="${{ github.event.repository.name }}"
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
WORKTREE_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/worktrees/print2"
case "$HEAD_SHA" in
''|*[!0-9a-fA-F]*)
echo "无效的上游提交 SHA: $HEAD_SHA" >&2
exit 1
;;
esac
if [ ! -f "$WORKTREE_DIR/.git" ]; then
echo "Prepare 未创建 Print Two Worktree: $WORKTREE_DIR" >&2
exit 1
fi
if ! git -C "$WORKTREE_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
echo "Worktree 共享仓库中不存在上游提交: $HEAD_SHA" >&2
exit 1
fi
git -C "$WORKTREE_DIR" checkout --detach --force "$HEAD_SHA"
git -C "$WORKTREE_DIR" reset --hard "$HEAD_SHA"
git -C "$WORKTREE_DIR" clean -ffdx
ACTUAL_SHA=$(git -C "$WORKTREE_DIR" rev-parse HEAD)
if [ "$ACTUAL_SHA" != "$HEAD_SHA" ]; then
echo "Worktree 提交不匹配: 期望 $HEAD_SHA,实际 $ACTUAL_SHA" >&2
exit 1
fi
echo "Print Two 使用仓库: $WORKTREE_DIR"
git -C "$WORKTREE_DIR" log -1 --oneline
- name: 每 10 秒打印一次数字 - name: 每 10 秒打印一次数字
run: | run: |
echo "[$(date '+%F %T')] Print Two 启动 (上游 Prepare 结论: ${{ github.event.workflow_run.conclusion }})" WORKTREE_DIR="${WORKSPACE_ROOT}/${{ github.event.repository.name }}/worktrees/print2"
echo "[$(date '+%F %T')] Print Two 启动 (仓库提交: $(git -C "$WORKTREE_DIR" rev-parse --short HEAD))"
for i in $(seq 1 5); do for i in $(seq 1 5); do
echo "[$(date '+%F %T')] Print Two -> $i" echo "[$(date '+%F %T')] Print Two -> $i"
sleep 10 sleep 10
+56
View File
@@ -0,0 +1,56 @@
from pathlib import Path
import unittest
REPO_ROOT = Path(__file__).resolve().parents[1]
WORKFLOWS = REPO_ROOT / ".gitea" / "workflows"
class WorkflowPipelineTest(unittest.TestCase):
def read_workflow(self, name: str) -> str:
return (WORKFLOWS / name).read_text(encoding="utf-8")
def test_prepare_fetches_once_into_shared_bare_repository(self) -> None:
workflow = self.read_workflow("prepare.yml")
self.assertIn('group: prepare-${{ github.repository }}', workflow)
self.assertIn('cancel-in-progress: false', workflow)
self.assertIn('REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"', workflow)
self.assertIn('git init --bare "$REPOSITORY_DIR"', workflow)
self.assertIn('git --git-dir="$REPOSITORY_DIR" fetch', workflow)
self.assertNotIn("/tmp/reused-repo", workflow)
self.assertNotIn("git clone", workflow)
def test_prepare_creates_persistent_print_worktrees(self) -> None:
workflow = self.read_workflow("prepare.yml")
self.assertIn('ensure_worktree "print1"', workflow)
self.assertIn('ensure_worktree "print2"', workflow)
self.assertIn('worktree add --detach "$worktree_dir" "$HEAD_SHA"', workflow)
self.assertNotIn("worktree remove", workflow)
def assert_print_workflow(self, filename: str, role: str) -> None:
workflow = self.read_workflow(filename)
self.assertIn('workflows: ["Prepare"]', workflow)
self.assertIn("github.event.workflow_run.conclusion == 'success'", workflow)
self.assertIn(f'group: {role}-${{{{ github.repository }}}}', workflow)
self.assertIn('cancel-in-progress: false', workflow)
self.assertIn('HEAD_SHA="${{ github.event.workflow_run.head_sha }}"', workflow)
self.assertIn(f'WORKTREE_DIR="${{WORKSPACE_ROOT}}/${{REPO_NAME}}/worktrees/{role}"', workflow)
self.assertIn('git -C "$WORKTREE_DIR" checkout --detach --force "$HEAD_SHA"', workflow)
self.assertIn('git -C "$WORKTREE_DIR" reset --hard "$HEAD_SHA"', workflow)
self.assertIn('git -C "$WORKTREE_DIR" clean -ffdx', workflow)
self.assertIn('git -C "$WORKTREE_DIR" log -1 --oneline', workflow)
self.assertNotIn("git fetch", workflow)
self.assertNotIn("git pull", workflow)
def test_print_one_uses_its_persistent_worktree(self) -> None:
self.assert_print_workflow("print1.yml", "print1")
def test_print_two_uses_its_persistent_worktree(self) -> None:
self.assert_print_workflow("print2.yml", "print2")
if __name__ == "__main__":
unittest.main()