Skip to content

In this mini project, we will use Jenkins features to create a Docker Image for a SpringBoot Application and then push it to DockerHub. All in a jenkins CI/CD pipeline configured.

Notifications You must be signed in to change notification settings

Tcarters/mini-DevOps-Project_jenkins-springBoot-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

A simple Mini DevOps Project:

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.

cover

Prerequisites:

All we need to accomplish this project are:

Step 1: Launch The Jenkins server and Configure the Pipeline

  • Start by launching the Jenkins server

  • 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.

s1-1pic

  • 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.

      • Before That configure your maven installation in Jenkins settings :

        In my case maven 3.6.3 is instatlled, and it can be accessed at: Dashboard > Manage Jenkins > Global Tool Configuration

        s1-4mvnConf

      • The pipeline script defined is : s1-5pipConf

    • And after the pipeline Configured, we save our choice and go on Dashboard to build it.

    s1-6buildPip

    • 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.

Step2: Tell jenkins to build the Docker Image for our Spring App

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 the pom.xml file of the Spring application. After commit and push it to your git Repo. s2-1push

  • 2.3 Now before proceeding with jenkins, let's start the Docker service if it's not running .

    As i am on linux machine, run : systemctl start docker s1

  • 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 .'
                }
            }
        }
    
    • 📛📛 Important Configuration before executing the pipeline isrequired : we have to give permission to Jenkins to run docker service , e.g Error is out cause of that s2-1builderror

    • And after Restart the Jenkins service with sudo systemctl restart jenkins

    • Now build the pipeline, after permission updated to Jenkins:

Step 3: Pushing the Docker Image with Jenkins help

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 '
                }
            }
        }
    
    • Before the push, we can see that we haven't a springApp pushed in our dockerhub registry. s3-0hub
  • 3.2 Save our changes and Build again the pipeline we got :

    • On our Pipeline Dashboard View:
      s3-1hub

    • By checking the log of our pipeline executed, we can see that everything go well.. 😀 s3-2log

  • 3.3 Checking our Docker Hub for verification : s3-4hub

    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 ....

Step 4: Testing our App by Launching a Docker container with Jenkins

  • 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 . s4-dashres

  • Global View

  • On a local Browser, we can see our SpringApp available ...

BONUS

Creating a Jenkinsfile for our pipeline instead of editing the script many times ..

  • 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:

    • sbonus-1

    • sbonus-2

  • After saved and build the configuration, we can see the success of App pushed, deployed and launched ...

    • sbonus-res
  • Verification on our browser, we have the App launched by jenkins

sbonus-dockerOutput

- On the Jenkins pipeline View Dashboard , we can see :

Waaaoooo, Our project is done 🤸 ... and Time to take a Coffee 💥💥 !!! HAPPY LEARNING !!!!

About

In this mini project, we will use Jenkins features to create a Docker Image for a SpringBoot Application and then push it to DockerHub. All in a jenkins CI/CD pipeline configured.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published