Automate GitHub Streak Checks With Actions

Alex Johnson
-
Automate GitHub Streak Checks With Actions

Hey everyone! Keeping up with your GitHub streak can be a real grind, right? Missing a day can feel like a major setback. That's why I'm stoked to walk you through setting up a slick GitHub Actions workflow that automatically keeps tabs on your contributions and gives you a heads-up if your streak is in danger. This workflow will run daily, check your activity, and even create an issue if you're at risk of breaking your streak. It's like having a personal streak bodyguard!

What We're Building: The Streak Savior

So, what exactly are we building? We're creating a GitHub Actions workflow that automates the process of checking your GitHub contributions. Here's the lowdown:

  • Daily Checks: The workflow runs every day at a time you specify (we'll use 8 PM UTC as the default, but you can change it). This ensures you're always on top of your game.
  • Contribution Count: The workflow checks your GitHub activity for the day. If your contributions are zero, it means you haven't committed anything.
  • Issue Alert: If no contributions are detected, the workflow automatically creates an issue in your repository. This issue serves as a warning, reminding you to push a commit and keep your streak alive. It's like a gentle nudge to get back on track.
  • Manual Trigger: We'll also set up the workflow to be manually triggered. This is super handy for checking your streak on demand, or if you just want to give it a test run.
  • Easy Setup: The setup process is designed to be straightforward, with clear instructions and example configurations.

This project is all about making streak maintenance easier and more automated. It's perfect for developers who are serious about their GitHub presence and want to stay consistent. The goal is to reduce the manual effort involved in tracking your streak and provide timely alerts to prevent any potential breaks. Using GitHub Actions, we can automate the whole process, so you don't have to worry about it.

Setting Up Your GitHub Actions Workflow

Alright, let's dive into the nitty-gritty of setting up your GitHub Actions workflow. This involves creating a YAML file in your repository that defines the workflow's behavior. Here's a step-by-step guide:

  1. Create the Workflow File: Inside your GitHub repository, navigate to the .github/workflows directory. If this directory doesn't exist, go ahead and create it. Inside this directory, create a new file named something like streak-check.yml. This is where we'll define our workflow.

  2. Define the Workflow Name and Triggers: At the top of your streak-check.yml file, give your workflow a descriptive name using the name: key. Then, define the triggers that will kick off the workflow. We'll use two triggers: schedule and workflow_dispatch. The schedule trigger runs the workflow automatically based on a cron expression, while workflow_dispatch allows for manual triggering.

    name: Daily Streak Check
    
    on:
      schedule:
        - cron: '0 20 * * *'  # Run at 8 PM UTC daily
      workflow_dispatch:  # Allow manual trigger
    
  3. Define the Jobs: Workflows are made up of one or more jobs. In our case, we'll have a single job called check-streak. This job will run on an Ubuntu virtual machine (specified by runs-on: ubuntu-latest).

    jobs:
      check-streak:
        runs-on: ubuntu-latest
        steps:
          # ... (steps will be added in the next sections) 
    
  4. Checkout the Repository: The first step within the job is to check out your repository's code. This step uses the actions/checkout@v3 action.

    steps:
      - uses: actions/checkout@v3
    
  5. Set Up Python: If your script is written in Python (which is the example here), you'll need to set up a Python environment. This involves using the actions/setup-python@v4 action to specify the Python version.

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'
    
  6. Install Dependencies: Install any Python packages your script relies on. This is typically done using pip and a requirements.txt file.

    - name: Install dependencies
      run: pip install -r requirements.txt
    
  7. Check GitHub Streak: This is where the magic happens. You'll run the Python script that checks your GitHub contributions. Important: You'll need to set up environment variables for your GitHub username and a personal access token (PAT). These are stored as repository secrets. The example shows how to access these secrets using {{ secrets.GH_USERNAME }} and {{ secrets.GH_PAT }}.

    - name: Check GitHub streak
      env:
        GITHUB_USERNAME: ${{ secrets.GH_USERNAME }}
        GITHUB_PAT: ${{ secrets.GH_PAT }}
      run: python main.py
    
  8. Create Issue if Streak at Risk: If the Python script (in main.py) detects that your streak is at risk (no contributions), this step will create a GitHub issue. The if: failure() condition ensures this step only runs if the previous step (the Python script) fails. The actions/github-script@v6 action is used to create the issue. The issue will include a title, a body, and a label (e.g., streak-alert).

    - name: Create issue if streak at risk
      if: failure()
      uses: actions/github-script@v6
      with:
        script: |
          github.rest.issues.create({
            owner: context.repo.owner,
            repo: context.repo.repo,
            title: '⚠️ GitHub Streak Alert!',
            body: 'You have 0 contributions today. Push a commit to keep your streak alive! 🔥',
            labels: ['streak-alert']
          })
    
  9. Configure Secrets: Before running the workflow, you'll need to configure two repository secrets in your GitHub repository settings:

    • GH_USERNAME: Your GitHub username.
    • GH_PAT: A personal access token with the repo scope (necessary for creating issues). Generate this token in your GitHub settings.
  10. Testing and Troubleshooting: After setting up the workflow, test it. You can trigger it manually via the

You may also like