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).
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.
- Select SSH Username with private key credential type.
- Give the credentials a name to remember by.
- provide a description 4 Make sure to set the username as git
- Select the option to enter the private key directly.
- Click the Add button to add the private key (from the deploy key we created earlier).
- Paste the private key (from the "deploy key), into the form.
- Enter the passphrase for the private key.
- 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.
- 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.
- I'm using a reverse proxy which terminates an HTTPS connection, so I have
- In the ‘Content type’ select: ‘application/json’
- Leave the ‘Secret’ field empty
- Just trigger the push event.
- 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
- Jenkins Plugins - Git
- blog.knoldus.com - Multibranch Pipeline With GitHub Webhook
- Blaze Meter - How to Integrate Your GitHub Repository to Your Jenkins Project
- How to trigger Multibranch Pipeline build with github webhook
First published: 29th June 2021