GitHub Actions

intermediate

CI/CD pipelines for testing, building, and deploying.

deploymentci-cdgithubautomation
Tested on201619TS5.9
$ bunx sinew add deployment/github-actions
Interactive demo coming soon

1The Problem

Manual deployments are error-prone and time-consuming:

  • Tests are skipped when rushing to ship
  • Builds fail in production but passed locally
  • No automated quality gates
  • Inconsistent deployment process

2The Solution

Use GitHub Actions for automated CI/CD with type checking, testing, and deployment on every push.

3Files

.github/workflows/ci.yml

.github/workflows/ci.ymlYAML
name: CI

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Type check
        run: npm run check-types

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

  build:
    runs-on: ubuntu-latest
    needs: [lint, typecheck, test]
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build
        env:
          SKIP_ENV_VALIDATION: true

.github/workflows/release.yml

.github/workflows/release.ymlYAML
name: Release

on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true

.github/workflows/preview.yml

.github/workflows/preview.ymlYAML
name: Preview

on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build
        env:
          SKIP_ENV_VALIDATION: true

      - name: Deploy to Vercel Preview
        id: deploy
        uses: amondnet/vercel-action@v25
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}

      - name: Comment Preview URL
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `🚀 Preview deployed to: ${{ steps.deploy.outputs.preview-url }}`
            })

.github/dependabot.yml

.github/dependabot.ymlYAML
version: 2
updates:
  - package-ecosystem: npm
    directory: /
    schedule:
      interval: weekly
    open-pull-requests-limit: 10
    groups:
      dependencies:
        patterns:
          - "*"
        exclude-patterns:
          - "eslint*"
          - "typescript*"
      dev-dependencies:
        dependency-type: development
        patterns:
          - "*"

4Configuration

Required Secrets

For Vercel deployment, add these secrets to your repository:

  • VERCEL_TOKEN - Your Vercel API token
  • VERCEL_ORG_ID - Your Vercel organization ID
  • VERCEL_PROJECT_ID - Your Vercel project ID

Branch Protection

Enable branch protection on main:

  1. Go to Settings > Branches
  2. Add rule for main
  3. Enable "Require status checks to pass"
  4. Select the CI jobs as required checks

5Usage

Running Workflows Locally

Use [act](https://github.com/nektos/act) to test workflows locally:

# Install act
brew install act

# Run CI workflow
act push

# Run a specific job
act -j build
Bash

Manual Workflow Triggers

Add workflow_dispatch for manual triggers:

on:
  workflow_dispatch:
    inputs:
      environment:
        description: "Deployment environment"
        required: true
        default: "staging"
        type: choice
        options:
          - staging
          - production
YAML

6Troubleshooting

Workflows not running

  • Check that workflows are in .github/workflows/
  • Verify the on trigger matches your event
  • Check repository settings for Actions permissions

Slow workflows

  • Use concurrency to cancel redundant runs
  • Cache dependencies with actions/cache or actions/setup-node cache
  • Run independent jobs in parallel

Related patterns