#100DaysOfCloudDevOps Challenge — Day 12 — CI/CD: Integrating Jenkins with GitHub

anjanj
6 min read

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern software development. Jenkins, a popular open-source automation server, allows you to automate the entire process, from building to testing and deploying your code. In this guide, I’ll walk you through the steps to integrate Jenkins with GitHub, set up a simple pipeline and troubleshoot common issues.

Set Up Jenkins Locally

Before integrating Jenkins with GitHub, ensure you have Jenkins installed and running locally. Here’s how to do it:

Install Jenkins using Docker or …

  1. Download and Install Jenkins:

    • Go to the official Jenkins website and download the installer for your operating system.

    • Run the installer and follow the setup instructions.

  2. Access Jenkins:

    • After installation, open your browser and navigate to http://localhost:8080 to access Jenkins.

Install the GitHub and Generic Webhook Trigger Plugins

To handle webhooks and integrate Jenkins with GitHub efficiently, you need to install the GitHub, Github Integration Plugin and Generic Webhook Trigger Plugin.

  1. GitHub and Github Integration Plugin:

    • This plugin allows Jenkins to interact with GitHub, enabling features like SCM polling, building projects based on GitHub repository changes, and triggering builds on pull requests or commits.

    • To install it, go to Manage Jenkins → Manage Plugins → Available, search for GitHub and Github Integration Plugin, and install it.

  2. Generic Webhook Trigger Plugin:

    • This plugin enables Jenkins to trigger builds on any HTTP webhook request, including GitHub push notifications.

    • To install it, go to Manage Jenkins → Manage Plugins → Available, search for Generic Webhook Trigger Plugin, and install it.

After installing these plugins, restart Jenkins to ensure they are fully integrated.


Set Up ngrok to Expose Jenkins Locally

Since Jenkins is running locally, it can’t be directly accessed by GitHub (which is hosted on the web). To expose Jenkins to the internet, we will use ngrok.

  1. Create an ngrok Account

    - Go to the ngrok website and sign up for a free account.

    - After signing up, you’ll get an AuthToken that you’ll need to authenticate ngrok.

  2. Install ngrok

    - Download and install ngrok from ngrok’s download page.

    - Follow the installation instructions for your operating system.

  3. Authenticate ngrok

    - After installing ngrok, open a terminal (or Command Prompt) and run the following command to authenticate ngrok with your account:

ngrok authtoken <your_auth_token>

Replace <your_auth_token> with the AuthToken you received when you created your ngrok account.

  1. Expose Jenkins Using ngrok

    - To expose Jenkins (running on port 8080), use the following command:

ngrok http 8080
  • After running this command, ngrok will provide a public URL (e.g., https://abcd1234.ngrok.io). Copy this URL as you’ll need it for configuring the GitHub webhook.

Configure GitHub Webhook to Trigger Jenkins Builds

Now that Jenkins is exposed via ngrok, you can configure GitHub to trigger Jenkins builds on certain events, like a push to your repository.

  1. Generate a GitHub Token in Jenkins

    - Go to Manage Jenkins → Manage Credentials → Global credentials.

    - Add a new GitHub Personal Access Token with repo and workflow permissions.

  2. Set Up a Webhook in GitHub

  • Go to your GitHub repository and navigate to Settings → Webhooks → Add webhook.
  • Set the Payload URL to http://<ngrok-url>/github-webhook/, where <ngrok-url> is the public URL generated by ngrok (e.g., https://abcd1234.ngrok.io). Important: to add /github-webhook/ at the end of the URL.

  • Set Content type to application/json.

  • Under Which events would you like to trigger this webhook?, select Push events (or any event you want to trigger the build).

  • Click Add webhook.


Configure Jenkins Job to Use GitHub Repository

Now that GitHub is set up to trigger Jenkins builds, let’s configure Jenkins to pull from your GitHub repository.

  1. Create a New Jenkins Job

  • Go to Jenkins → New Item and create a Pipeline project.

  • Under the Pipeline section, select Pipeline script from SCM.
  1. Configure the GitHub Repository

  • Select Git as the SCM (Source Code Management) system.

  • Enter the GitHub repository URL (e.g., https://github.com/your-repo/your-project.git).

  • In Branch Specifier, enter */main (or your branch name).

  1. Define the Jenkinsfile Location

  • In Script Path, specify Jenkinsfile (make sure your Jenkinsfile is located in the root of your repository).

Create the Jenkinsfile for Your Pipeline

The Jenkinsfile defines the steps Jenkins will follow to build, test, and deploy your code. Below is a simple example:

pipeline {
    agent any  // Use any available agent to run the pipeline
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/<your-repo-link>'
            }
        }
        stage('Build') {
            steps {
                sh 'make build'  // Replace with your actual build command
            }
        }
        stage('Test') {
            steps {
                sh 'make test'  // Replace with your test command
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
                // Replace with your actual deployment steps
            }
        }
    }
    post {
        success {
            echo 'Build completed successfully!'
        }
        failure {
            echo 'Build failed.'
        }
    }
}

Key Components of the Jenkinsfile:

  • pipeline: The top-level block that defines the pipeline.

  • agent any: Tells Jenkins to run the pipeline on any available agent.

  • stages: Defines individual stages (Checkout, Build, Test, Deploy).

  • post: Defines actions after the pipeline completes, such as success or failure notifications.


Troubleshooting Common Issues

Even after everything is set up, you might encounter a few issues. Here are some common problems and how to fix them.

Issue 1: No Stages Show Up in Jenkins

  • Ensure the Jenkinsfile is in the Root: The file should be in the root directory of your repository. If it’s in a subfolder, ensure that you specify the correct Script Path in the Jenkins job configuration.

  • Check GitHub Webhook Delivery: Ensure GitHub is sending the webhook correctly and Jenkins is receiving the event.

  • Use Simple Commands for Debugging: Try using simple commands like echo to ensure Jenkins is processing each stage.

Issue 2: Jenkins Can’t Find the Jenkinsfile

  • Correct Branch: Ensure Jenkins is configured to build the correct branch, where the Jenkinsfile resides.

  • GitHub Repository URL: Double-check that the GitHub repository URL is correctly configured in Jenkins.

Issue 3: Build Not Triggered by GitHub Push

  • Webhook URL: Ensure the webhook URL in GitHub points to your Jenkins server.

  • Polling SCM: As a temporary measure, enable Poll SCM in Jenkins to periodically check for changes on GitHub.

Integrating Jenkins with GitHub for CI/CD provides a robust solution to automate your software development lifecycle. By properly configuring the Jenkinsfile, setting up GitHub webhooks, and understanding how to troubleshoot common issues, you can establish a smooth and efficient pipeline that automates the entire process of building, testing, and deploying your code with minimal manual intervention. This integration not only enhances productivity but also ensures consistent and reliable deployments, enabling you to focus more on development and innovation.


Find the GitHub repo here.

If you find this blog helpful too, give back and show your support by clicking heart or like, share this article as a conversation starter and join my newsletter so that we can continue learning together and you won’t miss any future posts.

Thanks for reading until the end! If you have any questions or feedback, feel free to leave a comment.

0
Subscribe to my newsletter

Read articles from anj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

anj
anj