DEV Community

Alex Spinov
Alex Spinov

Posted on

GitHub Actions Has a Free API That Automates Your Entire Development Workflow

GitHub Actions is the CI/CD platform built into GitHub. Its workflow API automates testing, building, deploying, and everything in between.

CI Pipeline: Test on Every Push

# .github/workflows/ci.yml
name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

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 }}
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm run typecheck
      - run: npm test -- --coverage
      - uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.node-version }}
          path: coverage/
Enter fullscreen mode Exit fullscreen mode

Deploy on Release

name: Deploy
on:
  release:
    types: [published]

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    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 Cloudflare
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          command: deploy --env production
Enter fullscreen mode Exit fullscreen mode

Scheduled Jobs (Cron)

name: Daily Scrape
on:
  schedule:
    - cron: '0 9 * * *'  # 9 AM UTC daily
  workflow_dispatch: # Manual trigger

jobs:
  scrape:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: node scripts/scrape-prices.js
      - name: Commit results
        run: |
          git config user.name 'github-actions'
          git config user.email 'actions@github.com'
          git add data/
          git diff --staged --quiet || git commit -m 'Update scraped data'
          git push
Enter fullscreen mode Exit fullscreen mode

Reusable Workflows

# .github/workflows/reusable-test.yml
on:
  workflow_call:
    inputs:
      node-version:
        type: string
        default: '20'
    secrets:
      NPM_TOKEN:
        required: false

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: '${{ inputs.node-version }}' }
      - run: npm ci
      - run: npm test
Enter fullscreen mode Exit fullscreen mode
# Use it
jobs:
  test:
    uses: ./.github/workflows/reusable-test.yml
    with:
      node-version: '22'
Enter fullscreen mode Exit fullscreen mode

Matrix Strategy: Parallel Testing

strategy:
  matrix:
    os: [ubuntu-latest, macos-latest, windows-latest]
    node: [18, 20, 22]
    exclude:
      - os: windows-latest
        node: 18
  fail-fast: false
Enter fullscreen mode Exit fullscreen mode

Composite Actions

# .github/actions/setup/action.yml
name: Setup Project
runs:
  using: composite
  steps:
    - uses: actions/setup-node@v4
      with:
        node-version: 20
        cache: npm
    - run: npm ci
      shell: bash
    - run: npx playwright install --with-deps
      shell: bash
Enter fullscreen mode Exit fullscreen mode

Automate scraping workflows? My Apify tools + GitHub Actions = scheduled data collection.

Custom CI/CD pipeline? Email spinov001@gmail.com

Top comments (0)