Skip to content

Commit 2ee60f2

Browse files
authored
counter refence how to replicate
1 parent d8ab6f2 commit 2ee60f2

1 file changed

Lines changed: 75 additions & 17 deletions

File tree

README.md

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,104 @@ Last updated: 2025-07-10
3636
- In your repository, navigate to **Settings** > **Secrets and Variables** > **Actions**.
3737
- Add a new secret named `TRAFFIC_TOKEN` and paste the generated token.
3838
4. **Add the Pipeline**: This single pipeline will fetch the visitor count, update the badge in the `README.md` file, and push the changes back to the repository.
39-
- Create a GitHub Actions workflow (`update-metrics.yml`) in your repository to handle the visitor counter logic.
40-
- Use the following content for the workflow:
39+
- Create a GitHub Actions workflow (`use-visitor-counter.yml`) in your repository to handle the visitor counter logic.
40+
- Use the following content for the workflow, you can use [this pipeline as refence](https://github.com/brown9804/Cloud-DevOps-Overview/blob/main/.github/workflows/use-visitor-counter.yml).
4141

4242
```yaml
43-
name: Update Visitor Counter
43+
name: Use Visitor Counter Logic
4444

4545
on:
46+
pull_request:
47+
branches:
48+
- main
4649
schedule:
4750
- cron: '0 0 * * *' # Runs daily at midnight
48-
workflow_dispatch: # Allows manual triggering
51+
workflow_dispatch: # Allows manual triggering
52+
53+
permissions:
54+
contents: write
55+
pull-requests: write
4956

5057
jobs:
51-
update-counter:
58+
update-visitor-count:
5259
runs-on: ubuntu-latest
5360

5461
steps:
55-
- name: Checkout repository
56-
uses: actions/checkout@v3
62+
- name: Checkout current repository
63+
uses: actions/checkout@v4
64+
with:
65+
fetch-depth: 0
66+
67+
- name: Shallow clone visitor counter logic
68+
run: git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git
5769

5870
- name: Set up Node.js
59-
uses: actions/setup-node@v3
71+
uses: actions/setup-node@v4
6072
with:
61-
node-version: '16'
73+
node-version: '20'
6274

63-
- name: Install dependencies
64-
run: npm install @brown9804/github-visitor-counter
75+
- name: Install dependencies for github-visitor-counter
76+
run: |
77+
cd github-visitor-counter
78+
npm ci
6579
66-
- name: Run visitor counter script
67-
run: node node_modules/@brown9804/github-visitor-counter/update_repo_views_counter.js
80+
- name: Run visitor counter logic (updates markdown badges and metrics.json)
81+
run: node github-visitor-counter/update_repo_views_counter.js
6882
env:
6983
TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }}
7084
REPO: ${{ github.repository }}
7185

72-
- name: Commit and push changes
86+
- name: Move generated metrics.json to root
87+
run: mv github-visitor-counter/metrics.json .
88+
89+
- name: List files for debugging
90+
run: |
91+
ls -l
92+
ls -l github-visitor-counter
93+
94+
- name: Clean up visitor counter logic
95+
run: rm -rf github-visitor-counter
96+
97+
- name: Configure Git author
7398
run: |
7499
git config --global user.name "github-actions[bot]"
75100
git config --global user.email "github-actions[bot]@users.noreply.github.com"
76-
git add README.md metrics.json
77-
git commit -m "Update visitor count"
78-
git push
101+
102+
- name: Commit and push changes (PR)
103+
if: github.event_name == 'pull_request'
104+
env:
105+
TOKEN: ${{ secrets.GITHUB_TOKEN }}
106+
run: |
107+
git fetch origin
108+
git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }}
109+
git add "*.md" metrics.json
110+
git commit -m "Update visitor count" || echo "No changes to commit"
111+
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
112+
git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed"
113+
git push origin HEAD:${{ github.event.pull_request.head.ref }}
114+
115+
- name: Commit and push changes (non-PR)
116+
if: github.event_name != 'pull_request'
117+
env:
118+
TOKEN: ${{ secrets.GITHUB_TOKEN }}
119+
run: |
120+
git fetch origin
121+
git checkout ${{ github.event.pull_request.head.ref }} || git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }}
122+
git add "*.md" metrics.json
123+
git commit -m "Update visitor count" || echo "No changes to commit"
124+
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
125+
git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed"
126+
git push origin HEAD:${{ github.event.pull_request.head.ref }}
127+
128+
- name: Create Pull Request (non-PR)
129+
if: github.event_name != 'pull_request'
130+
uses: peter-evans/create-pull-request@v6
131+
with:
132+
token: ${{ secrets.GITHUB_TOKEN }}
133+
branch: update-visitor-count
134+
title: "Update visitor count"
135+
body: "Automated update of visitor count"
136+
base: main
79137
```
80138
81139
## Files structure

0 commit comments

Comments
 (0)