In this mini project, we will create a jenkins pipeline by getting a SpringBoot Application code directly from Github and then create a Docker Image of it with the help of Jenkins. After Jenkins will push this Image to docker Hub and test our App by launching a Dcoker container of this Application.
All we need to accomplish this project are:
- A springBoot Application availbale at: https://github.com/Tcarters/SpringBootApp_and_DevOps
- A Jenkins Server (Mine is running in a local virtual machine at 192.168.38.90:8082 )
- A Docker service installed locally and running and a DockerHub Account (Mine is at https://hub.docker.com/search?q=tcdocker2021 ) .
-
Install the Maven Integration plugin because we will need it for pipeline Integration in upcoming steps.
- Create a new Item or Job as :
- 1.1 Give a name to the new Job, here I use:
springBt-docker-jenkins
- 1.2 Then, select the type as a
pipeline
- 1.3 After click Ok to save our choice.
- 1.1 Give a name to the new Job, here I use:
-
Configuration of the new Job:
-
On the
General
section, give a description and a GitHub repo like showing below: -
On the next section
Build Triggers
, select option "GitHub hook trigger for GITScm polling" -
Following the next section , we configure our pipeline stages. Feel free to use a jenkinsfile by pulling it from your gitHub repo or directly writing the script.
-
And after the pipeline Configured, we save our choice and go on Dashboard to build it.
- We can see that our build successed which means we have done with the first part of the project which is the Integration of Jenkins with our gitHub repoand setting up a pipeline.
-
Basically to build the docker Image, we all know that we need a Docker Image isnt ? 🤡 Okay so we have now to create a
Dockerfile
in our App repo and with help of Jenkins we'll generate a DockerImage ..Let's do it now 🚴
-
2.1 First create a file named
Dockerfile
in the main directory of src Spring App >> Refer to this link, to understand https://github.com/Tcarters/SpringBootApp_and_DevOps -
2.2 In the same folder run
mvn install
to update thepom.xml
file of the Spring application. After commit and push it to your git Repo. -
2.3 Now before proceeding with jenkins, let's start the Docker service if it's not running .
-
2.4 Now we have to modify our pipeline configuration to integrate another stage which will create a docker image.
- The new stage added in the pipeline script :
stage ('Build SpringApp Docker Image ') { steps{ script { echo 'Checking if docker service is available ...' sh 'systemctl is-active --quiet docker && echo "Service is running ..."' echo 'Starting Docker Image building' sh 'docker build -t tcdocker2021/springbt-in-docker:latest .' } } }
Now that Jenkins build the Docker Image for our application, we'll tell him to push the Image to our public docker Hub registry... And to accomplish it we have just to modify again our pipeline script and That's it.
-
3.1 Modifying the pipeline script by adding a new stage
- The stage added is:
stage ('Pushing the SpringApp Docker Image to Docker Registry') { steps { script { echo 'Logging to Docker registry.....' withCredentials([string(credentialsId: 'mytcdocker-hub', variable: 'mydockerhubpwd')]) { sh 'docker login -u tcdocker2021 -p ${mydockerhubpwd}' // some block } echo 'Starting the push of Docker Image ....' sh ' docker push tcdocker2021/springbt-in-docker:latest ' } } }
-
3.2 Save our changes and
Build
again the pipeline we got : -
3.3 Checking our Docker Hub for verification :
So till now we can see our job is almost done but why not tell Jenkins to test our application ? So that will be our next travel ....
-
Here we are 🙂, To test our Application we will start a container based on the Image created earlier in a new state of our pipeline.
-
The new stage is :
stage ('Testing the deployment') {
steps {
script {
echo 'Starting a local container of the App ....'
sh 'docker run -dit --name springapp -p 2000:8080 tcdocker2021/springbt-in-docker:latest '
echo 'The App is now available at Port 2000 ....'
}
}
}
-
After saving and building again we have a new stage with a container launched for testing .
-
On a local Browser, we can see our SpringApp available ...
- To do it we have to create a file
Jenkinsfile
and put our pipeline code inside it. - Content of
Jenkinsfile
:
pipeline {
agent any
tools {
maven 'maven_3.6.3'
}
stages {
stage('Build a Maven Project '){
steps{
echo 'Start The checking of github repository'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/Tcarters/SpringBootApp_and_DevOps']])
echo 'Cleaning the Project'
sh 'mvn clean install'
}
}
stage ('Build SpringApp Docker Image ') {
steps{
script {
echo 'Checking if docker service is available ...'
sh 'systemctl is-active --quiet docker && echo "Service is running ..."'
echo 'Starting Docker Image building'
sh 'docker build -t tcdocker2021/springbt-in-docker:latest .'
}
}
}
stage ('Pushing the SpringApp Docker Image to Docker Registry') {
steps {
script {
echo 'Logging to Docker registry.....'
withCredentials([string(credentialsId: 'mytcdocker-hub', variable: 'mydockerhubpwd')]) {
sh 'docker login -u tcdocker2021 -p ${mydockerhubpwd}' // some block
}
echo 'Starting the push of Docker Image ....'
sh ' docker push tcdocker2021/springbt-in-docker:latest '
}
}
}
stage ('Testing the deployment') {
steps {
script {
echo 'Starting a local container of the App ....'
sh 'docker run -dit --name springapp -p 2000:8080 tcdocker2021/springbt-in-docker:latest '
echo 'The App is now available at Port 2000 ....'
}
}
}
}
}
-
And that summarize our pipeline code definition ...
-
To test it, let's create another pipeline named
TestJenkinsfile
like and choose the pipeline method as Git with configuration like: -
After saved and build the configuration, we can see the success of App pushed, deployed and launched ...
-
Verification on our browser, we have the App launched by jenkins
- On the Jenkins pipeline View Dashboard , we can see :