A guide on automating GitHub activity to keep your contribution streak alive using Python and GitHub Actions.
๐ How I Built a Daily Streak Keeper with Python & GitHub Actions
We've all been thereโstaring at that one gray square in our GitHub contribution graph that broke a perfect streak. Whether you're busy, sick, or just forgot to push code, losing a streak can be demotivating.
I decided to solve this problem by automating a daily commit using Python and GitHub Actions. Here is how I built my Daily Streak Keeper.
๐ก The Idea
The goal was simple:
- Fetch a daily quote (to make the commit meaningful).
- Update the README.md with the new quote.
- Commit and push the changes automatically every day.
๐ ๏ธ The Tech Stack
- Python: To fetch the quote and update the file.
- GitHub Actions: To schedule the script to run daily.
๐ป The Code
- The Python Script (
update_quote.py)
I wrote a simple Python script that fetches a random quote from an API and updates the README.md file. I used dummyjson.com for stability.
import requests
import datetime
def get_quote():
url = "https://dummyjson.com/quotes/random"
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return f"\"{data['quote']}\" โ {data['author']}"
else:
return "Could not fetch a quote today. Keep coding!"
except Exception as e:
return f"Error fetching quote: {e}"
def update_readme(quote):
date_str = datetime.datetime.now().strftime("%Y-%m-%d")
Template for the README
readme_content = f"""
๐ Daily Streak Keeper
This repository automatically updates itself every day at 12:00 PM Nairobi Time to keep my GitHub contribution streak alive.
๐
Quote for {date_str}
> {quote}
---
Last updated automatically by GitHub Actions.
"""
with open("README.md", "w", encoding="utf-8") as file:
file.write(readme_content)
if __name__ == "__main__":
quote = get_quote()
update_readme(quote)
- The Automation (
daily-quote.yml)
Next, I needed this script to run automatically. GitHub Actions is perfect for this. I set up a workflow that runs every day at 12:00 PM Nairobi Time (09:00 UTC).
name: Daily Quote Update
on:
schedule:
09:00 UTC = 12:00 PM Nairobi Time
- cron: '0 9 '
workflow_dispatch:
jobs:
update-readme:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run Python script
run: |
python update_quote.py
- name: Commit and Push
run: |
git config --global user.name "Quote Bot"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git add README.md
if git diff-index --quiet HEAD; then
echo "No changes to commit"
else
git commit -m "Daily Quote: Updated README with new inspiration ๐"
git push
fi
๐ฏ Conclusion
This was a fun little project to ensure my profile stays active while also giving me a nice quote to read every day. Itโs a great example of how powerful and easy GitHub Actions can be for automating simple tasks.
Check out the code in the repository and feel free to fork it to create your own streak keeper! REPO
Top comments (0)