Jenkins pipeline project

Jenkins pipeline project

·

4 min read

Hello connections, today we will be creating a Jenkins pipeline script that clones the repository from GitHub, uses docker to build an image, docker-compose to create a container and then pushes it to the docker hub

The following steps are followed to do this.

  1. Creating Jenkins master and agent instances

  2. Generating ssh key

  3. Creating the agent node on Jenkins

  4. Creating a declarative pipeline to send jobs to jenkins agent

  5. Adding Github hook trigger

Creating Jenkins master and agent instances

  1. First, we shall launch 2 t2.micro EC2 instances

    1. Jenkins master - Make sure this has Jenkins and java installed and open port 8080

    2. Jenkins agent - Make sure this has Java and docker installed installed

Generating ssh key

  1. We will first generate the ssh key in master. Once creayted, we will add the public ssh key to the agent.

  2. To create ssh key, in the master machine, get into .ssh folder and type the command ssh-keygen (we can leave folder and passphrase empty, the cli takes the default folder and passphrase ie., just press enter )

  3. We copy the public rsa key ie id_rsa.pub

    1. We use the id_rsa.pub to connect to Jenkins agent. To do this, in the Jenkins agent instance open the authorized_keys file in .ssh folder and paste the public key that we copied from Jenkins master machine

Creating the agent node on Jenkins

  1. In the Jenkins dashboard select Manage Jenkins>Nodes>New node. Create a new node with node name dev- agent and select a permanent agent

  2. Enter all the required fields:

    1. In the description, we can add that the agent will be handling dev builds.

    2. No of executors refers to the maximum number of concurrent builds that Jenkins may perform on this node, let us keep this 1 for simplicity

    3. The root directory refers to the directory that is dedicated to Jenkins in the agent machine

  1. In the launch agent, select the launch method via ssh as will be using ssh to connect to the Jenkins agent. Add the host IP address, select Add credentials

  2. Select the Kind as ssh username with private key and add the required fields

  3. Now, enter the Jenkins master's private key and click Add

    (As we have already added the master's public key to the Jenkins agent before and now we are adding the public key, these both keys will be matched to complete the ssh connection )

  4. Next, select "Non verifying Verification Strategy" as we will be using '2 ssh keys match to complete the setup and press "Save"

Creating a declarative pipeline to send jobs to jenkins agent

Now since the agent is successfully setup, all we have to do is create a pipeline job that sends jobs to the agent

  • First, enter the build name of your preference and select pipeline, click OK

  • Next, the description and github url of the project

  • Select, GitHub hook trigger for SCM polling as we will be using this method to start the build.

Adding Github hook trigger

To use GitHub web trigger, we need to add webhook in our GitHub repository. To do this

  • go to your repository in Github>Settings> Webook>Add Webhook.

  • Enter your Jenkins server URL followed by /github-webhook/

  • Select send me everything>Add webhook

  • When you refresh the page, you can see a small green tick beside your webhook indicating that the webhook was successfully added

Now in the pipeline, we can select pipeline script and add our script

pipeline{
    agent{ label 'dev-agent' }
    stages{
        stage('Code'){
            steps{
                git url: 'https://github.com/sowmya-bm/node-todo-cicd.git', branch: 'master'
            }
        }
        stage('Build and Test'){
            steps{
                sh 'docker build . -t sowmyabm/node-todo-app-cicd:latest'
            }
        }
          stage('Login and push image'){
            steps{
                withCredentials([usernamePassword(credentialsId:'dockerhub',passwordVariable:'dockerHubPassword',usernameVariable:'dockerHubUser')]){
                    sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
                    sh "docker push sowmyabm/node-todo-app-cicd:latest"
                }
            }
        }
        stage('Deploy'){
            steps{
                sh 'docker-compose down && docker-compose up -d'
            }
        }
    }
}

Make sure to open port no 8000 in inbound rules, as the application is running on this port

Once the script is complete, you can make some changes in github and commit it and can see that Jenkins automatically starts building the code the build is successful.

We can also write the entire script in a file named Jenkins file in our GitHub repo and include this file instead of writing the whole script in jenkins.

  1. To do this, add the Jenkins file with the required Groovy script to your GitHub repo

  2. In the Jenkins definition, select Pipeline script from SCM, provide the repository link(https://github.com/sowmya-bm/node-todo-cicd.git), specify the credentials if the repo is private else it can be left blank, specify the branch name (master)and the file path (Jenkinsfile) and click Save

  3. You can see that whenever there is a new push, the build starts