Write a GitHub Action to Count Repo Stars
I receive a Telegram notification whenever my OSS projects get a new star — it’s motivating. The automation is powered by GitHub Actions. To improve the experience, I wanted the notification to include the total star count, so I built a reusable Action.
Action marketplace: Repo Star Count. Read on for how it works.
How to get the star count
GitHub Actions doesn’t expose star count as an environment variable, and the star event payload lacks that field. Use the GitHub REST API instead. Key snippet:
curl -s 'https://api.github.com/repos/${{ inputs.repoPath }}?page=$i&per_page=100' | jq .stargazers_count
Note
jq is preinstalled on GitHub-hosted runners.
Build the GitHub Action
To reuse across projects, package it as a composite Action that outputs the star count.
Action源码
name: 'Repo Star Count'
summary: 'Get count of Github repository stars'
author: 'Alan He'
inputs:
repoPath:
summary: 'Repository Path'
required: false
default: ${{github.repository}}
outputs:
stars:
summary: "Repo Stars"
value: ${{ steps.repo-stars.outputs.stars }}
runs:
using: composite
steps:
- id: repo-stars
run: |
STARS=`curl -s 'https://api.github.com/repos/${{ inputs.repoPath }}?page=$i&per_page=100' | jq .stargazers_count`
echo "::set-output name=stars::$STARS"
shell: bash
branding:
icon: 'award'
color: 'green'
Consume the Action
Reference it in your workflow and use the output value:
...
steps:
- name: Star Count
id: repo-stars
uses: alanhg/repo-star-count-action@master
# ${{steps.repo-stars.outputs.stars}}
...
Once installed, you don’t need to care how the value is fetched — just use the output.
Pitfalls I hit
Docs are thorough, but a few gotchas:
Marketplace listings can lag. If the release succeeded, you can still install the Action.
Workflow error line numbers can be off by one.
Comments in certain places can break scripts:
- id: repo-stars run: | # STARS=`curl -s 'https://api.github.com/repos/${{ inputs.repoPath }}?page=$i&per_page=100' | jq .stargazers_count` echo "::set-output name=stars::$STARS" shell: bash
Export values via Action outputs — not via environment variables — so the workflow can consume them.
Final Thoughts
If it’s repetitive, automate it. Encapsulating the star count logic in an Action hides details and makes it reusable in any repo.
References
Official docs are still the best source: