Skip to main content
In this hands-on guide, you’ll use a Jenkins Pipeline to build a Spring Boot JAR with Maven, run unit tests, then build and push a Docker image to Docker Hub, tagging it with the Git commit SHA.

Pipeline Stages

Dockerfile

Include this Dockerfile at the repo root to define your container:
This:
  • Starts from the lightweight openjdk:8-jdk-alpine image
  • Exposes port 8080 (Spring Boot default)
  • Adds the JAR built by Maven
  • Runs the JAR on container start

Initial Jenkinsfile

Add a Docker Build & Push stage after Maven build and unit tests:
printenv lists all Jenkins environment variables. We leverage $GIT_COMMIT to tag images uniquely.

Inspecting Images on the Jenkins VM

On the VM where Docker runs, list existing images:
Trigger the pipeline. The Docker Build step succeeds but the Push fails:
Push failure means Jenkins doesn’t have Docker Hub credentials configured.

Configuring Docker Hub Credentials in Jenkins

Ensure the Docker Pipeline plugin is installed:
The image shows the Jenkins Plugin Manager interface with a list of installed plugins, including Docker-related plugins. A search for "docker" is highlighted, and various plugins are listed with options to uninstall.
Then add Docker Hub credentials:
  1. Go to Manage JenkinsManage CredentialsGlobalAdd Credentials
  2. Select Username with password
  3. Enter your Docker Hub username & password
  4. Set an ID (e.g., docker-hub) and save
The image shows a Jenkins interface displaying the "Global credentials (unrestricted)" section, with a credential named "kubeconfig" listed as a secret file.
The image shows a Jenkins interface where a user is adding global credentials, including a username, password, and ID. The browser tabs and taskbar are also visible.

Updated Jenkinsfile

Wrap Docker commands in withDockerRegistry, referencing the credential ID:
An empty url defaults to Docker Hub. Make sure credentialsId matches the ID you created.

Verifying Build & Push

Re-run the pipeline. You should see:
List images again to confirm the new tags:
You’ve now successfully built and pushed your Docker image. Next, integrate deployment to your Kubernetes cluster via the Jenkins Pipeline.

Watch Video

Practice Lab