Jenkins GitHub Integration for CI/CD Pipelines example

Jenkins GitHub Integration

Integration between Jenkins and GitHub takes on two separate forms:

  1. The ability of Jenkins to pull code from GitHub
  2. The ability of GitHub to trigger Jenkins build jobs

The first feature, namely the ability to pull code from GitHub is made possible through the Jenkins Git plugin. The ability to trigger Jenkins builds when GitHub commits  occur is provided through the Jenkins GitHub plugin. Both of these plugins are included as suggested plugins when a default installation of Jenkins CI is performed.

Jenkins with GitHub plugins

Jenkins GitHub integration requires the Git and GitHub Jenkins plugins to be installed

Jenkins builds with GitHub source code

With the Git plugin installed, the Jenkins with GitHub integration is achieved by providing the repository URL and branch name to pull in the build job. Jenkins with GitHub source code integration

The repository URL is the key to Jenkins with GitHub source code integration. With this configuration added to a build job, source code will be pulled from GitHub with Jenkins then being able to perform various compilations, testing and build operations on the files.

Your intro to GitHub Actions training course

Here’s how to get started with GitHub Actions:

Follow these tutorials to learn GitHub Actions fast.

Build pipelines with GitHub and Jenkins

To have Jenkins pull GitHub source code within a scripted or declarative build pipeline, simply use the DSL operation named git and provide the repository URL within single quotes.

The following declarative pipeline demonstrates a Docker based, Ant build in Jenkins with GitHub as the source code repository:

# Jenkins with GitHub Integration Pipeline Example
pipeline {
  agent { docker { image 'cameronmcnz/ant-jdk8-git:latest' } }
  stages {
    stage('GitHub Jenkins Ant Docker Build') {
      steps {
        git ''
        sh 'ant clean compile test package war'

Trigger builds in Jenkins with GitHub

The ability to pull code represents only half of the Jenkins GitHub integration puzzle. The other half is to have GitHub trigger a Jenkins build when a developer pushes new code to the repository.

The whole idea behind Jenkins CI/CD pipelines is to be constantly compiling code and running test suites to ensure software development constantly moves forward. If a build is broken or a test fails, it is the job of the CI/CD server to inform the team so the setback can be addressed immediately. This means Jenkins must be notified every time a Git commit is pushed to the GitHub server. This tight integration between the tools is achieved through a Jenkins GitHub webhook.

To implement a webhook, the GitHub hook trigger for GitScm polling must be enables on the Jenkins build job. (Note that the agility to trigger Jenkins builds remotely will not work with GitHub and will result in a 403 crumb error if attempted.)

For the Jenkins GitHub integration to succeed with a webhook, an API token must be generated in Jenkins and then shared as part of the webhook configuration in GitHub.

Jenkins GitHub webhook trigger

Webhooks help integrate GitHub with Jenkins by triggering build jobs remotely.

Jenkins with GitHub integration plugin

This Jenkins Git and GitHub integration example has demonstrated how to manually configure webhook triggers. However, this feature is automated when the GitHub with Jenkins integration plugin is configured through the Jenkins administrative console.

Jenkins with GitHub configuration

Jenkins GitHub integration plugin configuration through the admin console.

When the plugin is configured, these additional Jenkins with GitHub features are activated:

  • Hyperlinks between Jenkins and GitHub are included on the summary page of you build jobs
  • Triggers performed by groking HTTP POSTS happen automatically
  • Other plugins installed in Jenkins can take advantage of GitHub plugin APIs and features

How to integrate Jenkins with GitHub

In summary, the steps required to achieve Jenkins with GitHub integration are:

  1. Install the Jenkins Git plugin
  2. Install the Jenkins GitHub integration plugin
  3. Use GitHub URLs to pull source code into Jenkins
  4. Use GitHub webhooks to trigger Jenkins build jobs
  5. Configure the Jenkins with GitHub integration plugin to enable additional reporting features

Jenkins remains a leader in the CI/CD tools market, and GitHub continues to bring new customers to its cloud based, distributed version control offering. As such, it’s not surprising to know that Jenkins with GitHub integration has been tightly integrated into both of these popular DevOps tools.

App Architecture
Software Quality
Cloud Computing