Day 27 Task: Jenkins Declarative Pipeline with Docker

Use your Docker Build and Run Knowledge

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

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

Task-1

  • Step 1: Click on a new item, and then you will have a page on which you have to give your name to your job choose ‘pipeline’ or any other option according to your need and then click ‘ok’.

  • Step 2: After this, you will reach a page where you have different options(like build, build triggers, and source code management) that help you manage your job.

  • Step 3: Write the description "This is Django-todo-cicd-pipeline" and click on the GitHub project and past the GitHub repository URL

  • Step 4: Scroll down to the "Pipeline" section. In the "Definition" dropdown, select "Pipeline script" or "Pipeline script from SCM," depending on your preference and where your Jenkinsfile will be stored. For this example, let's select "Pipeline script." In the "Script" section, you'll define your Declarative Pipeline script.

pipeline {
    agent any

    stages{
        stage("code") {
            steps{
                echo "code cloned"
                git url: "https://github.com/pooja-bhavani/django-todo-cicd.git", branch: "develop"
            }
        }
        stage("build") {
            steps{
                echo "building the image"
                sh "docker build . -t django-todo-app"
            }
        }
        stage("deploy") {
            steps{
                echo "deploying the image"
                sh "docker run -d -p 8000:8000 django-todo-app:latest"
            }
        }
    }
}
  • Step 5: Now, we will run it for which click the ‘build now’ option and a building history will be created then click on it. Before that in your system, you need to give permission to docker and Jenkins
sudo chown $USER /var/run/docker.sock
sudo usermod -aG docker jenkins

Task-02

  • To set up a Docker-integrated Jenkins declarative pipeline using Docker's Groovy syntax inside the stage block, we'll start by configuring the Docker environment. Due to the sensitive nature of passwords, we don't display them openly. Therefore, to interact with Docker, we'll create environment variables by logging in to our DockerHub account and in the Jenkins server.

setting up a Docker-integrated

  • Now, open your Jenkins server and go to the dashboard. Click on 'Manage Jenkins', navigate to 'Plugins', then 'Available Plugins'. Search for 'Environment Injector', select it, and click 'Install'. Once installed, restart your Jenkins server.

  • Return to the dashboard and click on 'Manage Jenkins'. Now, let's create a Docker credential. Navigate to 'Credentials', select 'Stores scoped to Jenkins', then click on 'Global'. Proceed to 'Add Credentials'. Enter your DockerHub username as the username, add your password, set the ID as 'dockerhub', describe it as 'DockerHub Credential', and click 'Create'.

pipeline {
    agent any

    stages {
        stage('cloned') {
            steps{
                git url:'https://github.com/nallabellisriparthu/django-todo-cicd.git', branch: 'develop'
            }
        }
        stage('build and test') {
            steps{
                sh 'docker build . -t sriparthu/django-todo-app:latest'
            }
        }
        stage('Login and Image') {
            steps{
                echo "login in to docker hub and pushing image.."
                withCredentials([usernamePassword(credentialsId:'dockerhub',passwordVariable:'dockerhubPassword',usernameVariable:'dockerhubUser')]) {
                    sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPassword}"
                    sh "docker push sriparthu/django-todo-app:latest"
                }
            }
        }
        stage('deploy') {
            steps{
                echo "depolyed.."
                sh "docker-compose down && docker-compose up -d"
            }
        }
    }
}

  • Now, open AWS, navigate to your instance, select 'Security', click on the security group, and choose 'Edit inbound rules'. Click on 'Add rule', set the port range as 8000 and save the rules.

  • Select the IP address of your AWS instance, copy it, and paste it into a new tab followed by port number 8000.

  • open your docker hub account and see your image.