
GitHub CLI (gh) nâng cao: Tự động hóa PR, issue, release trực tiếp từ terminal
GitHub CLI (`gh`) là công cụ chính thức của GitHub giúp developer thao tác GitHub mà không cần mở trình duyệt. Bạn có thể tạo PR, review code, quản lý issue, release, workflow — tất cả từ terminal với tốc độ nhanh gấp 5-10 lần so với web UI. Bài viết này đi sâu vào các lệnh nâng cao và tự động hóa mà hầu hết developer bỏ qua.

Cài đặt và xác thực nhanh
Cài đặt trên macOS/Linux:
# macOS
brew install gh
# Linux (Debian/Ubuntu)
sudo apt install gh
# Authenticate
gh auth login
# Chọn GitHub.com → HTTPS → Y → browser login hoặc token
Xác minh trạng thái:
gh auth status
# ✓ Logged in to github.com as hungs20 (hungs20)
# ✓ Token: ghp_xxxx... (scopes: repo, workflow, gist, ...)
PR workflow nâng cao
Tạo PR từ branch hiện tại với title/body từ stdin hoặc file:
# Tạo PR với body từ file
gh pr create --title "feat: add OAuth2 login flow" --body-file /tmp/pr_body.md
# Tạo PR draft (không merge ngay)
gh pr create --draft --title "WIP: refactor auth module"
# Tạo PR với assignee, label, milestone
gh pr create
--title "fix: handle token expiry in API client"
--body "Token expiry check use `<` not `<=`"
--assignee @me,teammate
--label bug,priority:high
--milestone v2.1

Review code từ terminal
Bạn không cần mở Pull Request page để review. `gh` cung cấp đầy đủ khả năng comment, approve, request changes:
# Xem diff của PR
gh pr diff 1234
# Comment inline trên dòng cụ thể
gh pr review 1234 --comment --body "Lỗi logic ở dòng 42, nên dùng `<` thay vì `<=`"
# Approve PR
gh pr review 1234 --approve --body "LGTM, good to merge"
# Request changes
gh pr review 1234 --request-changes --body "Cần thêm error handling cho edge case"
# Merge PR (squash, rebase, hoặc commit)
gh pr merge 1234 --squash --delete-branch
Quản lý Issue và Project
# Tạo issue với template
gh issue create --title "Bug: login timeout on mobile" --body "..." --label bug
# List issue assigned cho mình
gh issue list --assignee @me --state open --label bug
# Close issue với comment
gh issue close 567 --comment "Fixed in #1234"
# Chuyển issue sang project
gh project item-add --project "Backlog" --owner org 567
Release automation
# Tạo release từ tag
gh release create v2.1.0
--title "v2.1.0: OAuth2 login, dark mode"
--notes-file /tmp/release_notes.md
--target main
# Upload asset
gh release upload v2.1.0 ./dist/app-darwin-arm64.zip
# List releases
gh release list --limit 10
# Download asset
gh release download v2.1.0 --pattern "*.zip" --dir ./downloads

Tự động hóa với script Bash
Kết hợp `gh` với Bash để tự động hóa quy trình:
#!/bin/bash
# auto-pr.sh: Tự động tạo PR từ branch feature
BRANCH=$(git branch --show-current)
BASE="main"
TITLE="feat: $(basename $BRANCH)"
# Lấy changed files
CHANGED=$(git diff --name-only $BASE...HEAD)
# Tạo PR
gh pr create
--base $BASE
--head $BRANCH
--title "$TITLE"
--body "## Changes
$(git log --oneline $BASE...HEAD)
## Files
$CHANGED
🤖 Generated by auto-pr.sh"
--label "auto-pr"
echo "PR created: $(gh pr view --json url -q .url)"
Workflow và Actions từ CLI
# Xem workflow runs
gh run list --workflow "CI.yml" --limit 5
# Xem log của run cụ thể
gh run view 1234567 --log-failed
# Re-run failed jobs
gh run rerun 1234567 --failed
# Trigger workflow_dispatch
gh workflow run deploy.yml -f environment=staging -f version=2.1.0
So sánh gh vs tự xây curl script
| Tiêu chí | GitHub CLI (gh) | Tự curl + jq |
|---|---|---|
| Setup | 1 lần auth, commands sẵn | Viết script, quản lý token |
| Output format | JSON/table/text đẹp | Raw JSON, cần jq/yq |
| Pagination | Tự động (–limit, –page) | Manual page param |
| Error handling | Exit code, retry built-in | Custom HTTP status check |
| Khi nào tự xây | Không cần | Rate limit cần control chi tiết |
Best practice và tips
- Alias phổ biến: thêm `alias gpr=’gh pr create’`, `alias gco=’gh pr checkout’` vào `.zshrc`/`.bashrc`.
- Template: dùng `gh config set editor “code –wait”` để mở PR body trong VS Code.
- Filter: kết hợp `–json` + `–jq` để lọc fields cần thiết, ví dụ `gh issue list –json number,title –jq ‘.[] | “(.number): (.title)”‘`.
- Git hook: pre-push hook tự động tạo PR nếu branch có prefix `feature/`.
Kết luận
GitHub CLI `gh` tiết kiệm hàng giờ mỗi tuần cho team làm việc với GitHub. Đừng tự build tool từ curl/jq khi `gh` đã làm sẵn, có thể extend qua alias/script khi cần. Bắt đầu bằng cách thay thế các thao tác web UI lặp lại: PR review, issue triage, release creation — automation sẽ follow naturally.
Nguồn: GitHub CLI Manual, gh repository
