Accessing the Full Payload of a GitHub Trigger in Jenkins

Webhooks in GitHub are incredibly useful for triggering actions externally, and Jenkins is often on the receiving end. When GitHub sends a webhook trigger, it includes a payload that contains detailed information about the event. In this article, we’ll see how to access the full payload of a GitHub trigger in a Jenkins pipeline.

Prerequisites:

  1. Jenkins Setup: Ensure Jenkins is up and running. You should have the necessary permissions to create or modify Jenkins pipelines.
  2. GitHub Plugin: Make sure the GitHub plugin is installed in Jenkins.
  3. Generic Webhook Trigger Plugin: Install the Generic Webhook Trigger Plugin from the Jenkins plugin marketplace. This plugin allows us to capture the entire webhook payload.
  4. Pipeline Utility Steps Plugin: Ensure that the Pipeline Utility Steps Plugin is installed. This provides us the readJSON method that we’ll use to parse the JSON payload.

Jenkinsfile Configuration:

Here’s a Jenkinsfile that forms the core of our pipeline:

pipeline {
    agent any

    stages {
        stage('Process Payload') {
            steps {
                script {
                    // Check if the payload is available
                    if (env.payload) {
                        def parsedPayload = readJSON text: env.payload
                        if (parsedPayload instanceof net.sf.json.JSONObject) {
                            // Handle JSONObject
                            if (parsedPayload.containsKey('repository')) {
                                echo "Repository name: ${parsedPayload.repository.name}"
                            } else {
                                echo "Repository field not found in payload"
                            }
                        } else if (parsedPayload instanceof net.sf.json.JSONArray) {
                            // Handle JSONArray
                            echo "Payload is an array. Length: ${parsedPayload.size()}"
                        } else {
                            echo "Unexpected type of payload"
                        }
                        echo "Full payload: ${env.payload}"
                    } else {
                        echo "Payload is empty or not available!"
                    }
                }
            }
        }
    }
}

In the above Jenkinsfile:

  • We use the readJSON method to parse the incoming JSON payload.
  • We print the entire payload.
  • We also extract the repository name from the payload as an example.

Common Errors and Troubleshooting:

  1. If you encounter the error NoSuchMethodError related to readJSON, it indicates the Pipeline Utility Steps Plugin is missing or not loaded correctly. Install or re-install this plugin as described in the prerequisites.

  2. If you face the error MissingPropertyException: No such property: payload, ensure the payload is being passed correctly through the Generic Webhook Trigger Plugin settings.

  3. Always ensure Jenkins is restarted after installing or updating plugins to ensure they are loaded correctly.

Conclusion:

Harnessing the power of GitHub webhooks in Jenkins allows us to create dynamic and responsive CI/CD pipelines. By accessing the full payload of a webhook trigger, we gain the flexibility to make decisions based on any piece of data GitHub sends us. This forms the basis for many advanced Jenkins workflows, enabling tighter integration between source code management and CI/CD processes.