Skip to content

Track Traffic

Track Traffic #78

Workflow file for this run

name: Track Traffic
on:
schedule:
- cron: "0 0 * * *" # Runs at 00:00, every day
workflow_dispatch: # Or can manually run
permissions:
contents: write
actions: read
jobs:
capture-traffic:
runs-on: ubuntu-latest
steps:
# Checkout the track-traffic branch
- name: Checkout repo
uses: actions/checkout@v3
with:
ref: track-traffic
- name: Get yesterday's clone traffic
id: clones
env:
GH_TOKEN: ${{ secrets.TRAFFIC_SECRET }}
run: |
# yesterday's clones have date = `yesterday`
YESTERDAY=$(date -I -d "yesterday")
# get all traffic from yesterday
DAILY_JSON=$(gh api repos/${GITHUB_REPOSITORY}/traffic/clones \
--jq ".clones[] | select(.timestamp | startswith(\"$YESTERDAY\"))")
# get total clone count and unique clone count
DAILY_COUNT=$(echo "$DAILY_JSON" | jq -r '.count // 0')
DAILY_UNIQUES=$(echo "$DAILY_JSON" | jq -r '.uniques // 0')
# Output variables date, count, and uniques to next step
echo "date=$YESTERDAY" >> $GITHUB_OUTPUT
echo "count=$DAILY_COUNT" >> $GITHUB_OUTPUT
echo "uniques=$DAILY_UNIQUES" >> $GITHUB_OUTPUT
- name: Append to clone_history.csv
run: |
# Create csv on first run
if [ ! -f .github/metrics/clone_history.csv ]; then
echo "date,daily_clones,daily_unique_cloners" > .github/metrics/clone_history.csv
fi
# Append yesterday’s entry to file
echo "${{ steps.clones.outputs.date }},${{ steps.clones.outputs.count }},${{ steps.clones.outputs.uniques }}" >> .github/metrics/clone_history.csv
- name: Commit and push
run: |
git config --local user.email "actions@github.com"
git config --local user.name "GitHub Actions"
git add .github/metrics/clone_history.csv
git commit -m "Daily clone stats (${{ steps.clones.outputs.date }})" || echo "No changes"
git push origin HEAD:track-traffic