Ravindra BagaleCourses & study guides मराठी

Chapter 80: DevOps Projects Part 10: Jenkins Agents Freestyle Pipeline Webhook Sonar ECR EKS Approval Libraries Backup

80.4 Project 70: SCM webhook auto-build

Friends, polling wastes minutes. Today we enable a Jenkins webhook endpoint, configure the SCM forge hook, push a commit, and prove the job starts within seconds.

What you will build in this project

  1. Enable webhook trigger on the job.
  2. Add the hook URL in the SCM forge.
  3. Push a commit and watch Build Now fire.
  4. Read delivery logs when it fails.

1. Start from a real company story

At Cognizant in Chennai, jobs polled every fifteen minutes and a hotfix sat invisible for fourteen minutes.

Customer SLA burned. Rough cost: about ₹7 lakh in credits that week.

Imran switched every Multibranch job to webhooks.

Today we wire that auto-build.

git pushcommitforge hookPOSTJenkinswebhook EPbuildseconds
Picture 1: git push → SCM forge hook → Jenkins /generic-webhook → Multibranch scan → build

Ravindra Bagale's Tip

Students paste the Jenkins crumb or API token into a public wiki. Store hook secrets in the forge encrypted settings only. Keep that habit.

2. Start and prepare

Step 1: Why webhooks

Why: Polling is slow and noisy. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 1: a1

Type this:

echo 'Webhook = forge POSTs Jenkins the moment you push'

You will see:

Webhook = forge POSTs Jenkins the moment you push

What it means: Why.

Example 2: a2

Type this:

mkdir -p /tmp/jen70 && echo 'Today: hook URL, secret, push proof'

You will see:

Today: hook URL, secret, push proof

What it means: Scope.

Example 3: a3

Type this:

echo 'Poll SCM every 15 min can hide a hotfix for 14 min'

You will see:

Poll SCM every 15 min can hide a hotfix for 14 min

What it means: Pain.

Step 2: Job trigger

Why: Enable hook on Multibranch. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: trigger tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 4: b1

Type this:

printf '%s\n' 'Job -> Configure -> Scan Multibranch Pipeline Triggers' 'Periodically if not otherwise: disable when hooks work' 'Or Freestyle: Build Triggers -> Generic Webhook Trigger' > /tmp/jen70/TRIGGER.txt && cat /tmp/jen70/TRIGGER.txt

You will see:

Or Freestyle: Build Triggers -> Generic Webhook Trigger

What it means: Trigger.

Example 5: b2

Type this:

echo 'Plugin tip: Generic Webhook Trigger or native git plugin hooks'

You will see:

Plugin tip: Generic Webhook Trigger or native git plugin hooks

What it means: Plugin.

Example 6: b3

Type this:

echo 'Note the hook path Jenkins shows after save'

You will see:

Note the hook path Jenkins shows after save

What it means: Path.

Step 3: Hook URL

Why: What the forge calls. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: URL shape. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 7: c1

Type this:

printf '%s\n' 'https://jenkins.example.com/generic-webhook-trigger/invoke' 'https://jenkins.example.com/git/notifyCommit?url=REPO' > /tmp/jen70/URLS.txt && cat /tmp/jen70/URLS.txt

You will see:

https://jenkins.example.com/generic-webhook-trigger/invoke

What it means: URLs.

Example 8: c2

Type this:

echo 'Replace jenkins.example.com with your real controller hostname'

You will see:

Replace jenkins.example.com with your real controller hostname

What it means: Host.

Example 9: c3

Type this:

echo 'Prefer HTTPS; HTTP hooks get blocked by many forges'

You will see:

Prefer HTTPS; HTTP hooks get blocked by many forges

What it means: TLS.

3. Build the main pieces

Step 4: Forge settings

Why: Add webhook in SCM UI. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: settings tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 10: d1

Type this:

printf '%s\n' 'SCM forge -> Settings -> Webhooks -> Add' 'Payload URL: your Jenkins hook URL' 'Content type: application/json' 'Secret: long random string stored only here' > /tmp/jen70/FORGE.txt && cat /tmp/jen70/FORGE.txt

You will see:

Secret: long random string stored only here

What it means: Forge.

Example 11: d2

Type this:

echo 'Events: push and pull_request (or merge request)'

You will see:

Events: push and pull_request (or merge request)

What it means: Events.

Example 12: d3

Type this:

echo 'Same secret must be configured in the Jenkins trigger token field'

You will see:

Same secret must be configured in the Jenkins trigger token field

What it means: Match.

Step 5: Secret file habit

Why: Generate a token. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: openssl tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 13: e1

Type this:

echo 'openssl rand -hex 32'

You will see:

openssl rand -hex 32

What it means: Generate.

Example 14: e2

Type this:

printf '%s\n' 'hook-secret-DO-NOT-COMMIT' > /tmp/jen70/SECRET.tip && chmod 600 /tmp/jen70/SECRET.tip && ls -l /tmp/jen70/SECRET.tip | awk '{print $1,$NF}'

You will see:

-rw------- /tmp/jen70/SECRET.tip

What it means: Mode 600.

Example 15: e3

Type this:

echo 'Never commit the secret; gitignore any local tip files'

You will see:

Never commit the secret; gitignore any local tip files

What it means: Gitignore.

Step 6: Push proof

Why: Trigger a build. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: commit tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 16: f1

Type this:

echo 'echo tweak >> README.md && git add README.md && git commit -m "ci: webhook proof" && git push'

You will see:

echo tweak >> README.md && git add README.md && git commit -m "ci: webhook proof" && git push

What it means: Push.

Example 17: f2

Type this:

echo 'Within seconds Jenkins should show a new queued/running build'

You will see:

Within seconds Jenkins should show a new queued/running build

What it means: Expect.

Example 18: f3

Type this:

printf '%s\n' 'Acceptance: push to build under 30 seconds' > /tmp/jen70/ACCEPT.txt && cat /tmp/jen70/ACCEPT.txt

You will see:

Acceptance: push to build under 30 seconds

What it means: Gate.

4. Automate and verify

Step 7: Delivery log

Why: Debug failed hooks. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: forge deliveries. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 19: g1

Type this:

printf '%s\n' 'Forge -> Webhooks -> Recent Deliveries' 'Red = look at response code and body' 'Common: 403 CSRF, 404 wrong path, 401 bad secret' > /tmp/jen70/DEBUG.txt && cat /tmp/jen70/DEBUG.txt

You will see:

Common: 403 CSRF, 404 wrong path, 401 bad secret

What it means: Debug.

Example 20: g2

Type this:

echo 'Jenkins system log also shows GenericWebhookTrigger lines'

You will see:

Jenkins system log also shows GenericWebhookTrigger lines

What it means: Syslog.

Example 21: g3

Type this:

echo 'Re-deliver from the forge UI after you fix the path'

You will see:

Re-deliver from the forge UI after you fix the path

What it means: Redeliver.

Step 8: Filter branches

Why: Do not build every tag noise. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: filters. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 22: h1

Type this:

printf '%s\n' 'Optional: only build refs/heads/main and refs/heads/release/*' 'Ignore docs-only paths if your plugin supports path filters' > /tmp/jen70/FILTER.txt && cat /tmp/jen70/FILTER.txt

You will see:

Optional: only build refs/heads/main and refs/heads/release/*

What it means: Filter.

Example 23: h2

Type this:

echo 'Tag pushes can skip CI if you release from a separate job'

You will see:

Tag pushes can skip CI if you release from a separate job

What it means: Tags.

Example 24: h3

Type this:

echo 'Keep PR builds enabled for review gates'

You will see:

Keep PR builds enabled for review gates

What it means: PRs.

Step 9: Disable polling

Why: Once hooks work. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: cleanup. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 25: i1

Type this:

echo 'Remove H/15 * * * * poll schedules after hooks are green for a week'

You will see:

Remove H/15 * * * * poll schedules after hooks are green for a week

What it means: Disable poll.

Example 26: i2

Type this:

echo 'Leave a daily scan as a safety net if you want'

You will see:

Leave a daily scan as a safety net if you want

What it means: Safety.

Example 27: i3

Type this:

echo 'Document the hook URL in the team runbook'

You will see:

Document the hook URL in the team runbook

What it means: Docs.

5. Harden and accept

Step 10: Story Cognizant

Why: Chennai polling lag. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 28: j1

Type this:

printf '%s\n' 'At Cognizant in Chennai, jobs polled every 15 minutes and a hotfix sat invisible for 14 minutes.' 'Imran switched Multibranch jobs to webhooks.' > /tmp/jen70/STORY.txt && wc -l /tmp/jen70/STORY.txt

You will see:

2 /tmp/jen70/STORY.txt

What it means: Story.

Example 29: j2

Type this:

echo 'Rough cost: about ₹7 lakh in credits that week'

You will see:

Rough cost: about ₹7 lakh in credits that week

What it means: Cost.

Example 30: j3

Type this:

echo 'Imran: PASS'

You will see:

Imran: PASS

What it means: Sign-off.

Step 11: Proof files

Why: Notes ready. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: ls. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 31: k1

Type this:

ls /tmp/jen70

You will see:

ACCEPT.txt\nDEBUG.txt\nFILTER.txt\nFORGE.txt\nSECRET.tip\nSTORY.txt\nTRIGGER.txt\nURLS.txt

What it means: Tree.

Example 32: k2

Type this:

test -f /tmp/jen70/ACCEPT.txt && echo 'accept ok'

You will see:

accept ok

What it means: accept.

Example 33: k3

Type this:

echo 'Next: Maven plus SonarQube quality gate project 71'

You will see:

Next: Maven plus SonarQube quality gate project 71

What it means: Bridge.

Step 12: Acceptance

Why: Push starts build fast. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 34: z1

Type this:

echo 'Hook URL HTTPS'

You will see:

Hook URL HTTPS

What it means: URL.

Example 35: z2

Type this:

echo 'Secret matched'

You will see:

Secret matched

What it means: Secret.

Example 36: z3

Type this:

echo 'Push to build under 30s'

You will see:

Push to build under 30s

What it means: Speed.

Check yourself

Q1. Who fixed Cognizant polling lag?

Imran

Q2. Better than poll?

webhook

Q3. Trigger type?

GenericWebhook

Q4. Prove with?

git push

Q5. Read failures in?

delivery log

Q6. Poll interval was?

15 min

Friends, you triggered builds with an SCM webhook. Did you understand? Let us try it once more. Break the secret on purpose, read the failed delivery, then fix it.

Lab: try at home

  1. Enable job trigger.
  2. Configure forge hook.
  3. Push and verify.
  4. Practice failed delivery.

Recap

  • Hooks beat polling.
  • Secrets stay in forge.
  • Delivery logs matter.
  • Multibranch scans on push.
  • HTTPS for hooks.