Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Integrate Jenkins Multibranch Pipeline With Git / GitHub

This tutorial will show you how to integrate your "multibranch pipeline" Jenkins project with GitHub so that your project builds when specific branches are pushed to (e.g. staging/production).

This tutorial is specific to when you select "multibranch pipeline" when creating a new project, but the steps may be the same or similar to other project types.

Related Posts

Steps

In Jenkins open the configuration settings of your multi-branch project.


Navigate down to Branch Sources and click Add source


Choose Git as the source, and not GitHub (here's why).


Get the SSH connection details from your project in GitHub:


... and plug them into Jenkins:


Credentials

Create your GitHub deploy key. You will need it for the next step.

Then click Add beside Credentials to add your credentials, and select this project.


  1. Select SSH Username with private key credential type.
  2. Give the credentials a name to remember by.
  3. provide a description 4 Make sure to set the username as git
  4. Select the option to enter the private key directly.
  5. Click the Add button to add the private key (from the deploy key we created earlier).


  1. Paste the private key (from the "deploy key), into the form.
  2. Enter the passphrase for the private key.
  3. Click Add to finish adding the SSH key credentials.

GitHub Webhook

Now we need to set up a webhook for GitHub to trigger our pipeline to build whenever there is an update.

One just needs to to alter the URL below with the URL of your Jenkins server, and the URL of your GitHub repository. Hitting this URL will notify Jenkins there has been an update, and it will then perform a scan of the repository for changes, for which if there are any, it will perform a build.

http://jenkins.mydomain.com:8080/git/notifyCommit?url=https://github.com/my-github-username/name-of-my-repository

Go to your GitHub repository and click on Settings, Webhooks, and then Add webhook.


  1. In the Payload URL field, paste the URL you built up in the earlier step.
    • I'm using a reverse proxy which terminates an HTTPS connection, so I have https:// and no :8080 in the screenshot, but you may need those from the URL you copied earlier if you aren't using a reverse proxy.
  2. In the ‘Content type’ select: ‘application/json’
  3. Leave the ‘Secret’ field empty
  4. Just trigger the push event.
  5. Press the button to add the webhook.

Test It

You can quickly check that the webhook works by clicking on the webhook that now appears in your project's webhooks area.


Then click on Recent Deliveries and you will see an overview of the requests made:


Click on a request to see the request/response in more detail:

Conclusion

Now when you push to your GitHub repository, it will prompt Jenkins to poll for changes and build staging/production if there are changes.

References

Last updated: 11th October 2021
First published: 29th June 2021