🔗 DevOps Day 8 — Build a Beginner-Friendly GitHub API Project with Shell Scripting

 

🚀 Introduction

Welcome to Day 8 of the DevOps series!
In this lesson, we’ll build something practical: a GitHub repository tracker using just shell scripting and curl.

This project introduces you to API integration — one of the most important DevOps skills.
By the end of this tutorial, you’ll know how to:

  • Fetch live GitHub data using an API

  • Parse results using a simple tool (jq)

  • Automate your report using a cron job




💡 What Is an API?

An API (Application Programming Interface) allows two systems to communicate.
In our case, we’ll use GitHub’s REST API to ask questions like:

  • “How many ⭐ stars does this repo have?”

  • “How many open PRs or issues exist?”

GitHub’s API responds in JSON format — a lightweight data structure we can read easily with jq.


🧰 Prerequisites

Before we start:

  1. Install curl and jq

    sudo apt-get install curl jq -y # Ubuntu/Debian

    curl lets you talk to APIs, and jq helps read JSON data.

  2. Know your GitHub repo
    Example:

    • Owner (user/org): torvalds

    • Repo name: linux

  3. (Optional) Add a GitHub Token for higher rate limits

    export GITHUB_TOKEN=ghp_yourTokenHere

    (You can skip this for public repos.)


🧩 Step 1 — Create the Script

Create a file called github_report.sh and paste this code:

#!/usr/bin/env bash
# Simple GitHub Repo Tracker
# Usage: ./github_report.sh <owner> <repo>
set -euo pipefail
OWNER="${1:-}"
REPO="${2:-}"
if [[ -z "$OWNER" || -z "$REPO" ]]; then
echo "Usage: $0 <owner> <repo>"
exit 1
fi
API="https://api.github.com"
HDR=(-H "Accept: application/vnd.github+json")
[[ -n "${GITHUB_TOKEN:-}" ]] && HDR+=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
echo "📊 GitHub Repository Report"
echo "Repository: ${OWNER}/${REPO}"
echo "---------------------------"
# Get repository info
repo_data=$(curl -sS "${HDR[@]}" "${API}/repos/${OWNER}/${REPO}")
stars=$(echo "$repo_data" | jq '.stargazers_count')
forks=$(echo "$repo_data" | jq '.forks_count')
# Open PRs
open_prs=$(curl -sS "${HDR[@]}" \
"${API}/search/issues?q=repo:${OWNER}/${REPO}+type:pr+state:open" | jq '.total_count')
# Open Issues
open_issues=$(curl -sS "${HDR[@]}" \
"${API}/search/issues?q=repo:${OWNER}/${REPO}+type:issue+state:open" | jq '.total_count')
echo "⭐ Stars: $stars"
echo "🍴 Forks: $forks"
echo "🔃 Open PRs: $open_prs"
echo "🐞 Open Issues: $open_issues"
echo "---------------------------"
# Save to a local file
mkdir -p reports
DATE=$(date +%F)
echo "$DATE,$stars,$forks,$open_prs,$open_issues" >> reports/github_stats.csv
echo "✅ Data saved to reports/github_stats.csv"

⚙️ Step 2 — Run the Script

Make it executable:

chmod +x github_report.sh

Then run:

./github_report.sh torvalds linux

Example output:

📊 GitHub Repository Report
Repository: torvalds/linux
---------------------------
⭐ Stars: 177000
🍴 Forks: 57000
🔃 Open PRs: 420
🐞 Open Issues: 980
---------------------------
✅ Data saved to reports/github_stats.csv

⏰ Step 3 — Automate with Cron

You can make this run daily and collect historical data.

Open the cron editor:

crontab -e

Add this line (runs every morning at 8 AM):

0 8 * * * /bin/bash /path/github_report.sh your-user your-repo >> /path/reports/gh.log 2>&1

This keeps a growing CSV (github_stats.csv) — one line per day with your repo stats.


🔐 Step 4 — Best Practices

  • For private repos, you must use a GitHub token with repo:read permission.

  • Keep your token secret (never upload it to GitHub).

  • You can extend the script to show:

    • Last 5 commits

    • Latest workflow run status

    • Top contributors


🌟 Summary

You just built your first DevOps automation with an API using shell scripting!

What you learned today:

  • How to make API calls using curl

  • How to read JSON data with jq

  • How to save and schedule data collection with cron

“APIs + Shell scripting = endless automation possibilities.”

Comments

Popular posts from this blog

🧩 DevOps Day 1 — Fundamentals of DevOps

DevOps Day 23 — Multi-Stage Docker Builds & Distroless Images: Build Smaller, Safer Containers

🚀 DevOps Day 2 — Understanding the SDLC and the Role of DevOps Engineers