Declarative Jenkins Pipeline for Spring Boot
Fast recall
Quick revision
Core answer
Whether you understand CI/CD pipeline design and can write production-grade Jenkinsfiles
Hardcoding credentials in the Jenkinsfile instead of using withCredentials block
Mention post blocks for cleanup, stash/unstash for artifacts, and parallel stages for speed
Interview-ready guide
Interview answer
A Jenkins declarative pipeline is a Jenkinsfile written in the declarative syntax — a structured DSL that defines stages, steps, agents, and post-conditions with explicit, readable structure. For a Spring Boot project, the pipeline typically covers checkout, build, test, analysis, Docker image creation, and deployment.
Pipeline structure
- The
pipeline {}block is the top-level wrapper;agent { docker { image 'eclipse-temurin:21' } }runs every stage inside a JDK container — no Java installation required on the Jenkins agent itself, and different pipelines can use different JDK versions simultaneously stagescontains an ordered list ofstageblocks; each stage groups related steps and is visible as a named column in Jenkins' Stage View — failed stages are immediately visible without reading the full logenvironmentat the pipeline level defines variables available in every stage —DOCKER_REGISTRY = 'my-registry.io'used in build and push stages without repetition
Testing and quality gates
- The test stage runs
mvn verifyrather thanmvn test—verifytriggers Surefire for unit tests and Failsafe for integration tests and JaCoCo coverage;mvn testskips integration tests junit 'target/surefire-reports/*.xml'in thepost { always {} }block publishes test results to Jenkins even when the build fails — test reports should be collected regardless of outcome so failures are visible without reading log output- Add
jacoco(execPattern: 'target/jacoco.exec', minimumLineCoverage: '80')to fail the build when coverage drops below the threshold; this requires the JaCoCo Jenkins plugin
Deployment and credentials
- Use
withCredentials([usernamePassword(credentialsId: 'docker-registry', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')])to inject registry credentials into the Docker push step — credentials never appear in the Jenkinsfile or the build log withKubeConfig([credentialsId: 'kubeconfig'])injects a kubeconfig forkubectl applyin the deploy stage — the kubeconfig lives in Jenkins Credentials, not committed to the repository
Declarative Jenkins Pipeline for Spring Boot
Direct Answer
A declarative Jenkins pipeline uses a Jenkinsfile with a structured pipeline {} block containing stages for build, test, and deploy. You define agent, environment variables, and sequential stages that run Maven or Gradle commands, run tests, build a Docker image, and push to a registry.
Common Mistakes & Pitfalls
Hardcoding credentials in the Jenkinsfile instead of using withCredentials block
Check your understanding
Answer these without looking back at the article.
- How do you handle Jenkins pipeline failures and send Slack notifications?
- What is a Jenkins shared library and when would you use one?
- How do you run parallel stages in Jenkins to speed up the pipeline?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.