GitHub Actions CI/CD
GitHub-native CI/CD with workflows, reusable workflows, and composite actions.
CI/CDIntermediate
Why
GitHub Actions is built into GitHub — no external CI service. Workflows are YAML in .github/workflows, with matrix builds, secrets, and 10k+ marketplace actions.
When
Use GitHub Actions for projects on GitHub. Avoid for complex self-hosted runners (use Jenkins).
How
1. Workflow — .github/workflows/ci.yml
2. Trigger — on: push, pull_request, schedule
3. Job — runs-on: ubuntu-latest
4. Step — uses: actions/checkout@v4
5. Matrix — strategy: matrix for multi-version
6. Secrets — ${{ secrets.NAME }}
7. Cache — actions/cache for deps
Example
# .github/workflows/ci.yml
name: CI
on: { push: { branches: [main] }, pull_request: {} }
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix: { node: [18, 20, 22] }
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: ${{ matrix.node }}, cache: npm }
- run: npm ci
- run: npm test
- run: npm run build
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- run: npx vercel --prod --token ${{ secrets.VERCEL_TOKEN }}
Common Problems
- ⚠Workflow not triggering — check on: syntax and branch filters
- ⚠Secrets not available — defined in repo settings, not in workflow file
- ⚠Configuration drift between environments — use infrastructure-as-code (Terraform/Pulumi) to keep dev/staging/prod identical
Best Practices
- ✓Use reusable workflows — DRY, share across repos via workflow_call
- ✓Pin actions to SHA — not @v4 (mutable tag), use @v4 → commit SHA for security
- ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit