DEV Community

AbdulKareem Alabi
AbdulKareem Alabi

Posted on

How I Automated My GitHub Contributions Using Bash (And the Hidden Traps I Solved Along the Way)

Building a consistent coding habit is tough, but maintaining that aesthetic green contribution square on GitHub shouldn't feel like a chore so i thought. So Recently, I decided to build a simple local automation script using a Bash shell script and a system cron job to log daily activity.

It sounded incredibly straightforward: write a script, update a log file, commit it, and let the system run it every hour. But as any developer learns quickly, background system automation loves to throw silent curveballs. Here is exactly what I ran into and how I solved it.

The Ghost Environment (Cron Path Issues)

When I tested my script manually, it worked flawlessly. But when the system cron job triggered it, absolutely nothing happened. I realized that the background cron manager runs in a completely blank, stripped-down environment. It had no idea what the ~ shortcut meant, and it couldn't even find where the git utility lived.

The Fix: I swapped out all shortcut paths for absolute explicit paths (like /home/myusername/) and explicitly exported the system PATH right inside the script instead.

The Passive-Aggressive Passphrase Prompt

My GitHub profile was hooked up via secure SSH keys. Every time the background script tried to run, Git would silently halt the script and wait for me to type my private key passphrase. Because cron runs completely invisibly, the script would just hang and timeout which took me a while though to figure this out.

The Fix: Instead of compromising my laptop's primary SSH security by removing the passphrase, I migrated the project to use a scoped GitHub Personal Access Token (PAT) over https by embedding the scoped token directly into the remote URL configuration, Git could authenticate seamlessly hands-free.

The final script that works

If you are looking to build a resilient local automation script that safely survives background task runners, this is the blueprint that got my setup fully green and automated.

`#!/bin/bash

This is the path to my bash file on my system (yours might be different

Provide system paths so cron can locate system utilities

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Move into the project repository using an absolute path

cd /home/nobleinux/Desktop/auto-commit/

pull first to prevent cloud sync conflicts

/usr/bin/git pull origin main

Perform the automated file modifications

echo "Auto commit: $(date)" >> /home/nobleinux/Desktop/auto-commit/local_log.txt
echo " "

5. Native Git transaction execution

/usr/bin/git add /home/nobleinux/Desktop/auto-commit/local_log.txt
/usr/bin/git commit -m 'chore: automated background sync update by cron'
/usr/bin/git push origin main
`

let me know if this helps you too as well, thank you

Note: the path on my own system is different so yours is likely to be as well

Top comments (1)