Automating CI/CD with Jenkins Groovy Scripts

Automating CI/CD with Jenkins Groovy Scripts

Overview:-

Automation is key to achieving efficient and reliable Continuous Integration and Continuous Deployment (CI/CD) pipelines. This blog post explores how to leverage the power of Jenkins Groovy scripts and GitHub webhooks to create a robust and automated CI/CD pipeline. I will guide you through the process of setting up a workflow that automatically triggers when changes are made to your GitHub repository, clones the repository, builds a Docker image, pushes it to Docker Hub, and deploys it to our Server by Docker Compose. I will also use environment variables in Jenkins for secure Pushing Docker Images to the DockerHub Account.

Creating an EC2 Server and Connecting it by SSH:-

  • Sign in to your AWS Account search for EC2 and click on it.

  • Now click on Launch Instance.

  • Name it Jenkins-cicd-Pipeline and in the Application and OS Images (Amazon Machine Image) choose Ubuntu.

  • In the Instance type select the t2.micro one.

  • Choose your Key pair(login).

  • In the Network settings check all three options(SSH, HTTP and HTTPS).

  • From the right side click on Launch Instance from the bottom.

  • We have our Server running now we will connect it by SSH.

  • Click on Connect.

  • We will see this after clicking on Connect and in the SSH client copy the line in Example.

  • Open Terminal and navigate to your directory where your key pair is. My key pair is in Downloads. So I will navigate to downloads by the command:

      cd Downloads/
    

    After navigating to Downloads paste your Example line with sudo in front of it. It should look like this:

Connection is made with our server.

Installing Jenkins, Docker and Docker Compose on our Server:-

  • Now we will Install Jenkins, Docker and Docker Compose on our Server. Jenkins for creating Job(Pipeline), Docker for building Image and Pushing the Image to the DockerHub Account and at the end Docker Compose for Deploying the Application on our server.

  • So let's install Jenkins on our server. First Update thing by:

      sudo apt update
    

  • Jenkins needs Java to Run so we will Install Java by the Command:

      sudo apt install openjdk-17-jre
    

  • Now to Install Jenkins paste these Commands:

      curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
        /usr/share/keyrings/jenkins-keyring.asc > /dev/null
      echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
        https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
        /etc/apt/sources.list.d/jenkins.list > /dev/null
      sudo apt-get update
      sudo apt-get install jenkins
    

This will Install Jenkins on our server.

  • Now we will Install Docker and Docker Compose on our Server. To install docker and docker-compose run the Commands:

      sudo apt-get install docker.io
      sudo apt-get install docker-compose
    

  • At the end, we will add Jenkins to the Docker group by running the Command:

      sudo usermod -aG docker jenkins
    

  • After these, reboot the Server with the Command:
sudo reboot

  • After a few minutes connect the Server again using SSH.

Setting Up Jenkins and Creating the Pipeline:-

  • We are done with Installing Jenkins, Docker and Docker Compose now we will set Jenkins and create the Pipeline.

  • Go to your EC2 Instance and Copy the Public IPv4 addresses

  • Paste this address with PORT 8080 at the end. It should look like this

Expose PORT 8080 in your Security group or else it will not work.

  • Now we will see that Jenkins is Running on our Server.

  • Open your terminal where EC2 is connected and run this Command:

      sudo cat /var/lib/jenkins/secret/initialAdminPassword
    

  • It will give you a Password Paste this in the Administrator password and click on Continue.

  • This will unlock Jenkins now here choose Install suggested plugins:

  • This will start Installing Plugins.

  • Now we have to create the first user. Enter the username you want, any password, full name and e-mail and click on Save and Continue.

  • Here click on Save and Finish.

  • Our Jenkins is ready.

  • Click on Create a job.

  • Name it cicd-pipeline select Pipeline and click on OK.

  • Write a short description I will write This is a CICD Pipeline for my TODO APP.

  • Coming down in the Build Triggers check the GitHub hook trigger for GITScm polling. We have checked it for triggering the Pipeline if there will be any changes made in our GitHub Repository. We will set this up later.

  • Coming down check the GitHub project and paste the GitHub Repository link in the Project url. In the Display name give it a name like TODO APP.

This is my Project URL:

Project

  • At the bottom, we will paste the Groovy Syntax Pipeline code. Paste this:
pipeline {
    agent any
    stages{
        stage("Code"){
            steps{
                git url: "https://github.com/Bharadwajshivam28/Todo-Project.git", branch: "main"
            }
        }
        stage("Build and Test"){
            steps{
                sh "docker build . -t groovy-cicd"
            }
        }
        stage("Push to Repository"){
            steps{
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                    sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                    sh "docker tag groovy-cicd ${env.dockerHubUser}/groovy-cicd:latest"
                    sh "docker push ${env.dockerHubUser}/groovy-cicd:latest"
                }
            }
        }
        stage("Deploy"){
            steps{
                sh "docker-compose down && docker-compose up -d"
            }
        }
    }
}

  • This Pipeline clones the GitHub Repository, builds the Docker Image, Pushes the Image to the DockerHub Account and at the end Docker Compose deploys the Application on the EC2 Server.

  • I am using Environment Variable for my DockerHub username and password for safely pushing the Docker Image to DockerHub Account. We will set the Env later and for now, keep it this only.

  • Click on the Save button.

Setting up Credentials for DockerHub Account and Integrating WebHooks:-

  • We have saved the Pipeline that will do all things for us but now we have to set the Credentials of our DockerHub Account and also set up WebHooks. WebHooks will trigger the Pipeline whenever any changes are made in our GitHub repository.

  • Navigate to Dashboard in Jenkins and click on Manage Jenkins.

  • Now click on Credentials.

  • Here click on System.

  • Click on global Credentials.

  • Click on Add Credentials.

  • Here in the Username write your DockerHub Username and in the Password give your DockerHub Account Password. In the ID write dockerHub

  • Click on Create.

  • Our Credentials are ready these will be used by the Pipeline when our Image is Pushed to the DockerHub Account. We can't openly give our ID and Password so it's a secure way to give.

  • We will set up WebHooks. WebHooks will trigger our Pipeline whenever any changes are made in our GitHub Repository. It's like whenever any Changes are made in the Code then we don't have to always build our Pipeline all things will be automated. Our Task will be over. The Pipeline will start automatically without clicking by us and the Pipeline will do its task by itself.

  • Navigate to your GitHub Repository and click on Settings.

  • From the right side click on Webhooks.

  • Now click on Add webhook.

  • In the payload URL paste your Jenkins IP v4 Address with Port 8080 at the end Also add github-webhook/ at the end. Select Send me everything and click on Add webhook. Overall It should look like this:

  • Our webhook is created. The blue tick shows that is working.

Running the Pipeline:-

  • We are done with everything navigate to your Job created and click on Build Now

  • Our Pipeline is Working Properly and doing all its tasks.

  • On our DockerHub Account, the Image is also pushed.

  • Now Open the PORT 3000 and with your EC2 public address add 3000 at the end. We will see that our Application is running.

  • Whenever we make changes in our GitHub repository the Piepline will be built automatically. We don't have to touch it either. The changes will be also deployed. We will not touch it at any time.

Thanks.