Jenkins GitHub Webhook Integration

In a previous post, we discovered how we can install Jenkins and run a basic pipeline. We have known the way Jenkins interacts with Git(Github) and Container Registry(Harbor). Just wanna remind you a bit that whenever Jenkins gets an order from us, it will clone/pull from the code repository which is pre-configured and takes actions followed by Jenkinsfile. Typically, it runs docker build commands to create a new image and push this image to the Container Registry. Then how can we automate these steps whenever the new code is pushed by developers? Jenkins will be triggered and run the same process as above. Now, l let’s find it out right in this blog through how to install a GitHub webhook to activate Jenkins job.
image

Note:
Ubuntu version 22.04
Jenkins version 2.387.1

Prerequisites:
GitHub account
Jenkins install on Ubuntu, please follow the previous post or you can use KodeKloud playground to skip this step.

Setup a GitHub Repo

First, we need to log in to GitHub and create a new code repository. Then we push the Jenkinsfile as below:

pipeline {
    agent any

    stages {
        stage('Pre-Build') {
            steps {
                echo 'Pre-Build...'
                echo 'Send status Pre-Build to Mail, Telegram, Slack...'
            }
        }
        stage('Build') {
            steps {
                echo 'Building...'
                echo 'Running docker build...'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Push') {
            steps {
                echo 'Pushing...'
                echo 'Running docker push...'
            }
        }
    }
    
    post {
        success {
            echo 'Success...'
            echo 'Send status Success to Mail, Telegram, Slack...'
        }
        failure {
            echo 'Failure...'
            echo 'Send status Failure to Mail, Telegram, Slack...'
        }
    }

}

As you can see, there are 4 stages in the Jenkinsfile: pre-build, build, test, push. In this post, we just need to focus on how to integrate GitHub webhook and Jenkins so I use the command echo for printing messages and explain the main steps we need to do in each stage. Besides that, there is a post section that defines one or more additional steps that are running upon the completion of a Pipeline’s or stage’s run. Here we use two post-conditions which are success and failure. Maybe you can guess the meaning of these two post-conditions. When the job is finished successfully, the commands in the success block will run. On the contrary, when the job fails, the commands in the failure block will run.
Now we have a GitHub repository with Jenkinsfile:

Create Jenkins Pipeline job with this GitHub repo

At this point, we log in to Jenkins and create a new pipeline job. To do that, from the Jenkins dashboard page, click on New Item > input the job name and choose the Pipeline.

Under Pipeline > Definition, choose Pipeline script from SCM, then choose Git and paste the link source code git. Next, update Branch Specifier from master to main which is the default branch of GitHub.

Click to checkbox GitHub hook trigger for GITScm polling under Build Triggers.

Note: You have to use your personal GitHub link because you need permission to set up the GitHub webhook in the next step.

That’s all, now you can scroll down and click the Save button.

In my experience, whenever we have just configured a new Jenkins job, we should try to build this job manually to ensure our actions will work as expected.

So we click Build Now to start executing Jenkinsfile.

Eventually, everything is looking good.

In addition, by hovering over the Build stage and clicking the Logs button, we can check the comments in this stage.

Configure webhook GitHub for triggering Jenkins job

We can see the pipeline is built successfully. So now, we can set up the GitHub webhook to trigger Jenkins whenever developers push the new code.

From the GitHub repository, which we have just created above, click on Settings > Webhooks > Add Webhook.

Under Payload URL, input your Jenkins URL/github-webhook/ and click Add webhook button.
An Example of Payload URL: http://localhost:8080/github-webhook/

Finally, we have configured Jenkins GitHub Webhook Integration successfully. For testing it, we can add something to the README.md file and push this change. Then go to Jenkins job page and we can see the result.

Congratulations, you finally know how to trigger Jenkins job when pushing new code. Knowing and understanding these fundamental configurations will be extremely beneficial for later (work), or more difficult tasks. In the next post, I will share about ArgoCD and Delivering GitOps with ArgoCD.

Awesome, thanks for sharing @raymond.baoly !

1 Like