Sample Webhook Listeners

Below are some sample Webhook Listener use cases and how to implement them.

The Default Listener (POST Webhook)

When creating a new Listener, the script will default to the below which simply forwards the payload via a REST Post request. This script emulates an outbound webhook in the truest sense since no payload modification or logic is done. Simply update the postUrl variable with your endpoint and its ready to go.

//Carry out any custom action you wish for this event. Check out the documentation for reference or ideas. //Optionally filter out events you dont want to execute by returning false on the filter script tab. def listenerName = "myListener"; LOG.info("Running listener: ${listenerName}"); def postUrl = ""; def responseCode = REST.forward(postUrl,EVENT); LOG.setMessage("Message forwarded (${responseCode}) to ${postUrl}");

End Release After Snapshot Deploys to Production

To take advantage of ending a release after a pipeline execution completes you can create a new listener and select the Pipeline Stage Completed. You will then need to check whether the completed stage is the final stage in the pipeline. You can do this in a number of ways. One way is shown below by checking the environment code and comparing it to our final PROD stage.

Example Groovy Code

//Carry out any custom action you wish for this event. Check out the documentation for reference or ideas. //Optionally filter out events you dont want to execute by returning false on the filter script tab. def listenerName = "endReleaseListener"; LOG.info("Running listener: ${listenerName}"); if(EVENT.payload.environment.environmentCode.equals("PROD")){ LOG.setMessage("Release ${EVENT.payload.release.releaseName} has completed moving through the pipeline. Ending release."); FLEXDEPLOY.endRelease(EVENT.payload.release.releaseId); }

Another way to take advantage of this is to use the Webhook Listener’s filter. This filter is also groovy script where you can determine whether the event should be processed by the Listener. In this example, we will filter the events so that only pipeline stage completed events that have completed the Prod stage in the pipeline are allowed through.

Logically, both approaches will accomplish the same task, however, using the Filter will automatically hide it from the Outgoing Webhook Messages display. This will reduce some of the noise and allow you to easily find the messages you care about.

Pipeline Stage Completed Filter