Sub topic 13
Automating build, test, and release in CI/CD
This chapter assumes you can build a Spring Boot application with Maven or Gradle, run the resulting JAR, and build a container image from it. It covers how to automate those steps so every change is built, tested, and published the same way.
What a pipeline does
A CI/CD pipeline is a script that a build server runs on each push. CI (continuous integration) means building and testing every change. CD (continuous delivery or deployment) means publishing a deployable artifact, and sometimes deploying it. The main rule is to build the artifact once, then promote that same artifact through each environment. Configuration stays outside the artifact, so the image tested in staging is the image that runs in production.
The stages
- Checkout. The runner fetches the source at the commit being built.
- Build and test. The build tool compiles the code, runs unit and integration tests, and packages the JAR. A failing test stops the pipeline.
- Build the image. Use the Dockerfile or the buildpack command from earlier chapters.
- Publish. Push the image to a registry under a tag that identifies the build.
- Deploy. Update the target environment, for example by changing the image tag in a Kubernetes manifest.
A sample pipeline
This GitHub Actions workflow runs the first four stages. Any CI system follows the same shape.
name: ci
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
cache: maven
- name: Build and test
run: ./mvnw --batch-mode clean verify
- name: Log in to registry
if: github.ref == 'refs/heads/main'
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
- name: Build and push image
if: github.ref == 'refs/heads/main'
run: |
IMAGE=ghcr.io/${{ github.repository_owner }}/myapp:${{ github.sha }}
./mvnw --batch-mode spring-boot:build-image -Dspring-boot.build-image.imageName=$IMAGE
docker push $IMAGEPoints to notice:
- Pull requests run the build and tests only. The image is published only from
main. ./mvnwuses the project's wrapper, so the Maven version matches your laptop.cache: mavenreuses downloaded dependencies between runs.--batch-moderemoves interactive output that clutters CI logs.- The registry token comes from the CI secret store, never from the repository.
Tagging and versions
Tag each image with the commit SHA. It is unique and traces an image back to its source. You can add a human-friendly tag such as 1.4.0 when you cut a release. Avoid deploying latest, because you cannot tell which build it points to, and rollbacks become guesswork.
Common errors
| Symptom | Likely cause |
|---|---|
| Tests pass locally but fail in CI | Tests depend on local services or environment variables. Start dependencies in the pipeline, for example with Testcontainers, and set required variables explicitly. |
| Push to registry is denied | The token lacks write permission, or the login step was skipped. |
| Build is slow on every run | Dependency caching is not configured. |
| Different behavior between stages | The image was rebuilt per environment. Build once and promote it. |
Exercise
Your pipeline builds a new image for staging and then builds another one for production from the same commit. Why is this a problem, and what should you do instead?
Show answer
Two builds from the same commit can differ, for example if a dependency version or base image changed between them, so production would run something that was never tested. Build the image once, tag it with the commit SHA, and promote that exact image to each environment, changing only the externalized configuration.
Exercise
In the sample workflow, which steps run for a pull request, and which are limited to the main branch?
Show answer
Checkout, Java setup, and "Build and test" run for pull requests. The registry login and the image build and push have if: github.ref == 'refs/heads/main', so they run only on pushes to main.