New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsGitHub ActionsGitHub Actions
FreeSheet 03 of 6

Build & Test Workflows

A comprehensive developer cheatsheet covering GitHub Actions build and test workflows, featuring essential YAML configurations and operational patterns for Node.js, Python, Java, Docker builds, matrix strategies, dependency caching, and automated test reporting.

Build & Test Overview

Common Build Steps

StepPurposeExample
CheckoutGet the codeactions/checkout@v4
Setup LanguageInstall runtimeactions/setup-node@v4
Install DependenciesInstall packagesnpm ci / pip install
LintCode qualitynpm run lint / flake8
BuildCompile/packagenpm run build / mvn compile
TestRun testsnpm test / pytest
Upload ArtifactsSave build outputactions/upload-artifact@v4

Node.js Workflow

Basic Node.js CI

yaml
name: Node.js CI

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

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x, 20.x, 22.x]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Build
        run: npm run build

      - name: Run tests
        run: npm test

      - name: Upload test coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.node-version }}
          path: coverage/

Node.js with Yarn

yaml
name: Node.js with Yarn

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18.x'
          cache: 'yarn'

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Build
        run: yarn build

      - name: Test
        run: yarn test

Python Workflow

Basic Python CI

yaml
name: Python CI

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

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.9", "3.10", "3.11", "3.12"]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements-dev.txt
          pip install -e .

      - name: Lint with flake8
        run: |
          pip install flake8
          flake8 src/ tests/ --count --max-complexity=10

      - name: Format check with black
        run: |
          pip install black
          black --check src/ tests/

      - name: Run tests with pytest
        run: |
          pip install pytest pytest-cov
          pytest tests/ --cov=src/ --cov-report=xml --cov-report=html

      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.python-version }}
          path: htmlcov/

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.xml
          flags: unittests
          name: codecov-umbrella

Python with Poetry

yaml
name: Python Poetry CI

on:
  push:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          cache: 'poetry'

      - name: Install Poetry
        run: pip install poetry

      - name: Install dependencies
        run: poetry install --no-interaction

      - name: Run tests
        run: poetry run pytest

Java Workflow

Java with Maven

yaml
name: Java CI with Maven

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

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [11, 17, 21]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup JDK
        uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.java-version }}
          distribution: 'temurin'
          cache: 'maven'

      - name: Build and test with Maven
        run: mvn clean verify

      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: test-results-${{ matrix.java-version }}
          path: target/surefire-reports/

Java with Gradle

yaml
name: Java CI with Gradle

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup JDK
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'gradle'

      - name: Build and test with Gradle
        run: ./gradlew build

      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: build/reports/tests/

Docker Build Workflow

Build and Push Docker Image

yaml
name: Docker Build & Push

on:
  push:
    branches: [ main ]
    tags:
      - 'v*'

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha,format=short

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Docker with Multiple Tags

yaml
steps:
  - name: Build and push
    uses: docker/build-push-action@v5
    with:
      context: .
      push: true
      tags: |
        myusername/myapp:latest
        myusername/myapp:${{ github.sha }}
        myusername/myapp:${{ github.run_number }}

Matrix Builds

What is Matrix Build?

Matrix builds allow you to run jobs with multiple configurations (e.g., different versions, operating systems).

Basic Matrix

yaml
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
        os: [ubuntu-latest, windows-latest]

    steps:
      - uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm test

Include/Exclude in Matrix

yaml
strategy:
  matrix:
    node-version: [18, 20, 22]
    os: [ubuntu-latest, windows-latest]
    include:
      # Additional config for specific combination
      - node-version: 18
        os: ubuntu-latest
        environment: production
      - node-version: 20
        os: windows-latest
        environment: staging
    exclude:
      # Remove specific combinations
      - node-version: 18
        os: windows-latest

Complex Matrix with Additional Variables

yaml
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest]
    version: [1.0, 2.0]
    include:
      - os: ubuntu-latest
        runner: linux
      - os: windows-latest
        runner: windows
    exclude:
      - os: windows-latest
        version: 1.0

Caching Dependencies

Why Cache?

Caching speeds up workflows by reusing previously downloaded dependencies.

Node.js Cache (npm)

yaml
steps:
  - name: Setup Node.js
    uses: actions/setup-node@v4
    with:
      node-version: '18.x'
      cache: 'npm'

  - name: Install dependencies
    run: npm ci

Python Cache (pip)

yaml
steps:
  - name: Setup Python
    uses: actions/setup-python@v4
    with:
      python-version: '3.11'
      cache: 'pip'
      cache-dependency-path: '**/requirements-dev.txt'

  - name: Install dependencies
    run: pip install -r requirements-dev.txt

Custom Cache with actions/cache

yaml
steps:
  - name: Cache node_modules
    uses: actions/cache@v3
    with:
      path: ~/.npm
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
        ${{ runner.os }}-node-

  - name: Install dependencies
    run: npm ci

Multiple Cache Keys

yaml
steps:
  - name: Cache pip
    uses: actions/cache@v3
    with:
      path: ~/.cache/pip
      key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
      restore-keys: |
        ${{ runner.os }}-pip-

  - name: Cache pytest
    uses: actions/cache@v3
    with:
      path: .pytest_cache
      key: ${{ runner.os }}-pytest-${{ hashFiles('**/requirements.txt') }}

Test Reporting

JUnit/TestNG Reports

yaml
steps:
  - name: Run tests
    run: npm test

  - name: Upload test results
    uses: actions/upload-artifact@v4
    with:
      name: test-results
      path: test-results/*.xml

Pytest Reports

yaml
steps:
  - name: Run tests with pytest
    run: |
      pip install pytest pytest-html
      pytest --html=report.html --self-contained-html

  - name: Upload test report
    uses: actions/upload-artifact@v4
    with:
      name: test-report
      path: report.html

JUnit Report Action

yaml
steps:
  - name: Publish Test Results
    uses: EnricoMi/publish-unit-test-result-action@v2
    if: always()
    with:
      files: |
        test-results/**/*.xml

Coverage Reports

yaml
steps:
  - name: Upload coverage to Codecov
    uses: codecov/codecov-action@v3
    with:
      file: ./coverage.xml
      flags: unittests
      name: codecov-umbrella
      fail_ci_if_error: true

Quick Reference — Most Used Commands

TaskYAML Snippet
Node.js setupuses: actions/setup-node@v4 + with: { node-version: '18.x', cache: 'npm' }
Python setupuses: actions/setup-python@v4 + with: { python-version: '3.11', cache: 'pip' }
Java setupuses: actions/setup-java@v4 + with: { java-version: '17', distribution: 'temurin', cache: 'maven' }
Node.js installrun: npm ci
Python installrun: pip install -r requirements.txt
Maven buildrun: mvn clean verify
Gradle buildrun: ./gradlew build
Docker builduses: docker/build-push-action@v5 + with: { context: ., push: true, tags: myapp:latest }
Matrix buildstrategy: { matrix: { node-version: [18, 20, 22] } }
Cache dependenciesuses: actions/cache@v3 + with: { path: ~/.npm, key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}}
Upload artifactuses: actions/upload-artifact@v4 + with: { name: build, path: dist/ }

Pro Tips

  1. Use npm ci over npm install — faster and more reliable in CI.

  2. Use pip install -e . — for editable installs with dependencies.

  3. Use mvn clean verify — for Maven builds with tests.

  4. Use ./gradlew build — for Gradle builds (use Gradle wrapper).

  5. Cache dependencies — huge speed improvement.

  6. Use matrix builds — test across multiple versions.

  7. Upload test artifacts — for debugging failures.

  8. Use if: always() — for post-test steps even on failure.

  9. Use Docker Buildx — for cross-platform builds.

  10. Use cache-from and cache-to — for Docker layer caching.


Next in GitHub ActionsDeployment Workflows Pro