Certified Jenkins Engineer
Jenkins Pipelines
Demo Build and Test via Pipeline
In this guide, we’ll extend our Jenkins pipeline by adding Build and Unit Test stages alongside the existing Echo Version step. By the end, you’ll have an automated flow that compiles, packages, and tests your Spring Boot application.
Current Pipeline Status
Below is the current pipeline, which installs Maven and echoes its version:
Application Repository
Our sample Spring Boot “Hello World” application resides in the jenkins-hello-world
repository. It uses Maven to build a JAR and includes six JUnit test cases (five passing, one failing).
Defining the Pipeline
Create or update a Jenkinsfile
at the root of your project with the following content:
pipeline {
agent any
tools {
// Install the Maven version configured as "M398"
maven "M398"
}
stages {
stage('Echo Version') {
steps {
sh 'echo Print Maven Version'
sh 'mvn -version'
}
}
stage('Build') {
steps {
// Clone the repository (defaults to branch "master")
git 'http://139.84.159.194:5555/dasher-org/jenkins-hello-world.git'
// Build without running tests
sh 'mvn clean package -DskipTests=true'
}
}
stage('Unit Test') {
steps {
// Execute JUnit tests
sh 'mvn test'
}
}
}
}
Pipeline Stages Overview
Stage | Purpose | Command |
---|---|---|
Echo Version | Verify Maven installation | mvn -version |
Build | Compile and package the application | mvn clean package -DskipTests=true |
Unit Test | Execute JUnit tests | mvn test |
First Run: Checkout Failure
On the initial run, the pipeline fails at the Build stage because the default branch is main
, not master
:
The console logs display:
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Warning
Ensure your git
step points to the correct branch (main
in this repository) to avoid checkout errors.
Specifying the Correct Branch
Use the Jenkins Snippet Generator to craft a branch-specific git
checkout:
- Select Git.
- Enter your repository URL.
- Set Branch to
main
. - Click Generate Pipeline Script.
Adjust your Jenkinsfile
:
git branch: 'main', url: 'http://139.84.159.194:5555/dasher-org/jenkins-hello-world.git'
Commit and re-run the pipeline. The checkout and build succeed, but one unit test still fails:
[ERROR] Tests run: 6, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.539 s <<< FAILURE!
java.lang.AssertionError:
Expected: a string starting with "Hola"
but: was "Hello, KodeKloud community!"
Fixing the Unit Test
Update the assertion in HelloControllerTests.java
to match the controller’s greeting:
@Test
public void welcome_startsWithExpectedGreeting() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(startsWith("Hello")));
}
Commit the change and trigger the pipeline again. All stages should now pass:
Application Overview
Controller (src/main/java/com/kodekloud/hello_demo/HelloController.java
):
package com.kodekloud.hello_demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
String hello() {
return "Hello, KodeKloud community!";
}
}
Configuration (src/main/resources/application.properties
):
spring.application.name=hello-demo
server.port=6767
Note
For better collaboration, commit your Jenkinsfile
directly into the repository so pipeline changes are tracked alongside your application code.
Next Steps
With this pipeline, you’ve automated:
- Tool installation and version verification
- Source checkout (branch
main
) - Maven clean package (skipping tests)
- JUnit test execution
Consider adding stages for artifact archiving, static code analysis, or deployment.
Links and References
Watch Video
Watch video content