This repository contains a Jenkins pipeline (see the jenkinsfile) that builds, tests, analyzes, and packages the Petclinic sample application, and then builds a Docker image.
Reference: jenkinsfile
The declarative Jenkins pipeline performs the following high-level steps:
- Checkout source from the Petclinic Git repository (branch:
main) - Compile code with Maven
- Run unit tests (including an additional JaCoCo test run that excludes certain packages)
- Run SonarQube analysis via
sonar-scanner - Run OWASP Dependency Check and publish the report
- Build the Maven artifact (
mvn clean install) - Verify Docker access and build a Docker image
- Git Checkout
- Code Compile (
mvn clean compile) - Unit Tests (
mvn test) - Test with JaCoCo (
mvn test -Djacoco.excludes=org/hibernate/proxy/**) - SonarQube Analysis (via configured SonarQube server and
sonar-scanner) - OWASP Dependency Check (publish
dependency-check-report.xml) - Artifact Build (
mvn clean install) - Check Docker (verify docker socket and permissions)
- Docker Build (build image using
docker build -t image1 .)
Make sure your Jenkins master/agent(s) have the following configured:
-
Tools (configured in Jenkins -> Global Tool Configuration)
- JDK with the label
jdk17 - Maven with the label
maven3 - SonarScanner installation labeled
sonar-scanner - (Optional) Docker installation entry (toolName used in pipeline is
docker)
- JDK with the label
-
Jenkins plugins
- Pipeline
- Pipeline: Groovy
- SonarQube Scanner for Jenkins
- OWASP Dependency-Check Plugin (dependency-check)
- Docker Pipeline
- Any Git plugin you use (usually Git plugin)
-
SonarQube server configured in Jenkins Global Configuration with the name
sonar-server(used inwithSonarQubeEnv('sonar-server')) -
OWASP Dependency-Check installation configured in Jenkins with the name
DP-Check(used asodcInstallation) -
Credentials / IDs
- A credentials entry for Docker registry used in
withDockerRegistry(credentialsId: '16bda58e-7358-4', ...)— replace with your own credentials ID - If your SonarQube server requires authentication you may also need to configure a token/credential and set it up in Jenkins/Sonar configuration
- A credentials entry for Docker registry used in
SCANNER_HOME— set from the installed toolsonar-scanner:- The pipeline sets
SCANNER_HOME = tool 'sonar-scanner'and then runs$SCANNER_HOME/bin/sonar-scanner ...
- The pipeline sets
- Git checkout: the pipeline currently pulls from
https://github.com/jaiswaladi246/Petclinic.git(branchmain). Update this URL to point to your repo if required. - SonarQube:
- The pipeline calls sonar-scanner with:
-Dsonar.projectName=Petclinic-Dsonar.projectKey=Petclinic-Dsonar.java.binaries=.-Dsonar.host.url=http://172.17.0.3:9000
- Update
sonar.host.url,sonar.projectKey, andsonar.projectNameas needed for your environment.
- The pipeline calls sonar-scanner with:
- JaCoCo:
- The pipeline runs a second test invocation with
-Djacoco.excludes=org/hibernate/proxy/**. Adjust excludes as needed.
- The pipeline runs a second test invocation with
- OWASP Dependency Check:
- The pipeline runs
dependencyCheckand publishes results withdependencyCheckPublisherlooking for**/dependency-check-report.xml. Ensure this path matches where the plugin writes reports in your job workspace.
- The pipeline runs
- Docker:
- The pipeline checks for Docker access by running
docker psand verifying/var/run/docker.sock. Ensure the Jenkins agent has Docker installed and the executing user can access the Docker socket or configure a dedicated Docker agent. - The Docker build command tags the image as
image1. Update the tag to your desired repository/name and push step if needed.
- The pipeline checks for Docker access by running
- Credentials and IDs in the pipeline (example placeholders):
sonar-server— SonarQube server config name in JenkinsDP-Check— OWASP Dependency-Check installation name16bda58e-7358-4— Docker registry credentials ID (replace with your own)
- Create a new Pipeline job in Jenkins.
- Add / configure the required global tools and plugin settings listed above.
- In the Pipeline job:
- Option A: Use "Pipeline script from SCM" and point to the repo that contains this
jenkinsfile. - Option B: Paste the pipeline script content from the jenkinsfile into the job's Pipeline script field.
- Option A: Use "Pipeline script from SCM" and point to the repo that contains this
- Make sure the agent you run on has Java, Maven, Docker (if building images) and access to execute the Sonar scanner and dependency-check tool if those stages are required.
- Run the job and review console output for each stage. Reports from SonarQube and OWASP Dependency-Check will be available per their plugin/configuration.
- Update the Git repository URL and branch if you're working with a fork or different source.
- Replace IP
172.17.0.3:9000with your SonarQube server's URL or a configured server in Jenkins. - Change Docker image name/tag and add a push step to publish to a registry (e.g., Docker Hub or a private registry).
- Add post-build actions, artifact archives, or promotion steps as your delivery process requires.
- Sonar analysis fails: verify
SCANNER_HOMEpoints to a valid sonar-scanner installation and thatsonar-serveris configured in Jenkins with correct URL and authentication. - OWASP Dependency-Check report missing: check plugin logs, ensure DP-Check is installed and that the report path
**/dependency-check-report.xmlmatches the generated report path. - Docker permission errors: ensure the Jenkins agent user is in the
dockergroup or use a dedicated Docker agent/container with proper access. - Maven build/test failures: run the same Maven commands locally to reproduce issues:
mvn clean compile,mvn test, andmvn clean install.
This repository contains CI/CD configuration and should follow the licensing of the source application (Petclinic). Add a LICENSE file as appropriate for your project.