test: let print workflows own their worktrees
Prepare / prepare (push) Successful in 11s
Test Multi Jobs / first (push) Successful in 0s
Test Multi Jobs / second (push) Successful in 0s

This commit is contained in:
csh
2026-07-20 09:37:00 +08:00
parent 94d2d5b042
commit 4d02a87806
4 changed files with 88 additions and 44 deletions
+37 -4
View File
@@ -24,7 +24,9 @@ jobs:
REPO_NAME="${{ github.event.repository.name }}"
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"
WORKTREE_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/worktrees/print1"
WORKTREE_LOCK="${WORKSPACE_ROOT}/${REPO_NAME}/worktree-admin.lock"
case "$HEAD_SHA" in
''|*[!0-9a-fA-F]*)
@@ -32,12 +34,43 @@ jobs:
exit 1
;;
esac
if [ ! -f "$WORKTREE_DIR/.git" ]; then
echo "Prepare 未创建 Print One Worktree: $WORKTREE_DIR" >&2
if [ ! -d "$REPOSITORY_DIR" ] || \
[ "$(git --git-dir="$REPOSITORY_DIR" rev-parse --is-bare-repository)" != "true" ]; then
echo "Prepare 未创建共享 bare 仓库: $REPOSITORY_DIR" >&2
exit 1
fi
if ! git -C "$WORKTREE_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
echo "Worktree 共享仓库中不存在上游提交: $HEAD_SHA" >&2
if ! git --git-dir="$REPOSITORY_DIR" cat-file -e "${HEAD_SHA}^{commit}"; then
echo "共享仓库中不存在上游提交: $HEAD_SHA" >&2
exit 1
fi
if [ ! -f "$WORKTREE_DIR/.git" ]; then
if ! command -v flock >/dev/null 2>&1; then
echo "首次创建 Worktree 需要 flock" >&2
exit 1
fi
exec 9>"$WORKTREE_LOCK"
flock 9
if [ ! -f "$WORKTREE_DIR/.git" ]; then
if [ -e "$WORKTREE_DIR" ]; then
echo "Worktree 路径已存在但不是 linked worktree: $WORKTREE_DIR" >&2
exit 1
fi
mkdir -p "$(dirname "$WORKTREE_DIR")"
echo "首次创建 Print One Worktree: $WORKTREE_DIR"
git --git-dir="$REPOSITORY_DIR" worktree add --detach "$WORKTREE_DIR" "$HEAD_SHA"
fi
flock -u 9
fi
COMMON_DIR=$(git -C "$WORKTREE_DIR" rev-parse --path-format=absolute --git-common-dir)
if [ "$COMMON_DIR" != "$(realpath "$REPOSITORY_DIR")" ]; then
echo "Print One Worktree 不属于共享仓库: $WORKTREE_DIR" >&2
exit 1
fi