GitHub Actions: Testing, Build, Release, and Deploy Pipelines

Practical CI/CD recipes for testing, build matrices, release creation, package publishing, and environment-based deployments in GitHub Actions.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Testing and build pipelines
Node.js CI workflow
name: Node CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  ci:
    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 -- --ci
      - run: npm run build

# Install dependencies, lint, test, and build a Node project.

Python CI workflow
- uses: actions/setup-python@v5
  with:
    python-version: "3.12"

- run: pip install -r requirements.txt
- run: pytest -q

# Set up Python, install dependencies, and run tests.

Build a Docker image
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
  with:
    context: .
    push: false
    tags: my-app:ci

# Use Buildx for modern Docker builds.

## Release and deployment workflows
Create a GitHub release on tag push
on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true

# Publish a release whenever a version tag is pushed.

Publish an npm package
- uses: actions/setup-node@v4
  with:
    node-version: 20
    registry-url: https://registry.npmjs.org

- run: npm ci
- run: npm publish
  env:
    NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

# Publish to npm from GitHub Actions.

Deploy only from main after tests
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: echo "run tests here"

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: ./scripts/deploy.sh

# Gate deployments on successful CI from the main branch.

Recommended next

No recommendations yet.