CI/CD Pipeline Using Docker and Jenkins


By this time, we all know what Cloud, Digital Transformation and deployable toolchain. But, the main question is, what controls them without the hassle of manually deploying the code to the cloud every time? Is there any way through which a developer can deploy his code automatically to a cloud be it public, private or enterprise architecture for the users to use without wasting any time?

What is CI/CD and why it is so much needed?

Modern software and cloud applications are much more demanding. For a developer to deliver his code to the users and generate revenue is the ultimatum. Thus, CI/CD comes to the rescue. CI/CD revolves around the concept of one being able to write and deploy codes for production, simultaneously. CI/CD or Continuous Integration and Continuous Deployment helps you to generate more revenue in an enterprise application production, by making the code writing and deployment process automatic and effortless. From building enterprise level applications to capital ventures, use of CI/CD always adds more value to your organization.

Using CI/CD pipeline with Docker and Jenkins:

The CI or Continuous Integration part makes sure that a developer is able to send all of his code into the same place and is able to run that using the same process, which not only makes it more secure and easy, but also makes it easy to release a new version of your application. This happens with the concept of being able to commit your code into a single repository.

CD or Continuous Deployment is the part where one can deploy the new codes safely and quickly making sure all the functionalities of the new code are intact. So, we arrive at building a CI/CD pipeline.

Please follow the below steps very carefully, so that we can arrive at our desired result. After following these steps, you will be able to code and deploy your applications with automated accuracy.

Some prerequisites that are to be arranged:

  1. Git Repository with your application code to be deployed should be available. If you don’t have your code, you can get the sample application code from the following git repo:- https://github.com/LovesCloud/java-groovy-docker/
    Feel free to fork the repo in your GitHub account.
  2. Configure GitHub Webhook for Jenkins on your forked repo. Follow the steps mentioned in Configure GitHub Webhook for Jenkins
  3. To publish Docker image on DockerHub, Follow the settings mentioned in section Configuring Jenkins for publishing Docker image into DockerHub .
  4. You need to have the Public IP of the Ubuntu Deployment Server with username and password authentication and Docker installed in it, where you want to deploy your application.
  5. To run Docker image on Deployment Server, Follow the settings mentioned in section Configuring Jenkins for running Docker image on Deployment Server .

Steps to be followed at the GitHub end:

  1. If you are using your own application code git repo, Create a file named Jenkinsfile a in the root directory and copy the code from our Jenkinsfile and do the following modification as mentioned below.And if you are using our sample code and forked this repo, Open Jenkinsfile in your root directory of your repo, click the ‘pencil’ icon to edit it.
    In line,
    def dockerImageName= ‘rajnikhattarrsinha/javademoapp_$JOB_NAME: $BUILD_NUMBER’
    Replace rajnikhattarrsinha with <your dockerhub username>.
  2. In SCM Checkout stage, Replace the https://github.com/LovesCloud/java-groovy-docker/ with URL of your forked repo.
  3. In stage, Publish Docker Image,
    1. Replace “dockerpwd” with “dockerpwd<yourname>
    2. In line. sh “docker login -u rajnikhattarrsinha -p ${dockerPWD}” , Replace “rajnikhattarrsinha” with <your dockerhub username>
  4. In stage, Run Docker Image, Replace IP with your Deployment Server Public IP at all places and commit the changes.
  5. In stage Run Docker Image, replace devops with your ubuntu server user at all places.
  6. Open xml from root directory, click the ‘pencil’ icon to edit it,
    Replace line <finalName>demopipeline_rajni</finalName> with <finalName>demopipeline_<yourname></finalName> and commit the changes.

Steps to follow at Jenkins end:

  1. Open your Jenkins.
  2. Click New Item link on left panel
  3. Enter an Item name like <yourname>_pipeline

Note: PLEASE DO NOT INCLUDE CAPITAL LETTER IN THE NAME IN STEP-8

  1. Select Pipeline
  2. Click OK
  3. Select General Tab, GitHub Project as <repo to be deployed>
  4. Under Build Triggers section, Select GitHub hook trigger for GITScm polling
  5. Under Pipeline section, Select Definition as “Pipeline script from SCM”
  6. Select SCM as Git
  7. Enter Repository URL as <repo to be used>
  8. Keep Script Path as “Jenkinsfile”
  9. Click Save
  10. On Left Panel, Click on Build now
  11. View the Pipeline being built and showing Stages.
  12. Once all stages are successfully completed. Click on the latest triggered build number from left panel.
  13. View Console Output.

Verifying the Deployed application:

  1. Take the Public IP of your Deployment Server.
  2. Open any browser
  3. Type http://<Public IP of Deployment Server IP>:8082/<file name of the war given in pom.xml>
  4. Hit Enter;

Steps to follow for making automated CICD pipeline:

  1. On GitHub end, edit pom.xml, Change the name of the war file name again of your choice and Commit the changes.
  2. Open the Jenkins end, Open the Job Details page and observe that the build is triggered automatically as soon as you committed the file in GitHub in your repo.
  3. View the Pipeline being built and showing Stages.
  4. Once all stages are successfully completed. Click on the latest triggered build number from left panel.
  5. View the Console Output

The complete log of the steps involved in successful building of job are displayed.

  1. In order to view the latest deployment, repeat the steps mentioned in section Verifying the deployed application above and use the war file name you gave in step 23.

Configuring Jenkins for publishing Docker image into DockerHub

  1. Navigate to Credentials in Jenkins opened in browser
  2. Click “global” link in section Stores scoped to Jenkins
  3. Click Add Credentials link
  4. Select Kind= Secret text
  5. Enter Secret =<Type the docker hub password>

Note: If your dockerhub password has $ character in it, please prefix it with \ (backward slash)

  1. Enter ID as “dockerpwd<yourname>”
    Note: please keep value as mentioned. This is used in pipeline script.
  2. Enter Description as “dockerpwd<yourname>”
  3. Click OK

Configuring Jenkins for running Docker image on Deployment Server

  1. Navigate to Credentials in Jenkins opened in browser
  2. Click “global” link in section Stores scoped to Jenkins
  3. Click Add Credentials link
  4. Select Kind= Secret text
  5. Enter Secret =<Type the deployment server password>

Note: If your deployment server password has $ character in it, please prefix it with \ (backward slash)

  1. Enter ID as deploymentserverpwd
    Note: please keep value as mentioned. This is used in pipeline script.
  2. Enter Description as deploymentserverpwd
  3. Click OK

Configure GitHub Webhook for Jenkins

  1. Open GitHub
  2. Navigate to Git Repo;
  3. Navigate to Settings of repository
  4. Click Webhook
  5. Click Add Webhook
  6. Enter Payload URL-http://<Public IP of Jenkins Server>:8080/github-webhook/
  7. Click Save Webhook

At Loves, we have gained expertise in building CI/CD pipeline that enables our clients to code and deploy their applications with accuracy. To bring about digital transformation in your organization using new-age software tools and boost your revenue generation in an enterprise application production, reach out to biz@loves.cloud.