Creating a Jenkins Pipeline for Spring Boot application
- 4.7/5
- 5611
- Jul 20, 2024
Download, install, and run Jenkins
Download the latest stable version of Jenkins (2.361.3 LTS) - Generic Java package (.war).
Run the downloaded .war file with the following commands:
java -jar jenkins.war
It will run Jenkins as a "java" web application.
In order to access the Jenkins app, navigate to http://0.0.0.0:8080/.
An auto created admin "user" and "password" can be found from the output logs as well as from the file located at: /Users/nkchauhan003/.jenkins/secrets/initialAdminPassword.
Click on "Install suggested plugins".
Once plugins are installed, on the next screen, create an admin user. From now on, this username and password will be used to login instead of the default one.
Now we have a fresh installation of Jenkins ready to be used.
Let's create a "pipeline" following a more imperative programming model built with "Groovy".
To start using pipelines, we have to install the Pipeline plugin" that allows composing simple and complex automation.
We can also install the Pipeline Stage View plugin" to see all the stages we've configured.
We also need to install - JUnit plugin - to create and run the pipeline described in the next section.
In order to install a plugin, go to "Dashboard" > "Manage Jenkins" > "Manage Plugins".
Create pipeline for Spring BOOT project
In this demo, we will create a Jenkins pipeline that clones a spring-boot project from Git Hub, builds it, runs several tests, and then deploys the application.
Let's first create a new Jenkins job ("Dashboard" > "Create a job") selecting "Pipeline" as the type, as shown in the image below:
Select "Pipeline" from the "Configuration" menu on the top-left corner of the page.
Select "Pipeline script from SCM" from the dropdown and check "Lightweight Checkout."
Fill in "Repository URL," "Credentials," "Branches to build," and also check the name of the "Script Path." This file name, i.e., "Jenkinsfile," should be the same as the Jenkins file placed in the root folder of the project.
Jenkinsfile
Create a "Jenkinsfile" in the root directory of the Spring Boot application with the below content:
Here, we are all done setting up a Jenkins pipeline for a Spring Boot project. Let's go to "Dashboard" > "Jenkins Demo" and run "Build Now."