@@ -57,28 +57,32 @@ jobs:
5757 git config --global user.name "github-actions[bot]"
5858 git config --global user.email "github-actions[bot]@users.noreply.github.com"
5959
60- # Checkout to correct branch before commit
61- - name : Checkout to correct branch (PR or main)
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'
63+ env :
64+ TOKEN : ${{ secrets.GITHUB_TOKEN }}
6265 run : |
63- BRANCH="${{ github.head_ref || github.ref_name }}"
6466 git fetch origin
65- git checkout "$BRANCH" || git checkout -b "$BRANCH" "origin/$BRANCH"
66-
67- - name : Pre-commit local changes
68- run : |
69- # Only add files that exist
70- [ -f metrics.json ] && git add metrics.json || true
67+ git checkout ${{ github.head_ref }}
68+ git pull origin ${{ github.head_ref }} || echo "No merge needed"
7169 git add "*.md" || true
72- git commit -m "Automated update of visitor count" || echo "No changes to commit"
70+ [ -f metrics.json ] && git add metrics.json || true
71+ git commit -m "Update visitor count" || echo "No changes to commit"
72+ git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
73+ git push origin HEAD:${{ github.head_ref }}
7374
74- - name : Pull (merge) and push changes
75+ # Commit and push logic for non-PR events (merge, not rebase)
76+ - name : Commit and push changes (non-PR)
77+ if : github.event_name != 'pull_request'
7578 env :
7679 TOKEN : ${{ secrets.GITHUB_TOKEN }}
7780 run : |
78- BRANCH="${{ github.head_ref || github.ref_name }}"
79- git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
80- git pull origin "$BRANCH" || echo "No merge needed"
81- [ -f metrics.json ] && git add metrics.json || true
81+ git fetch origin
82+ git checkout ${{ github.ref_name }} || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
83+ git pull origin ${{ github.ref_name }} || echo "No merge needed"
8284 git add "*.md" || true
85+ [ -f metrics.json ] && git add metrics.json || true
8386 git commit -m "Update visitor count" || echo "No changes to commit"
84- git push origin HEAD:"$BRANCH"
87+ git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
88+ git push origin HEAD:${{ github.ref_name }}
0 commit comments