GitHub CLI (gh): Workflow Automation๐
Part of a pathway: Debugging With Nothing But a Terminal
Consult the map
-
Debugging With Nothing But a Terminal โ step 19 of 20
โ Git Workflows for Infrastructure ยท you are here ยท GitHub Actions for SREs (coming soon) โ
A finished code change usually means opening a browser, navigating to the repo, clicking "New Pull Request," filling out the form, and adding reviewers. There is a better way.
The GitHub CLI (gh) brings the power of GitHub directly to your terminal. For SREs, this means you can manage PRs, view CI/CD status, and even interact with GitHub Actions without ever leaving your shell.
Installation๐
| Install gh on Linux | |
|---|---|
| Install gh on macOS | |
|---|---|
Then authenticate once โ this is the first step in Quick Start below.
Quick Start: Get Productive in 3 Minutes๐
- Auth:
gh auth login - Create a PR:
gh pr create --title "feat: add vpc logging" --body "See Jira-123" - Check CI:
gh run watch(Watch your GitHub Actions run in real-time) - Merge:
gh pr merge --auto --squash
Four commands, one continuous loop, no browser tab required:
graph LR
Auth["gh auth login"] --> Create["gh pr create"]
Create --> Watch["gh run watch"]
Watch --> Merge["gh pr merge"]
style Auth fill:#2d3748,stroke:#cbd5e0,stroke-width:2px,color:#fff
style Create fill:#2d3748,stroke:#cbd5e0,stroke-width:2px,color:#fff
style Watch fill:#d69e2e,stroke:#cbd5e0,stroke-width:2px,color:#000
style Merge fill:#2f855a,stroke:#cbd5e0,stroke-width:2px,color:#fff
Why GitHub CLI Matters for Platform Work๐
SREs often manage dozens of repositories simultaneously. Using a browser for every PR or Action becomes a major friction point. gh allows you to treat GitHub as a command-line tool that can be integrated into your scripts and aliases.
Common Scenarios๐
Instead of refreshing a browser tab, watch your deployment pipeline from your terminal:
Need to review three dependency updates from Dependabot?
Use gh api to perform complex tasks that aren't available in standard commands, returning clean JSON that you can pipe to jq.
| Find Large Repos | |
|---|---|
Essential Commands๐
Three command groups cover nearly everything above:
-
PR Management (
gh pr)
Why it matters: Create, list, view, and merge pull requests without a mouse.
- Shows PRs relevant to you: created, review-requested, or assigned.
- Shows the actual code diff for PR #123, right in the terminal.
-
Actions Workflow (
gh run)
Why it matters: Monitor and trigger GitHub Actions. Invaluable for debugging failing CI/CD pipelines.
- See recent runs and their status at a glance.
- Stream the full log of the most recent run โ no need to open the Actions tab.
-
API Access (
gh api)
Why it matters: Full access to the GitHub REST API for anything the built-in commands don't cover โ already authenticated, no separate token to manage.
Check Your Own Rate Limit
Practice Problems๐
Practice Problem 1: Speeding Up Merges
You've opened a PR and you know the CI will take 5 minutes. You don't want to wait around to click "Merge." What command can you run to tell GitHub to merge the PR as soon as the checks pass?
Answer
| Auto-Merge Once Checks Pass | |
|---|---|
--auto flag enables "auto-merge," which will complete the merge automatically once all required status checks have passed.
Practice Problem 2: Debugging Actions
A GitHub Action failed, and you need to see the logs for the specific failed step. How do you do this without the UI?
Answer
| Stream Logs from the Latest Run | |
|---|---|
grep or jq if the logs are structured!
Key Takeaways๐
| Command | Purpose |
|---|---|
gh pr create |
Open a new Pull Request |
gh run watch |
Monitor Actions in real-time |
gh pr checkout |
Pull a PR's branch locally |
gh repo clone |
Intelligent cloning (handles SSH/HTTPS) |
gh alias set |
Create custom GitHub commands |
What's Next๐
gh api returning raw JSON you can pipe into jq is the same wire-level data Seeing API Traffic taught you to read with curl -v, this time from an authenticated CLI instead of a raw request.
This is the last free step in the Debugging With Nothing But a Terminal pathway. Turning everything you just did by hand into something that runs itself โ GitHub Actions for SREs โ is Mastery tier, coming soon.
Further Reading๐
Official Documentation๐
- GitHub CLI Manual - Complete reference for every command.
- GitHub CLI Extension Guide - Learn how to build your own
ghsubcommands.
Related Tools & Alternatives๐
- Lab (for GitLab) - A similar tool for GitLab users.
- Octokit - Official GitHub SDKs for building deeper integrations.
Deep Dives๐
- Seeing API Traffic: curl -v and the Network Tab - The same request/response anatomy
ghis automating for you underneath.