@@ -2,33 +2,55 @@ name: Use Visitor Counter Logic
22
33on :
44 pull_request :
5- branches :
6- - main
7- workflow_dispatch : # Allows manual triggering
5+ branches : [main]
6+ workflow_dispatch :
87 # schedule:
98 # - cron: '0 0 * * *' # Runs daily at midnight
10-
9+
1110permissions :
1211 contents : write
1312 pull-requests : write
1413
14+ # Prevent parallel runs from racing on the same branch
15+ concurrency :
16+ group : visitor-counter-${{ github.repository }}-${{ github.event.pull_request.head.ref || github.ref_name }}
17+ cancel-in-progress : true
18+
1519jobs :
1620 update-visitor-count :
1721 runs-on : ubuntu-latest
1822
1923 steps :
20- - name : Checkout current repository
24+ # Checkout the *actual branch* we intend to push to
25+ # (avoids being on refs/pull/<id>/merge and needing a later checkout)
26+ - name : Checkout target branch
2127 uses : actions/checkout@v4
2228 with :
2329 fetch-depth : 0
30+ clean : true
31+ persist-credentials : false
32+ ref : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }}
33+
34+ - name : Configure Git author
35+ run : |
36+ git config user.name "github-actions[bot]"
37+ git config user.email "github-actions[bot]@users.noreply.github.com"
38+
39+ # Optional but recommended: sync before generating changes (no checkout later)
40+ - name : Sync branch with origin
41+ env :
42+ TARGET_REF : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }}
43+ run : |
44+ git fetch origin "$TARGET_REF"
45+ git pull --no-rebase --no-edit origin "$TARGET_REF" || true
2446
2547 - name : Shallow clone visitor counter logic
2648 run : git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git
2749
2850 - name : Set up Node.js
2951 uses : actions/setup-node@v4
3052 with :
31- node-version : ' 20 '
53+ node-version : " 20 "
3254
3355 - name : Install dependencies for github-visitor-counter
3456 run : |
@@ -44,43 +66,20 @@ jobs:
4466 - name : Move generated metrics.json to root
4567 run : mv github-visitor-counter/metrics.json .
4668
47- - name : List files for debugging
48- run : |
49- ls -l
50- ls -l github-visitor-counter
51-
5269 - name : Clean up visitor counter logic
5370 run : rm -rf github-visitor-counter
5471
55- - name : Configure Git author
56- run : |
57- git config --global user.name "github-actions[bot]"
58- git config --global user.email "github-actions[bot]@users.noreply.github.com"
59-
60- # Commit and push logic for PR events (merge, not rebase)
61- - name : Commit and push changes (PR)
62- if : github.event_name == 'pull_request'
72+ - name : Commit and push (only if changed)
6373 env :
6474 TOKEN : ${{ secrets.GITHUB_TOKEN }}
75+ TARGET_REF : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }}
6576 run : |
66- git fetch origin
67- git checkout ${{ github.head_ref }}
68- git pull origin ${{ github.head_ref }} || echo "No merge needed"
69- git add -A
70- git commit -m "Update visitor count" || echo "No changes to commit"
71- git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
72- git push origin HEAD:${{ github.head_ref }}
77+ if [ -z "$(git status --porcelain)" ]; then
78+ echo "No changes to commit"
79+ exit 0
80+ fi
7381
74- # Commit and push logic for non-PR events (merge, not rebase)
75- - name : Commit and push changes (non-PR)
76- if : github.event_name != 'pull_request'
77- env :
78- TOKEN : ${{ secrets.GITHUB_TOKEN }}
79- run : |
80- git fetch origin
81- git checkout ${{ github.ref_name }} || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
82- git pull origin ${{ github.ref_name }} || echo "No merge needed"
8382 git add -A
84- git commit -m "Update visitor count" || echo "No changes to commit"
85- git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
86- git push origin HEAD:${{ github.ref_name }}
83+ git commit -m "Update visitor count"
84+
85+ git push "https://x-access-token:${TOKEN}@github.com/${{ github.repository }}" "HEAD:${TARGET_REF}"
0 commit comments