I’m working on a pipeline that receives webhook payloads and I need to extract and process the request body. I’m struggling to figure out the best approach to capture and parse the webhook data within my Jenkins pipeline.
What I’m trying to achieve:
Capture the full JSON payload from incoming webhooks
Parse specific fields from the webhook body
Use this data to trigger different pipeline actions based on the payload content
What I’ve tried so far:
Looking at env variables but not seeing the full payload
Tried using params but that seems to only capture query parameters
Attempted to use currentBuild.rawBuild but getting access issues
If you’re looking to capture and work with the full JSON payload from a webhook in Jenkins, one option that’s worked well for many teams is using the Generic Webhook Trigger plugin. It’s designed to give you access to the raw request body, which can be super helpful when you need to make decisions based on webhook content.
How You Might Set It Up
Install the Plugin
First, make sure the Generic Webhook Trigger plugin is installed on your Jenkins controller.
Enable the Trigger in Your Job
Then, in your pipeline job configuration, you can add a Generic Webhook Trigger as a build trigger. This tells Jenkins to listen for incoming webhooks.
Access and Parse the Payload
The plugin can expose the full request body as a variable, often named something like requestBody. You can parse this JSON in your pipeline using the readJSON step (part of the Pipeline Utility Steps plugin).
Here’s one possible way to do it in Declarative Pipeline syntax:
pipeline {
agent any
triggers {
GenericTrigger(
genericVariables: [
[key: 'requestBody', value: '$', expressionType: 'JSONPath', regexpFilter: '', defaultValue: '']
],
causeString: 'Triggered on webhook',
printContributedVariables: true,
printPostContent: true
)
}
stages {
stage('Process Webhook') {
steps {
script {
def payload = readJSON text: requestBody
// Example: access a field
def action = payload.action
echo "Webhook action: ${action}"
// Use fields to control pipeline logic
if (action == 'deploy') {
// trigger deploy logic
}
}
}
}
}
}
A Few Things to Keep in Mind
The requestBody should contain the full JSON payload sent to the webhook endpoint.
The readJSON step is pretty handy for parsing and working with that data.
You can use fields from the payload to control what your pipeline does conditionally.
Notes & Considerations
If you’re using a Multibranch Pipeline, this might not work out of the box, as the Generic Webhook Trigger doesn’t integrate as directly with multibranch jobs. Some folks work around this by using a separate standalone pipeline to handle the webhook and trigger their multibranch job manually.
Also, depending on your webhook source (e.g., GitHub, GitLab, a custom system), you might need to adjust how you send and interpret the JSON.
For sure, this approach isn’t the only way, but it does give you quite a bit of flexibility if you’re trying to make your Jenkins jobs respond to incoming JSON events in a meaningful way.