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
| Step | Purpose | Example |
|---|---|---|
| Checkout | Get the code | actions/checkout@v4 |
| Setup Language | Install runtime | actions/setup-node@v4 |
| Install Dependencies | Install packages | npm ci / pip install |
| Lint | Code quality | npm run lint / flake8 |
| Build | Compile/package | npm run build / mvn compile |
| Test | Run tests | npm test / pytest |
| Upload Artifacts | Save build output | actions/upload-artifact@v4 |
Node.js Workflow
Basic Node.js CI
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
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 testPython Workflow
Basic Python CI
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-umbrellaPython with Poetry
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 pytestJava Workflow
Java with Maven
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
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
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=maxDocker with Multiple Tags
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
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 testInclude/Exclude in Matrix
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-latestComplex Matrix with Additional Variables
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.0Caching Dependencies
Why Cache?
Caching speeds up workflows by reusing previously downloaded dependencies.
Node.js Cache (npm)
steps:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18.x'
cache: 'npm'
- name: Install dependencies
run: npm ciPython Cache (pip)
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.txtCustom Cache with actions/cache
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 ciMultiple Cache Keys
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
steps:
- name: Run tests
run: npm test
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: test-results
path: test-results/*.xmlPytest Reports
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.htmlJUnit Report Action
steps:
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: |
test-results/**/*.xmlCoverage Reports
steps:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: trueQuick Reference — Most Used Commands
| Task | YAML Snippet |
|---|---|
| Node.js setup | uses: actions/setup-node@v4 + with: { node-version: '18.x', cache: 'npm' } |
| Python setup | uses: actions/setup-python@v4 + with: { python-version: '3.11', cache: 'pip' } |
| Java setup | uses: actions/setup-java@v4 + with: { java-version: '17', distribution: 'temurin', cache: 'maven' } |
| Node.js install | run: npm ci |
| Python install | run: pip install -r requirements.txt |
| Maven build | run: mvn clean verify |
| Gradle build | run: ./gradlew build |
| Docker build | uses: docker/build-push-action@v5 + with: { context: ., push: true, tags: myapp:latest } |
| Matrix build | strategy: { matrix: { node-version: [18, 20, 22] } } |
| Cache dependencies | uses: actions/cache@v3 + with: { path: ~/.npm, key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}} |
| Upload artifact | uses: actions/upload-artifact@v4 + with: { name: build, path: dist/ } |
Pro Tips
-
Use
npm ciovernpm install— faster and more reliable in CI. -
Use
pip install -e .— for editable installs with dependencies. -
Use
mvn clean verify— for Maven builds with tests. -
Use
./gradlew build— for Gradle builds (use Gradle wrapper). -
Cache dependencies — huge speed improvement.
-
Use matrix builds — test across multiple versions.
-
Upload test artifacts — for debugging failures.
-
Use
if: always()— for post-test steps even on failure. -
Use Docker Buildx — for cross-platform builds.
-
Use
cache-fromandcache-to— for Docker layer caching.