0
0
JenkinsHow-ToBeginner · 3 min read

How to Use Webhook for Build Trigger in Jenkins

To use a webhook for build triggering in Jenkins, configure your Jenkins job with the GitHub hook trigger for GITScm polling or equivalent plugin and set the webhook URL in your source control repository to point to Jenkins. When a push or pull request event happens, the webhook sends a notification to Jenkins, which then starts the build automatically.
📐

Syntax

To enable webhook build triggers in Jenkins, you typically use the following setup:

  • Jenkins Job Configuration: Enable GitHub hook trigger for GITScm polling or similar option in the job's build triggers.
  • Webhook URL: The URL to configure in your source control webhook settings is http://your-jenkins-url/github-webhook/.
  • Source Control Webhook: Configure your repository to send events (push, pull request) to the Jenkins webhook URL.
text
Jenkins Job Configuration:
- Go to your Jenkins job > Configure > Build Triggers
- Check 'GitHub hook trigger for GITScm polling'

Webhook URL:
http://your-jenkins-url/github-webhook/

Source Control Webhook Setup:
- In GitHub/GitLab repo settings > Webhooks
- Add webhook with above URL
- Select events like 'push' or 'pull request'
- Save webhook
💻

Example

This example shows how to configure a Jenkins freestyle job to trigger builds via GitHub webhook.

Steps:

  • Create a new Jenkins freestyle project.
  • Under Source Code Management, add your GitHub repository URL.
  • Under Build Triggers, check GitHub hook trigger for GITScm polling.
  • In your GitHub repository, go to Settings > Webhooks and add a new webhook with the URL http://your-jenkins-url/github-webhook/.
  • Select application/json as content type and choose Just the push event.
  • Save the webhook.

Now, when you push code to GitHub, Jenkins will automatically start the build.

groovy
pipeline {
  agent any
  triggers {
    githubPush()
  }
  stages {
    stage('Build') {
      steps {
        echo 'Building project triggered by GitHub webhook'
      }
    }
  }
}
Output
[Pipeline] Start of Pipeline [Pipeline] echo Building project triggered by GitHub webhook [Pipeline] End of Pipeline
⚠️

Common Pitfalls

Common mistakes when using webhooks for Jenkins build triggers include:

  • Incorrect webhook URL: The URL must end with /github-webhook/ for GitHub plugin to recognize it.
  • Missing build trigger in Jenkins job: Forgetting to enable GitHub hook trigger for GITScm polling prevents Jenkins from reacting to webhook calls.
  • Firewall or network issues: Jenkins must be accessible from the source control system to receive webhook calls.
  • Not setting correct events: Webhook should be configured to send push or pull request events as needed.
text
Wrong webhook URL example:
http://your-jenkins-url/

Right webhook URL example:
http://your-jenkins-url/github-webhook/

Wrong Jenkins trigger:
# No trigger enabled

Right Jenkins trigger:
# Enable 'GitHub hook trigger for GITScm polling'
📊

Quick Reference

StepDescription
Configure Jenkins JobEnable 'GitHub hook trigger for GITScm polling' in build triggers
Set Webhook URLUse 'http://your-jenkins-url/github-webhook/' in your repo webhook settings
Select EventsChoose push or pull request events to trigger builds
Ensure Network AccessJenkins must be reachable from your source control system
Test WebhookPush code and verify Jenkins starts the build automatically

Key Takeaways

Enable the GitHub hook trigger in Jenkins job to accept webhook calls.
Set the webhook URL in your source control to 'http://your-jenkins-url/github-webhook/'.
Ensure Jenkins is accessible from the source control system for webhook delivery.
Configure webhook events properly to trigger builds on desired actions.
Test by pushing code to confirm Jenkins starts the build automatically.