name: Print One on: workflow_run: workflows: ["Prepare"] types: - completed concurrency: group: print1-${{ github.repository }} cancel-in-progress: false env: WORKSPACE_ROOT: "/data/workspace" jobs: print: runs-on: standard-ubuntu-22 if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: 将 Print One Worktree 对齐到上游提交 run: | set -euo pipefail 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]*) echo "无效的上游提交 SHA: $HEAD_SHA" >&2 exit 1 ;; esac 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 --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 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 秒打印一次数字 run: | 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 echo "[$(date '+%F %T')] Print One -> $i" sleep 10 done echo "[$(date '+%F %T')] Print One 完成"