Jenkins e2e project

what is jenkins?

Jenkin is an open-source project written in Java that runs on windows, macOS and other unix like os. It is free, community supported and might be your first-choice tool for CI.

what is CI/CD ?

what is CI - CI stand for continuous integration it is a process where you integrate a set of tool or set of process that you follow before delivering to your application to your customer.

what is CD - CD stand for continuous delivery (manual) or deployment (automatic) it is a process where you deploy your applicaion on a specific plateform to your customer.

what is agent in jenkins ?

An agent is typically a machine or container which connect to a jenkins controller and executes tasks when directed by the controller.

what is jenkins agent any ?

Jenkins will run the job on any of the available nodes.

What is node in jenkins?

A machine which is part of the jenkins environment and capable of executing pipelines or jobs.

differences between Declarative and Scripted Pipeline

Declarative Pipeline :- Declarative pipelines break down stages into multiple steps while in scripted pipelines there is no need for this. The benefit of Declarative Pipeline is its readability and ease of use, as it is designed to be more intuitive and less verbose than Scripted Pipeline.

Example of a simple Declarative Pipeline:-

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Scripted Pipeline :- Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on Groovy scripting language. In Scripted Pipeline, the entire workflow is defined in a single file called a Jenkinsfile. The Jenkinsfile is written in Groovy and is executed by the Jenkins Pipeline plugin. Scripted Pipeline provides a lot of flexibility and control over the workflow, but it can be more complex and verbose than Declarative Pipeline. In the Scripted Pipeline they have only two basic blocks: “node” and “stage”. A “node” block specifies the machine that executes a particular pipeline, whereas the “stage” blocks are used to group steps that, when taken together, represent a separate operation.

Example of a simple Scripted Pipeline:

node {
    stage('Build') {
        // Build the application
        sh 'mvn clean install'
    }
    stage('Test') {
        // Run the tests
        sh 'mvn test'
    }
    stage('Deploy') {
        // Deploy the application
        sh 'deploy.sh'
    }
}

Start project:-

Clone the repository :-

git clonehttps://github.com/Hemantjangir53/django-notes-app.git

first of all launch ec2 server then open jenkins window using prot <ip_add>:8080

then create a job and give any name and select pipeline checkbox.

give any discreption related to your project

select gitHub project checkbox if you use gitHub code, then end of the scroll bar in pipeline you type pipeline script this is also know as groovy code or Groovy DSL (Declarative Pipeline syntax).

  1. Groovy code stracture
pipeline {
    agent any 
    stages {
        stage('code clone') {
            steps {
                echo "code cloned successfully"
            }
        }
        stage('Build') {
            steps {
                echo "code build successfully"
            }
        }
        stage('push to docker Hub') {
            steps {
                echo "code push"
            }
        }
        stage('deploy') {
            steps {
                echo "container deploy"
            }
        }
    }
}

2. clone code from gitHub

stage('code clone') {
            steps {
                git url: "https://github.com/Hemantjangir53/node-todo.git", branch: "main"
                echo "code cloned successfully"
            }
        }

3. Build code

stage('Build') {
            steps {
                sh 'docker build . -t node-todo-app'
                echo "code build successfully"
            }
        }

if sometimes you can see some error like —

+ docker build . -t node-todo-app
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/
permission denied while trying to connect to the Docker daemon socket at unix:///var/
run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=
%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpuse
tmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&netw
orkmode=default&rm=1&shmsize=0&t=node-todo-app&target=&ulimits=null&version=1":
 dial unix /var/run/docker.sock: connect: permission denied

I did the quick fix and it worked immediately.

sudo chmod 777 /var/run/docker.sock

4. image push to dockerHub

first we need to login dockerHub useing pipeline

 stage('push to docker Hub') {
            steps {
                withCredentials([
                        usernamePassword(
                            credentialsId: 'dockerHub',
                            usernameVariable: 'DOCKER_USERNAME',
                            passwordVariable: 'DOCKER_PASSWORD')]){
                                sh "docker login -u ${env.DOCKER_USERNAME} -p ${env.DOCKER_PASSWORD}" // always use in double invertated commas " "(' '--> show error ) in docker command login .
                    }

                echo "code push"
            }

install docker compose

sudo apt install docker-compose

here we can see in terminal we run docker-compose that image name is <folder_name>_<service_name>

service_name use in .yaml file

Now image push to dockerHub and using tag image, image name also change.

withCredentials([
                        usernamePassword(
                            credentialsId: 'dockerHub',
                            usernameVariable: 'DOCKER_USERNAME',
                            passwordVariable: 'DOCKER_PASSWORD')]){

                                sh "docker tag node-todo-app ${env.DOCKER_USERNAME}/node-todo-app:latest"
                                sh "docker login -u ${env.DOCKER_USERNAME} -p ${env.DOCKER_PASSWORD}"
                                sh "docker push ${env.DOCKER_USERNAME}/node-todo-app:latest"

                    }

5.After push code(image) on docker Hub, run cmd of docker container

stage('deploy') {
            steps {
                sh "docker run -d -p 8000:8000 hemantjangir/node-todo-app:latest"
                echo "container deploy"
            }
        }

But we Build second time this pipeline code this give error that port is already allocated. so we use here docker-compose cmd.

stage('deploy') {
            steps {
                //sh "docker run -d -p 8000:8000 hemantjangir/node-todo-app:latest"
                sh "docker-compose down && docker-compose up -d"
                echo "container deploy"
            }
        }

Now final node-todo-app code for delivery

pipeline {
    agent any 
    stages {
        stage('code clone') {
            steps {
                git url: "https://github.com/Hemantjangir53/node-todo.git", branch: "main"
                echo "code cloned successfully"
            }
        }
        stage('Build') {
            steps {
                sh 'docker build . -t node-todo-app'
                echo "code build successfully"
            }
        }
        stage('push to docker Hub') {
            steps {
                withCredentials([
                        usernamePassword(
                            credentialsId: 'dockerHub',
                            usernameVariable: 'DOCKER_USERNAME',
                            passwordVariable: 'DOCKER_PASSWORD')]){

                                sh "docker tag node-todo-app ${env.DOCKER_USERNAME}/node-todo-app:latest"
                                sh "docker login -u ${env.DOCKER_USERNAME} -p ${env.DOCKER_PASSWORD}"
                                sh "docker push ${env.DOCKER_USERNAME}/node-todo-app:latest"

                    }

                echo "code push"
            }
        }
        stage('deploy') {
            steps {
                //sh "docker run -d -p 8000:8000 hemantjangir/node-todo-app:latest"
                sh "docker-compose down && docker-compose up -d"

                echo "container deploy"
            }
        }
    }
}

you also give name of docker image same as dockerHub push image, using image key in docker-compose.yaml>services>webs

Now make a new Jankinsfile in github and copy all groovy code and change something in pipeline . like following this:-

now press Build Now button

For continuous Deployment (no need to press any button/automated)

  1. first we need to give anywhere access in security inbound rule.

  2. then we need to add webhooks in gitHub

  1. In the Build Triggers section, check the GItHub hook trigger section

  2. Now you can any change(commit) in your github code and after commit any file your automatically deploy you don’t need to press any key.

final stage view of deployment

So, here we can sew the how easily jenkins e2e-project deliver and deploy using jenkins.

Thanks.