Concept Explanation
CI means every push is automatically built and tested, so breakage is caught in minutes rather than at release time. CD means a passing build can be deployed automatically.
The value is not automation for its own sake — it is that main is always known-good.
A GitHub Actions Pipeline
.github/workflows/ci.yml:
name: CI
on:
push:
branches: [ main ]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: maven # cache ~/.m2 between runs
- name: Build and test
run: ./mvnw -B verify
- name: Upload test report
if: always() # upload even when tests failed
uses: actions/upload-artifact@v4
with:
name: test-results
path: target/surefire-reports/
The if: always() matters — the report is most useful precisely when the build failed.
Adding a Deploy Stage
deploy:
needs: build # only if tests passed
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Log in to registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
Tagging with the commit SHA gives every image a precise, immutable identity — you always know exactly what is deployed and what to roll back to.
Quality Gates
Fail the build on things humans forget to check:
- name: Coverage gate
run: ./mvnw jacoco:check # fails below the configured threshold
- name: Dependency vulnerabilities
run: ./mvnw dependency-check:check
- name: Formatting
run: ./mvnw spotless:check
A rule enforced by CI is a rule; a rule in a wiki page is a suggestion.
Deployment Strategies
| Strategy | How it works | Trade-off |
|---|---|---|
| Recreate | stop old, start new | brief downtime |
| Rolling | replace instances gradually | two versions run at once |
| Blue-green | switch traffic between two full environments | instant rollback, double cost |
| Canary | 5% of traffic to the new version first | safest, most complex |
Rolling and canary both require your app to tolerate two versions running simultaneously — which means database migrations must be backward-compatible.
Common Mistakes
- Secrets in the workflow file: Use the repository secret store. A committed token is a compromised token.
- Skipping tests to make the build green: The pipeline's only job is telling the truth about the code.
- No dependency caching: Re-downloading Maven dependencies on every run wastes minutes per build.
- Deploying from a laptop: Unreproducible, unauditable, and it works only while that laptop exists.
- Destructive migrations during a rolling deploy: Dropping a column the old version still reads causes errors mid-rollout.
Best Practices
- Fast feedback: Aim for a build under ten minutes, or people stop waiting for it.
- Tag images with the commit SHA, never only
latest. - Require a green build to merge via branch protection.
- Make migrations additive: add a column, deploy, backfill, then remove the old one in a later release.
- Keep rollback one command away and test it before you need it.
Practice Exercises
- The First Pipeline: Add a workflow that runs
./mvnw verifyon every pull request. Break a test and confirm the check fails. - The Cached Build: Enable Maven caching and compare run times before and after.
- The SHA Tag: Push a Docker image tagged with the commit SHA and explain how you would roll back to the previous one.