|
| 1 | +name: Use Visitor Counter Logic |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + schedule: |
| 8 | + - cron: '0 0 * * *' # Runs daily at midnight |
| 9 | + workflow_dispatch: # Allows manual triggering |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + update-visitor-count: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout current repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Shallow clone visitor counter logic |
| 26 | + run: git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git |
| 27 | + |
| 28 | + - name: Set up Node.js |
| 29 | + uses: actions/setup-node@v4 |
| 30 | + with: |
| 31 | + node-version: '20' |
| 32 | + |
| 33 | + - name: Install dependencies for github-visitor-counter |
| 34 | + run: | |
| 35 | + cd github-visitor-counter |
| 36 | + npm ci |
| 37 | +
|
| 38 | + - name: Run visitor counter logic (updates markdown badges and metrics.json) |
| 39 | + run: node github-visitor-counter/update_repo_views_counter.js |
| 40 | + env: |
| 41 | + TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }} |
| 42 | + REPO: ${{ github.repository }} |
| 43 | + |
| 44 | + - name: Move generated metrics.json to root |
| 45 | + run: mv github-visitor-counter/metrics.json . |
| 46 | + |
| 47 | + - name: List files for debugging |
| 48 | + run: | |
| 49 | + ls -l |
| 50 | + ls -l github-visitor-counter |
| 51 | +
|
| 52 | + - name: Clean up visitor counter logic |
| 53 | + run: rm -rf github-visitor-counter |
| 54 | + |
| 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 |
| 61 | + - name: Commit and push changes (PR) |
| 62 | + if: github.event_name == 'pull_request' |
| 63 | + env: |
| 64 | + TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + run: | |
| 66 | + git fetch origin |
| 67 | + git checkout ${{ github.head_ref }} |
| 68 | + git add "*.md" metrics.json |
| 69 | + git commit -m "Update visitor count" || echo "No changes to commit" |
| 70 | + git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} |
| 71 | + git pull --rebase origin ${{ github.head_ref }} || echo "No rebase needed" |
| 72 | + git push origin HEAD:${{ github.head_ref }} |
| 73 | +
|
| 74 | + # Commit and push logic for non-PR events (schedule, workflow_dispatch) |
| 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 add "*.md" metrics.json |
| 83 | + git commit -m "Update visitor count" || echo "No changes to commit" |
| 84 | + git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} |
| 85 | + git pull --rebase origin ${{ github.ref_name }} || echo "No rebase needed" |
| 86 | + git push origin HEAD:${{ github.ref_name }} |
| 87 | +
|
| 88 | + - name: Create Pull Request (non-PR) |
| 89 | + if: github.event_name != 'pull_request' |
| 90 | + uses: peter-evans/create-pull-request@v6 |
| 91 | + with: |
| 92 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + branch: update-visitor-count |
| 94 | + title: "Update visitor count" |
| 95 | + body: "Automated update of visitor count" |
| 96 | + base: main |
0 commit comments