'How to turn on and off stages based on a webhook JSON payload in a Jenkins Pipeline using CloudBees?
I am attempting to set conditions in a Jenkins Pipeline to turn on and off stages based on values in a JSON payload that's being sent from a BitBucket webhook. I am doing this on Cloudbees, so I have the options of using plugins available there.
There seems to be 2 tasks needed to accomplish this:
Print out the JSON data in the pipeline, then parse through the data to access the values that I want to set if conditions in the stages based on.
Set the conditional in the stages, using a when condition in DSL or a groovy conditon in scripted.
My issue here is with step 1, how am I going to be able to access the JSON data from the external webhook? I have completed tasks previously where I set conditional statements to turn on and off stages based on values in payloads being sent within the cloudbees API (setting conditional statements in a pipeline job based on JSON data being sent from another pipeline job and setting conditional statements based on values in the properties file of the developer's repo), an example groovy function that I've used to print out a JSON object from within the cloudbees API:
(Link from cloudbees docs: https://docs.cloudbees.com/docs/admin-resources/latest/pipelines/cross-team-collaboration#cross-team-when-event-trigger)
String getTriggerCauseEvent() {
def buildCauseInfo = currentBuild.getBuildCauses ("com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause")
if (buildCauseInfo && buildCauseInfo[0]) {
def artifactId = buildCauseInfo[0].event.ArtifactEvent.Artifacts[0].artifactId
return artifactId
}
return "N/A"
}
I then plugged this into a when condition, to turn on, and off stages based on the JSON payload.
How would I do something like this with a BitBucket webhook considering that it's an external API? Should I write a Python script for this using something like the requests module and plug that into each stage of the jenkinsfile?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
