How to integrate GitHub repository with Jenkins?

How to integrate GitHub repository with Jenkins?

How to integrate GitHub repository with Jenkins?

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

Today we will discuss on how to integrate a GitHub repository with Jenkins using GitHub webhook. We will also learn on how to integrate a private GitHub repository with Jenkins and trigger a Jenkins pipeline manually using API.

We will integrate our GitHub repository with Jenkins in two ways

  1. Publicly accessible Jenkins controller endpoint + GitHub public/private repository (to store the source code) + Automatic trigger (using GitHub Webhook to trigger the pipeline on every push)
  2. Private Jenkins controller endpoint + GitHub public/private repository (to store the source code) + Manual Trigger (using Scheduled/API trigger to trigger the pipeline)

How to integrate publicly accessible Jenkins controller endpoint with public or private GitHub repository using GitHub webhook?

Warning: This is for demo only, if your Jenkins controller endpoint is exposed to internet then make sure you are using TSL/SSL encryption (HTTPS) for your Jenkins controller before login to Jenkins dashboard and continue with below setup. Or use the 2nd option with private Jenkins endpoint.

Prerequisite

  • One EC2 instance with public IP address
  • One GitHub private/public repository

Step 1: Install and setup Jenkins on EC2 using Docker.

Step 2: Login to your GitHub account and create a public repository with README file (for initial commit) and copy the HTTPS clone URL of your GitHub project.

How to integrate GitHub repository with Jenkins?

How to integrate GitHub repository with Jenkins?

Step 3: Setup a webhook for Jenkins in your GitHub repository.

Navigate to your GitHub repository => Settings => Webhooks => “Add webhook”

How to integrate GitHub repository with Jenkins?

In the “Payload URL”, provide the Jenkins controller IP address followed by github-webhook in “http://<EC2_Public_IP>:8080/github-webhook/

In the “Content type”, select “application/json” and select push event for the webhook trigger. Finally click “Add webhook” to add the Jenkins webhook in GitHub.

How to integrate GitHub repository with Jenkins?

Make sure your GitHub webhook for Jenkins is configured properly. The webhook should get a status OK (200) response from your Jenkins controller endpoint.

How to integrate GitHub repository with Jenkins?

Step 4: Setup your Jenkins pipeline with GitHub.

Login to your Jenkins dashboard and click “New Item”.

How to integrate GitHub repository with Jenkins?

Provide a name to the pipeline and select “Pipeline”. Click “OK”.

How to integrate GitHub repository with Jenkins?

Select “GitHub hook trigger for GITScm pooling” and “Build Triggers”. Optionally you can also provide a description for your pipeline.

How to integrate GitHub repository with Jenkins?

In the “Pipeline” section, select “Pipeline script from SCM” for “Definition”, “Git” for “SCM” and your GitHub HTTPS/SSH clone URL (for public repository) or SSH clone URL (for private repository).

How to integrate GitHub repository with Jenkins?

Note: In case of private repository, you also need to add SSH key in Jenkins credentials and GitHub. This is covered in the Private Jenkins controller endpoint + Private GitHub repository + Scheduled/Manual API trigger section below.

Next, select your GitHub branch name for which you want to trigger your Jenkins pipeline and provide the Jenkinsfile (pipeline definition) location and click “Save”.

How to integrate GitHub repository with Jenkins?

Step 4: Create a Jenkinsfile in root for your GitHub repository and push the changes.

Note: Replace the label as per your Jenkins label.

How to integrate GitHub repository with Jenkins?

The pipeline was successfully executed by GitHub webhook.

How to integrate GitHub repository with Jenkins?

How to integrate private Jenkins controller endpoint with a private GitHub repository and trigger the pipeline using scheduled/manual using API?

Prerequisite

Step 1: Generate a new SSH key for GitHub.

Step 2: Add the new SSH public key in GitHub.

Step 3: Add the new SSH private key in Jenkins.

Login to your Jenkins dashboard, “Manage Jenkins” => “Manage Credentials” => Select “global” as credential domain => “Add credentials”.

How to integrate GitHub repository with Jenkins?

Step 4: Add Jenkinsfile definition to your private GitHub repository.

Note: Replace the label as per your Jenkins node label where you want to execute this pipeline.

Step 5: Setup your Jenkins pipeline with GitHub.

Login to your Jenkins dashboard and click “New Item”.

How to integrate GitHub repository with Jenkins?

Provide a name to the pipeline and select “Pipeline”. Click “OK”.

How to integrate GitHub repository with Jenkins?

Configure your pipeline settings.

In the “Pipeline” section, select “Pipeline script from SCM” for “Definition”, “Git” for “SCM” and your GitHub SSH clone URL as “Repository URL”, in “Credentials” add the credential created in step 3.

How to integrate GitHub repository with Jenkins?

Finally select the branch name and Jenkinsfile location and click “Save”.

How to integrate GitHub repository with Jenkins?

Next, we will configure trigger for the pipeline using API or scheduled.

Option 1: Trigger a Jenkins pipeline manually using API:

Step 1: Create an API Token for your Jenkins user.

Step 2: Update the pipeline trigger configuration.

Select “Trigger builds remotely (e.g. – from scripts)” and provide the API token name. Click “Save”.

How to integrate GitHub repository with Jenkins?

Step 3: Trigger Jenkins pipeline using API.

Our Jenkins pipeline executed successfully.

How to integrate GitHub repository with Jenkins?

Option 2: Trigger a Jenkins pipeline based on schedule:

Update your pipeline “Build trigger” configuration and select “Build periodically” and provide a cron expression for the schedule. Finally click “Save”

How to integrate GitHub repository with Jenkins?

Note: For this demo I have setup the build job to trigger every one minute.

Our pipeline executed as per cron schedule.

How to integrate GitHub repository with Jenkins?

Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.

https://www.jenkins.io/doc/

Leave a Reply

Close Menu