Labs · DevOps
Lab: Build Every Branch Automatically with a Jenkins Multibranch Pipeline and a GitHub Webhook
Course: DevOps · Chapter 24: Jenkins with GitHub, GitLab and Bitbucket · Lesson: 24.4 Multibranch
Chapter 24 explains HTTPS vs SSH, webhooks vs polling and multibranch; this lab wires them together.
Chala mitrano! Jenkins la "kahi navin aahe ka?" he dar minitala vicharayla lavu naka. GitHub ne swatah saangava, tyala webhook mhantat.
Suppose we are…
Suppose we are on an Instagram-style feature team where every developer works on a branch. With a single pipeline job, only main gets built and broken branches are found late. Polling Git every minute wastes Jenkins time and still feels slow.
In this lab a Multibranch Pipeline creates one job per branch that has a Jenkinsfile, and a GitHub webhook tells Jenkins the moment someone pushes.
Goal of this lab
By the end you will have:
- A Multibranch Pipeline that lists
mainand a feature branch as separate jobs. - A GitHub webhook that shows a green tick under Recent Deliveries.
- A build that starts within seconds of
git push.
What you need
- Jenkins reachable from GitHub over HTTPS (a lab EC2 server with a domain, or a temporary tunnel). A laptop-only Jenkins cannot receive webhooks.
- The repo and Jenkinsfile from the previous lab.
- A GitHub fine-grained token with read access to that repo, saved in Jenkins as a credential.
- 50 minutes.
Safety
Expose Jenkins only over HTTPS, with your own admin user and no anonymous access. Allow the webhook path; do not open the whole server to everyone without login.
Part 1: Credentials and the job
In Jenkins, open Manage Jenkins → Credentials → System → Global → Add Credentials. Kind Username with password: username = your GitHub user, password = the fine-grained token. ID
github-token.Click New Item, name
badge-multibranch, choose Multibranch Pipeline.Under Branch Sources → Add source → GitHub, pick credential
github-tokenand paste the repository HTTPS URL. Click Validate.What you should see:
Credentials ok. Connected to https://github.com/YOURUSER/badge-ciLeave Build Configuration as by Jenkinsfile. Save.
What you should see: a Scan Repository Log ending with
Finished: SUCCESS, and a job formainthat starts building.
Part 2: The webhook
On GitHub open the repo Settings → Webhooks → Add webhook. Payload URL:
https://YOUR-JENKINS-DOMAIN/github-webhook/(the slash at the end matters). Content typeapplication/json. Events: Just the push event plus Pull requests. Add webhook.Open the webhook and the Recent Deliveries tab.
What you should see: a ping delivery with response
200.
Part 3: Push a branch and watch
Create a feature branch and push it:
git switch -c feature/hello-header echo "// header tweak" >> sum.js git commit -am "Tweak header comment" git push -u origin feature/hello-headerWhat you should see: within a few seconds a new job
feature/hello-headerappears underbadge-multibranchand builds.Open a pull request from that branch on GitHub.
What you should see: a Pull Requests tab in the multibranch job with
PR-1, and the Jenkins status on the pull request page.Merge the pull request and delete the branch on GitHub, then click Scan Repository Now.
What you should see: the branch job is marked as removed (or deleted, depending on the Orphaned Item Strategy).
Clean up
Delete the webhook on GitHub if this was a temporary Jenkins, and revoke the token if you no longer need it. Stop the lab server so it does not cost money.
Ravindra Bagale's Tip
If a push does not start a build, check GitHub Recent Deliveries first. A 200 there means Jenkins got the message; anything else tells you exactly what went wrong.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Payload URL without the trailing slash | 302 or 404 in Recent Deliveries | Use /github-webhook/ exactly |
Jenkins on localhost | GitHub cannot reach it; deliveries time out | Use a reachable HTTPS address for the lab |
| Using your GitHub password as the credential | Fails and is unsafe | Use a fine-grained token with read-only access |
| Branch without a Jenkinsfile | No job is created for it | Add the Jenkinsfile to the branch |
Self-check checklist
0 of 4 done
Try-at-home challenge
Add when { branch 'main' } to a new "Deploy (demo)" stage that only echoes a message. Push to the feature branch and to main and confirm the stage runs only on main.
Samajla ka? GitHub tells Jenkins, Jenkins builds every branch. Aata pudhe jaauya security scans.