CICD Pipeline using SpringBoot Jenkins Ansible

Vivek Singh
4 min readApr 1, 2022

--

Git Code available

In this article, I will walk you through the steps needed to create a CICD pipeline. This pipeline will have multiple stages :

  1. Source code checkout
  2. Unit Test
  3. Integration Test
  4. SonarQube Analysis
  5. Build Jars
  6. Run Spring Boot Application.

Screenshot for the pipeline stages :

We have a very basic SpringBoot application with a controller, a test case and an integration test case. Our main focus has been on using Jenkins and Ansible together.

Jenkins is used to manage the CICD process. Jenkins will build, test and create jar for the repository.

You should have your Sonarqube and Jenkins running on local to run the code.

We will use Ansible playbook to deploy and start the spring boot application. For this article we are doing everything in local. However with Ansible you can run the jar file in your server. Check my blog on on how to install Ansible on Jenkins.
I have integrated Ansible with Jenkins which means Jenkins uses Ansible as s plugin. So we do not need to manage a server for Ansible to work. Ansible Playbook has made this process very convenient.

Ansible Playbooks offer a repeatable, re-usable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications. If you need to execute a task with Ansible more than once, write a playbook and put it under source control.

For this CICD pipeline we are using 2 files related to Ansible :

  1. ansible.yml which has the server details where we need to deploy and run the application. Since in our case, we are working locally, so my server will point to either localhost or local IP.
  2. dev.inv is the file where we write the command to deploy and run the springboot application. In our case, as we are working locally, so we do not need to deploy. We just need to run the springboot application in our local server.

I have also added steps in the Jenkins pipeline to trigger emails with appropriate reasons whenever the pipeline fails.

Link to my article on how to integrate mail with Jenkins

Check my blog on running separate unit test and integration test for your pipeline.

Jenkins File content :

pipeline {
agent any
tools {
maven 'mvn'
//version 3.0.5
}
parameters {
choice(
name: 'envSelected',
choices: ['dev', 'test', 'prod'],
description: 'Please choose en environment where you want to run?'
)
}
stages {
stage('unit test') {
steps {
echo "Environment selected: ${params.envSelected}"
sh 'mvn test -Punit-tests'
}
post {
failure {
mail to: 'vivek.sinless@gmail.com',
subject: 'Dude your Azuga-RUC Pipeline failed. Check your Unit Tests',
body: 'Unit Test Cases Failure'
}
}
}
stage('integration test') {
steps {
echo "Environment selected: ${params.envSelected}"
sh 'mvn test -Pintegration-tests'
}
post {
failure {
mail to: 'vivek.sinless@gmail.com',
subject: 'Dude your Azuga-RUC Pipeline failed. Check your integration tests',
body: 'Integration Test Cases Failure'
}
}
}
stage('SonarQube Analysis') {
steps {
//def mvn = tool 'mvn';
withSonarQubeEnv('sonar') {
sh "mvn sonar:sonar"
}
// timeout(time: 4, unit: 'MINUTES') {
// waitForQualityGate abortPipeline: true
// }
}
}
stage('Build Jars') {
steps {
sh 'mvn clean package'
}
}
stage('Run Spring Boot App') {
steps {
script {
if (env.envSelected == "dev" || env.envSelected == "test") {
echo 'triggered by dev or test'
ansiblePlaybook installation: 'ansible2', inventory: 'dev.inv', playbook: 'ansible.yml', disableHostKeyChecking: true
} else {
echo 'triggered by prod'
input "Continue Deployment to Prod ? Are you Sure ?"
ansiblePlaybook installation: 'ansible2', inventory: 'dev.inv', playbook: 'ansible.yml', disableHostKeyChecking: true
// check below code for IP ssh based deployment
// for different Ips
// IP address and role goes in dev.inv
/**[webservers]
IP-address ansible_user=ec2-user
**/
// command changes to include crendeitalsId
// private-key values if your jenkins configured key to connect to server IP
// check the screenshot you have
// ansiblePlaybook crendeitalsId: 'private-key', installation: 'ansible2', inventory: 'dev.inv', playbook: 'ansible.yml', disableHostKeyChecking: true
}
}
}
}
}
}

Once you click on Jenkins build, there are 3 options you can select from. This option is mainly to select the environment you want to deploy in :

For production, there’s a confirmation popup shown before actually starting the application on prod. The user has to manually select ‘Proceed’. This is for safety purpose so that we don’t trigger a prod deployment by mistake.

Below is the Ansible plugin usage in Jenkins file (jenkins file has this command) :

ansiblePlaybook installation: 'ansible2', inventory: 'dev.inv', playbook: 'ansible.yml',

The above command will use file dev.inv to get the IP address of the server. ansible.yml file has the command to start/run springboot application on the server.

That’s it. You can check the source code and readme file to get better understanding : Git Code

Please reach out to me for any queries or freelancing work. LinkedIn . Or email me at vivek.sinless@gmail.com

Happy Learning! Cheers.

--

--

Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless