Video in progress

Which stages would you put in a CI pipeline for a Java service? Give an example.

At minimum: build and unit tests, integration tests, static analysis, scanning dependencies and the image for vulnerabilities, and pushing a versioned image to the registry. Each stage should fail fast, with the expensive stages at the end.

📖 Answer

Using GitHub Actions as an example:

name: ci
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with: { distribution: temurin, java-version: '21', cache: maven }
      - run: ./mvnw -B verify          # unit + integration tests
      - name: Image
        if: github.ref == 'refs/heads/main'
        run: |
          docker build -t registry.example.uz/shop-api:${GITHUB_SHA::7} .
          docker push registry.example.uz/shop-api:${GITHUB_SHA::7}

A real project usually adds more:

  • integration tests with Testcontainers, against a real Postgres;
  • an image scanner such as Trivy or Grype, and OWASP dependency-check;
  • coverage and static analysis (Sonar, SpotBugs), though you should not blindly chase the coverage percentage;
  • tagging the image with the commit SHA, so you know exactly which code is in prod.

One thing: if the pipeline takes 25 minutes, people find ways around it. A Maven cache, parallel jobs and splitting the tests properly are often more important than an extra check.