Skip to content

Commit e7dcfb4

Browse files
authored
adding condition pull before push
1 parent 516116d commit e7dcfb4

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,33 @@ 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+
# Commit and push logic for PR events
6061
- name: Commit and push changes (PR)
6162
if: github.event_name == 'pull_request'
6263
env:
6364
TOKEN: ${{ secrets.GITHUB_TOKEN }}
6465
run: |
6566
git fetch origin
66-
git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }}
67+
git checkout ${{ github.head_ref }}
6768
git add "*.md" metrics.json
6869
git commit -m "Update visitor count" || echo "No changes to commit"
6970
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
70-
git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed"
71-
git push origin HEAD:${{ github.event.pull_request.head.ref }}
71+
git pull --rebase origin ${{ github.head_ref }} || echo "No rebase needed"
72+
git push origin HEAD:${{ github.head_ref }}
7273
74+
# Commit and push logic for non-PR events (schedule, workflow_dispatch)
7375
- name: Commit and push changes (non-PR)
7476
if: github.event_name != 'pull_request'
7577
env:
7678
TOKEN: ${{ secrets.GITHUB_TOKEN }}
7779
run: |
7880
git fetch origin
79-
git checkout ${{ github.event.pull_request.head.ref }} || git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }}
81+
git checkout ${{ github.ref_name }} || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
8082
git add "*.md" metrics.json
8183
git commit -m "Update visitor count" || echo "No changes to commit"
8284
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
83-
git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed"
84-
git push origin HEAD:${{ github.event.pull_request.head.ref }}
85+
git pull --rebase origin ${{ github.ref_name }} || echo "No rebase needed"
86+
git push origin HEAD:${{ github.ref_name }}
8587
8688
- name: Create Pull Request (non-PR)
8789
if: github.event_name != 'pull_request'

0 commit comments

Comments
 (0)