Day 27: 90DaysOfChallenge

Day 27: 90DaysOfChallenge

Jenkins Declarative Pipeline with Docker

We made a simple "Hello World" example with declarative pipeline in Jenkins on our previous day. Let's integrate Docker with Jenkins declarative pipeline.

Docker Build and Docker Run

docker build - We can use sh 'docker build . -t <tag>' in the pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

docker run: We can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

git clone: We can use git clone: 'url', branch:master in the pipeline stage to take a pull from Git Hub.

Task - 01

  • Create a docker-integrated Jenkins declarative pipeline

  • Use the above-given syntax using sh inside the stage block

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2.

  1. Login to Jenkins server with your username and password.

  2. Click on "New Item" from Dashboard. Enter the project name in "Enter an Item name" field, select Pipeline and click OK.

  3. Enter the Description of the Project and select GitHub Project.

  4. Select "GitHub hook trigger for GITScm polling" from Build Triggers Section.

  5. Select Pipeline script from Definition dropdown under Pipeline section

  6. Start writing the Pipeline script which is written in Groovy Language

    Find below the groovy script -

     pipeline {
         agent any
    
         stages {
             stage('Code') {
                 steps {
                     git url: "https://github.com/shilpi-22/node-todo-cicd.git", branch: "master"
                     echo 'Code clone successful'
                 }
             }
               stage('Build') {
                 steps {
                     sh 'docker build . -t node-todo-cicd'
                     echo 'Code Build successful'
                 }
             }
         }
     }
    
  7. Let's build the job and see the result. We can see the build is successful showing the build pipeline with Code and Build in green box.

Task - 02

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

  • You won't face errors, you can Follow this documentation

  • Complete your previous projects using this Declarative pipeline approach.

Let's enhance the groovy script -

In the above steps, we have just taken a code pull from GitHub and build the docker image. Now, we will be pushing the docker image to dockerHub and deploy the container. To do so, first we need to configure dockerHub credentials in Jenkins.

  1. Go to Dashboard and click on Manage Jenkins

    1. Click on Credentials as below-

3. Click on Global.

  1. Click on Add Credential button

  2. Provide the Username and Password of DockerHub in the username and password field. Also, provide a unique id for these credentials which would be used as an identification from jobs and other configuration and click on Create. The dockerHub credentials is successfully created. We would now use this credential to login to dockerHub in our pipeline script.

  1. Write the below script for logging to dockerHub, pushing the image to dockerHub and deploying the container

  2. The script to login to dockerHub, push the image and deploy the container is as follows-

     pipeline {
         agent any
    
         stages {
             stage('Code') {
                 steps {
                     git url: "https://github.com/shilpi-22/node-todo-cicd.git", branch: "master"
                     echo 'Code clone successful'
                 }
             }
               stage('Build') {
                 steps {
                     sh 'docker build . -t node-todo-cicd'
                     echo 'Code Build successful'
                 }
             }
    
             stage("Push to Private Docker Hub Repo") {
                 steps {
                     withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerPass",
                     usernameVariable:"dockerUser")]) {
                         sh "docker login -u ${env.dockerUser} -p ${env.dockerPass}"
                         sh "docker tag node-todo-cicd:latest ${env.dockerUser}/node-todo-cicd:latest" // tag docker image to updated dockerimage with username of dockerHub
                         sh "docker push ${env.dockerUser}/node-todo-cicd:latest" // Push the updated docker image name to dockerHub
                         echo "Docker Image Push successful"
                     }
                 }
             }
    
             stage("deploy") {
                 steps {
                     sh "docker-compose up -d" // Make sure docker-compose is installed and config is correct
                     echo "Node-app deployment successful"
                 }
             }
         }
     }
    
  3. Now, click on the Build Now button from left side bar and wait for the build to complete. After multiple build failures and fixing the errors, we are able to build the Job successfully

    1. Login to dockerHub to check whether the image is pushed or not

    2. Let's check whether the container is up and running by using "docker ps" command in EC2 instance,

    3. Let's open the url with the exposed port to see the running application.

Task - 03

  • Build a automated declarative pipeline.

In the above example, we have been deploying the build manually. Why not, automate the build as soon as there's a code change in the Git repository.

  1. To do this, we will copy the above pipeline script and paste it in a new file name "Jenkinsfile" and add it to our repository

    .

  2. In Jenkins configuration, select Pipeline script from SCM in the definition dropdown under Pipeline section, select Git in SCM dropdown and provide the repository URL in repository URL dropdown and save the changes.

  3. Now, let's establish a connection from Github to Jenkin via Webhooks. Go to your repository and click on the settings. Click on WebHooks from left side bar and click on Add Webhooks.

    Now, enter the payload url and select Send me everything and add the webhook

  4. Now, update any file in the Git repository and navigate back to Jenkins to see if the build has started automatically.

  5. The build is started as soon as the code is updated as we can see below-

  6. As we see above, the build failed at the last stage i.e. deploy. Let's view the log and fix the issue. Let's check the log, docker-compose is not found. Hence, we need to install docker-compose first and then rebuild it again.

  7. Let's install the docker-compose in our EC2 instance, update the code in Git and wait for the build to trigger automatically in Jenkins. We see the build has started automatic and is successful

  8. Click on #2 hyperlink and select Console Output to check the Output. We see that the build is started by GitHub is printed in the Output as below-

    Finally we have successfully created a CI/CD pipeline and deployed the node-todo-app.

Thanks for reading!

~Happy Learing