Reputation: 11384
I am using generic-webhook-trigger
in Jenkins to trigger job when events happen in Github.
It seems I have to extract each variable I need from the big JSON request body to convert them to env-var.
Is it possible to pass the whole JSON body to the Jenkins job and have it parse it?
Upvotes: 3
Views: 2610
Reputation: 46
Thank you to everyone for your help. Expanding on the answer by 'Noam Helmer', I was able to resolve the issue. In order to retrieve the JSON body from the variable, it is necessary to have the 'Pipeline Utility Steps' plugin installed. This plugin will provide the 'readJSON' method to convert the variable from a string to a JSON dataset, allowing you to read any properties using JsonPath. Additionally, make sure to escape PowerShell variables as "$Error[0]". Below is an example of working pipeline (declarative).
Pipeline {
agent { label 'agent_label_here' }
stages {
stage ('The first step or the build') {
steps {
withEnv ("[PAY_LOAD = ${env:payload}]") {
script {
def PAY_LOAD = readJSON text: "${env:payload}"
def user = "${PAY_LOAD.app.userID}"
def server = "${PAY_LOAD.app.DomainName}"
powershell """
try {
Get-ADUser -Identity "${user}" -server "${server}" -ErrorAction SilentlyContinue
} catch {
write-output "\$_.Exception.Message"
write-output "\$Error[0].Exception.Message"
}
"""
}
}
}
}
}
}
Upvotes: 0
Reputation: 6824
You can achieve what you want by assigning the entire body to a specific variable, then read it as Json in your code and parse it by yourself.
For example, if your payload (received post content) is:
{
"ref": "refs/heads/master",
"head_commit": {
"committer": {
"name": "ido",
"email": "[email protected]"
}
}
}
You can define a single parameter in your generic webhook configuration called payload
, set the expression for that parameter to $
, set the expressionType JSONPath
, and when the job is triggered that parameter will include the entire content of the received post content.
You can then parse it by yourself:
def payloadMap = readJSON text: payload
println "ref value is: ${payloadMap.ref}"
println "committer name is: ${payloadMap.head_commit.committer.name}"
You can see more advanced examples for using the generic-webhook-trigger
configurations plugin Here, and especially This one which is more relevant for your requirements.
Upvotes: 4