Update Clone Count #17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Clone Count | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs daily at midnight | |
workflow_dispatch: # Allows manual triggering | |
jobs: | |
update-clone-count: | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Fetch clone count | |
id: fetch_count | |
run: | | |
response=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/traffic/clones") | |
echo "API Response: $response" | |
today=$(date -u +"%Y-%m-%d") | |
clone_count=$(echo "$response" | jq --arg today "$today" '.clones[] | select(.timestamp | startswith($today)) | .count') | |
if [ -z "$clone_count" ]; then | |
echo "CLONE_COUNT=0" >> $GITHUB_ENV # Default to 0 if no clones today | |
else | |
echo "CLONE_COUNT=$clone_count" >> $GITHUB_ENV | |
fi | |
echo "API-retrieved clones count for the day ($today): $clone_count" | |
- name: Update Gist | |
env: | |
GIST_ID: 6dc3699f407bffa35cd6670ad08dd307 | |
run: | | |
gist_content=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \ | |
"https://api.github.com/gists/$GIST_ID") | |
#echo "Gist Response: $gist_content" | |
current_count=$(echo "$gist_content" | jq -r '.files."clone_count.json".content' | jq '.count' | tr -d '"') | |
echo "Current clones count from Gist: $current_count" | |
new_count=$((current_count + CLONE_COUNT)) | |
new_content="{\"count\":$new_count}" | |
echo "New content: $new_content" | |
payload=$(jq -n \ | |
--arg content "{\"count\": \"$new_count\"}" \ | |
'{"files": {"clone_count.json": {"content": $content}}}') | |
echo "Constructed payload: $payload" | |
curl -X PATCH -H "Authorization: token ${{ secrets.PAT }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-d "$payload" \ | |
"https://api.github.com/gists/$GIST_ID" |