Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Build Every Branch Automatically with a Jenkins Multibranch Pipeline and a GitHub Webhook

Intermediate50 minJenkins with the GitHub Branch Source plugin · A GitHub repository with a Jenkinsfile · A public URL for Jenkins (lab server)

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 main and 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

  1. 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.

  2. Click New Item, name badge-multibranch, choose Multibranch Pipeline.

  3. Under Branch Sources → Add source → GitHub, pick credential github-token and paste the repository HTTPS URL. Click Validate.

    What you should see: Credentials ok. Connected to https://github.com/YOURUSER/badge-ci

  4. Leave Build Configuration as by Jenkinsfile. Save.

    What you should see: a Scan Repository Log ending with Finished: SUCCESS, and a job for main that starts building.

Part 2: The webhook

  1. On GitHub open the repo Settings → Webhooks → Add webhook. Payload URL: https://YOUR-JENKINS-DOMAIN/github-webhook/ (the slash at the end matters). Content type application/json. Events: Just the push event plus Pull requests. Add webhook.

  2. 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

  1. 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-header
    

    What you should see: within a few seconds a new job feature/hello-header appears under badge-multibranch and builds.

  2. 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.

  3. 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

  1. 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

MistakeWhat happensFix
Payload URL without the trailing slash302 or 404 in Recent DeliveriesUse /github-webhook/ exactly
Jenkins on localhostGitHub cannot reach it; deliveries time outUse a reachable HTTPS address for the lab
Using your GitHub password as the credentialFails and is unsafeUse a fine-grained token with read-only access
Branch without a JenkinsfileNo job is created for itAdd 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.