0
0
Jenkinsdevops~5 mins

Webhook triggers from GitHub/GitLab in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want Jenkins to start a build automatically after you push code to GitHub or GitLab, you use webhook triggers. This saves you from manually starting builds and keeps your project up to date.
When you want Jenkins to build your project immediately after code changes are pushed to GitHub or GitLab.
When you want to automate testing and deployment after every commit without manual intervention.
When you want to keep your Jenkins builds in sync with the latest code changes in your repository.
When you want to reduce delays between code updates and build feedback.
When you want to trigger Jenkins pipelines from pull requests or merge requests automatically.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  triggers {
    githubPush()
  }
  stages {
    stage('Build') {
      steps {
        echo 'Building the project...'
      }
    }
  }
}

This Jenkinsfile defines a simple pipeline that runs on any available agent.

The triggers { githubPush() } block tells Jenkins to start this pipeline automatically when it receives a push event from GitHub via webhook.

The stages section defines the build steps, here just printing a message.

Commands
This command simulates a GitHub webhook POST request to Jenkins to trigger the build. In real use, GitHub sends this automatically when you push code.
Terminal
curl -X POST http://jenkins.example.com/github-webhook/
Expected OutputExpected
HTTP/1.1 200 OK Content-Type: text/plain OK
This command manually triggers the Jenkins pipeline named 'my-pipeline' using Jenkins CLI, useful for testing or manual runs.
Terminal
java -jar jenkins-cli.jar -s http://jenkins.example.com build my-pipeline
Expected OutputExpected
[my-pipeline] Started by user admin [my-pipeline] Building in workspace /var/lib/jenkins/workspace/my-pipeline [my-pipeline] Finished: SUCCESS
-s - Specifies the Jenkins server URL
This command simulates a GitLab webhook POST request to Jenkins to trigger the build when code is pushed to the main branch.
Terminal
curl -X POST -H 'Content-Type: application/json' --data '{"ref":"refs/heads/main"}' http://jenkins.example.com/gitlab-webhook/
Expected OutputExpected
HTTP/1.1 200 OK Content-Type: text/plain OK
-H 'Content-Type: application/json' - Sets the content type header to JSON
--data - Sends the JSON payload with branch information
This command checks the status and details of the last build of the 'my-pipeline' job in Jenkins.
Terminal
curl http://jenkins.example.com/job/my-pipeline/lastBuild/api/json
Expected OutputExpected
{ "result": "SUCCESS", "building": false, "number": 15, "timestamp": 1686000000000 }
Key Concept

If you remember nothing else from this pattern, remember: Jenkins can automatically start builds when GitHub or GitLab sends a webhook after code changes.

Common Mistakes
Not configuring the webhook URL correctly in GitHub or GitLab.
If the webhook URL is wrong, Jenkins never receives the trigger and builds won't start automatically.
Set the webhook URL exactly to your Jenkins webhook endpoint, for example, http://jenkins.example.com/github-webhook/ for GitHub.
Not enabling the webhook trigger in the Jenkins pipeline configuration or Jenkinsfile.
Without the trigger configured, Jenkins ignores webhook calls and does not start builds automatically.
Add the trigger block like 'triggers { githubPush() }' in your Jenkinsfile or enable webhook triggers in the job settings.
Using the wrong webhook event type or missing required permissions in GitHub/GitLab.
If the webhook is not set to send push events or lacks permissions, Jenkins won't get notified properly.
Configure the webhook to send push events and ensure Jenkins has access permissions to the repository.
Summary
Configure Jenkins pipelines with webhook triggers to start builds automatically on code pushes.
Set the correct webhook URL in GitHub or GitLab to notify Jenkins of changes.
Use Jenkins CLI or API to manually trigger or check build status when needed.