Skip to content

Commit 679bc1e

Browse files
Timna BrownTimna Brown
authored andcommitted
Harden visitor counter sync workflow
1 parent da48fb7 commit 679bc1e

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

.github/workflows/use-visitor-counter.yml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,34 @@ jobs:
6666
GIT_COMMITTER_NAME: github-actions[bot]
6767
GIT_COMMITTER_EMAIL: github-actions[bot]@users.noreply.github.com
6868
run: |
69+
set -euo pipefail
70+
6971
# Ensure we're on the correct branch
7072
git switch -c "$PR_BRANCH" || git switch "$PR_BRANCH"
71-
72-
# Stage and commit changes if any
73+
74+
# Rebase onto the latest remote branch state before creating a commit.
75+
git fetch origin "$PR_BRANCH"
76+
git rebase "origin/$PR_BRANCH"
77+
78+
# Stage and commit changes if any.
7379
git add -A
74-
git diff --staged --quiet || git commit -m "Update visitor count"
75-
76-
# Pull and merge existing changes
77-
git pull origin "$PR_BRANCH" --no-rebase
78-
79-
# Push all changes
80-
git push origin "$PR_BRANCH"
80+
if git diff --staged --quiet; then
81+
echo "No changes to commit"
82+
exit 0
83+
fi
84+
85+
git commit -m "Update visitor count"
86+
87+
# Retry the push after rebasing if another update lands first.
88+
for attempt in 1 2 3; do
89+
if git push origin HEAD:"$PR_BRANCH"; then
90+
exit 0
91+
fi
92+
93+
echo "Push rejected on attempt $attempt; fetching and rebasing"
94+
git fetch origin "$PR_BRANCH"
95+
git rebase "origin/$PR_BRANCH"
96+
done
97+
98+
echo "Failed to push visitor count updates after 3 attempts"
99+
exit 1

0 commit comments

Comments
 (0)