Exploring Two Ways of Jenkins-GitHub Integration

ยท

4 min read

Exploring Two Ways of Jenkins-GitHub Integration

In this blog, we'll explore how to seamlessly connect your Jenkins server with your GitHub account for Continuous Integration and Continuous Deployment (CI/CD) processes. We'll delve into two straightforward methods for integrating Jenkins with GitHub, ensuring that your Jenkins pipeline automatically kicks off whenever there's a code commit to your GitHub repository. Let's dive in!

# Prerequisites:

  • Server(local machine) with Jenkins installed.

  • Github account with demo repository.

We'll cover two methods for integrating Jenkins with GitHub. The first method involves using GitHub hook trigger for Git SCM polling, while the second method is through Poll SCM. Both approaches enable Jenkins to detect changes in your GitHub repository and trigger the pipeline accordingly.

  1. Method 1: Github Hook trigger for Git SCM Polling:

    When Jenkins receives a GitHub push hook, GitHub Plugin checks to see whether the hook came from a GitHub repository which matches the Git repository defined in SCM/Git section of the job. If they match and this option is enabled, GitHub Plugin triggers a one-time polling on GITScm. When GITScm polls GitHub, it finds that there is a change and initiates a build. The last sentence describes the behaviour of Git plugin, thus the polling and initiating the build is not a part of GitHub plugin.

    • Adding payload url to github repository:

      1. Go To Github repository for which you are building the CICD pipeline

      2. Go to the repository settings. Then click webhooks and then click Add Webhook.

      3. Add a payload url:

        e.g. jenkins_server_ip:8080/github-webhook

      4. Next select content-type as application/json.

      5. Other all keep default and click create Webhook.

    • Next you have to check the Github hook trigger for GITScm polling under Build Trigger section.

    • Your Github is integrated with the jenkins and will initiate the pipeline when the changed code is pushed to github.

  2. Method 2: Poll SCM:

    A feature in Jenkins that periodically checks the version control system for changes. It uses a cron: Cron is a time-based job scheduler in Unix-like operating systems to schedule and automate the execution of jobs periodically at fixed times or specific intervals.

    • Install github plugin on Jenkins:

      Open Jenkins -> Click Manage Jenkins -> Click Plugins -> Click Available Plugins -> Search for Github plugin and click Install.

    • Generating ssh keys on jenkins server:

      1. On Jenkins server (where jenkins is installed) create ssh keys using the command ssh-keygen

         #To go into home directory
         cd ~
        
         #Command to create the ssh keys
         ssh-keygen
        
         #To go inside .ssh to see generated keys
         cd .ssh
        

        Above are the files present in .ssh directory

        id_rsa = private key

        id_rsa.pub = public key

        From this private key will be with jenkins and the public key we will give to the github.

    • Adding public key to github:

      1. Go to Github settings -> click SSH & GPG keys

      2. Click new ssh key -> any title you can give -> next copy paste the public key into the key section.

      3. Click Add SSH key to create key.

    • Adding private key to Jenkins:

      1. Go to Jenkins webapp -> Click Manage Jenkins -> Click System -> then Global Credentials.

      2. Click Add Credentials

        Kind: ssh username with private key

        Id: any meaningful name

        Username: the jenkins user (user with which you are using jenkins).

        Private key: copy the created key id_rsa file content directly to the text box.

      3. Click create.

    • Creating Job by selecting Poll SCM:

      1. Select the Poll SCM from the pipeline to trigger/notice the code changes on github.

      2. You can run the Poll SCM to check for the code changes on github using the cron by setting the time intervals.

  3. Difference between Poll SCM and Webhook trigger:

    • Polling requests are made by the the receiver(here jenkins)of the data updates while webhook requests are made by the source(here github repo) of the data.

    • Polling is set up to run at fixed intervals and runs whether there is a new event or not whereas webhooks are also automatically triggered when an event occurs.

In summary, integrating Jenkins with GitHub streamlines CI/CD workflows. Whether you opt for GitHub hook trigger or poll SCM, these methods ensure seamless pipeline triggering on code commits. Mastering these integrations enhances collaboration and accelerates deployment. So, go ahead, experiment, and witness the power of Jenkins-GitHub integration in action! Happy coding!

ย