PureTools

GitHub Actions: CI/CD From Zero to Deploy

PureTools Team· 8 min read
GitHub Actions: CI/CD From Zero to Deploy

GitHub Actions: Your Free CI/CD Pipeline

GitHub Actions runs workflows in response to events (push, pull request, schedule). It's free for public repos and comes with 2000 minutes/month for private repos. No separate CI service needed.

Basic Workflow

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
      - run: npm ci
      - run: npm run lint
      - run: npm test

Key Concepts

ConceptMeaning
WorkflowA YAML file in .github/workflows/
EventWhat triggers the workflow (push, PR, schedule, manual)
JobA set of steps that run on a runner (can run in parallel)
StepIndividual command or action
ActionReusable unit (e.g., actions/checkout@v4)
RunnerThe VM that executes the job
SecretEncrypted variable (Settings → Secrets)

Full CI/CD: Test → Build → Deploy

name: CI/CD

on:
  push:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build

  deploy:
    needs: test  # only runs if test passes
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
      - run: npm ci
      - run: npm run build
      - name: Deploy to production
        env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: |
          npx wrangler deploy --token $DEPLOY_TOKEN

Matrix Builds (Test Multiple Versions)

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm test

Useful Patterns

# Cache dependencies
- uses: actions/setup-node@v4
  with:
    cache: 'npm'  # auto-caches node_modules

# Run only when specific files change
on:
  push:
    paths:
      - 'src/**'
      - 'package.json'

# Scheduled (cron)
on:
  schedule:
    - cron: '0 8 * * 1'  # Every Monday at 8 AM UTC

# Manual trigger
on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Deploy environment'
        required: true
        default: 'staging'

Tips

  • Use actions/checkout@v4 (always pin major version)
  • Enable dependency caching for faster runs
  • Use needs: to create job dependencies
  • Keep secrets in Settings → Secrets, reference with ${{ secrets.NAME }}
  • Use if: conditions to control when jobs/steps run

Validate your YAML: YAML Formatter — check your workflow file syntax.