Triggering jobs with a simple webhook

If you use customized tools to create build artifacts, you can trigger jobs when a new version of a build artifact is ready with a custom HTTP call. First, you need to create a webhook on the Operations Center. Then, enable notifications on both the Operations Center and the Master. Finally, create a job on the master that is triggered when this webhook is called.

This tutorial steps you through each of these.

Prerequisites

The installation of the following plugins on each CloudBees Core instance:

  • notification-api (required): responsible for sending the messages across teams and jobs.

  • pipeline-event-step (required): provides a Pipeline step to add a new event publisher or trigger.

  • operations-center-notification (optional): provides the router to transfer the messages across different teams and it’s required if you want to implement cross-team collaboration. It is still possible to use the cross-team-collaboration feature without this plugin, by using the local-only mode which allows you to trigger events across different jobs inside the same team.

Configuring a simple webhook on the Operations Center

To configure a simple webhook on the Operations Center:

  1. On the Operations Center dashboard, select Manage Jenkins > Manage Notification Webhook HTTP Endpoints.

    Manage Notification Webhook HTTP Endpoints menu item
  2. Click Create a new Endpoint.

  3. Enter a Webhook Name.

  4. Select Custom JSON Webhook as the Webhook Message Format.

  5. Select the Define a list of IP addresses that are allowed to send events to this endpoint checkbox and enter a list of IPv4 and IPv6 addresses.

    Create a new Endpoint form
    This use case is minimal; it works with an IP filtered endpoint. For more complex uses, see External HTTP endpoints.
    If you do not enter an IPv4 or IPv6 address, all hosts will be denied access.
  6. Click Save Webhook.

    List of created webhooks
  7. Test the endpoint with cURL or an equivalent tool, by checking the “Last Event” and “Last Status” on the Webhook management screen.

curl -vvv \
    --data '{"my_source": "homemade", "application": "real-fact", "version": "1.3",
            "fact": { "id": 7368975833,"content": "Ada Lovelace was the first programmer, ever." }}' \
    --header "Content-Type:application/json" \
    -XPOST /http://localhost:8400/webhooks/XoLZfgK/
The top level attribute source is a reserved field, and may be overwritten by Jenkins when processing webhook events.

Now you need a job on a master that is triggered when this webhook is called, but first you must enable notifications on both Operations Center and your Master so the job can be triggered with the incoming notifications.

Enabling notifications on both Operations Center and the Master

To enable notifications on both Operation Center and your Master:

  1. From the Operations Center dashboard, select Manage Jenkins > Configure Notifications.

  2. Select the Enabled checkbox.

  3. Select Operations Center Messaging as the Notification Router Implementation.

    enable notifications on the Notification Configuration page
  4. Click Save.

Creating a pipeline job that is triggered by a webhook

Then create a pipeline job on the master and use the following script in your pipeline:

pipeline {
	agent none
	triggers {
    		eventTrigger jmespathQuery("source=='homemade'")
		}
	stages {
    	stage('First stage') {
        	steps {
            	script {
                	echo "Build triggered by:" + currentBuild.getBuildCauses()[0].toString()
                	def cause = currentBuild.getBuildCauses()[0];
               // We check that the job is triggered by event, to avoid NullPointerException when using the json content.
                	if ( cause._class.contains("EventTriggerCause") ) {
                    	echo "Job triggered by event"
                    	//The json payload is enclosed in an event field
                  	  def eventContent = cause.event
                    	echo eventContent.fact.toString()
                    	//You can now use any json field send by the cURL command
                    	def fact = eventContent.fact.content
                    	echo "Fact is:" + fact.toString()
                	}
                	else if ( cause._class.contains("UserIdCause") ) {
                    	// The job can still be run manually, you may want to have different behavior.
                    	echo "Job triggered by user"
                	}
                	else {
                    	echo "Job triggered by something else"
                	}
            	}
          }
    	}
  }
}

Run the pipeline manually to validate the syntax, using the cURL command above, and the job should be triggered in few seconds.

Sample console output for a job triggered by a webhook:

Triggered by event
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (First stage)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Build triggered by:{"_class":"com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause","event":{"my_source":"homemade","application":"real-fact","version":"1.3","fact":{"id":7368975833,"content":"Ada Lovelace was the first programmer, ever."}},"shortDescription":"Triggered by event"}
[Pipeline] echo
Job triggered by event
[Pipeline] echo
{"id":7368975833,"content":"Ada Lovelace was the first programmer, ever."}
[Pipeline] echo
Fact is:Ada Lovelace was the first programmer, ever.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
The event propagation is not immediate; you have to wait a few seconds.
When the JSON payload contains a field with a null value, Core Products remove the field in the JSON displayed on pipelines. So, if you send an event like: curl -vvv \ --data '{"my_source: "homemade","application":null}' \ --header "Content-Type:application/json"\ -XPOST /http://localhost:8400/webhooks/XoLZfgK/ You will only have access to {"my_source": "homemade"} in the pipeline script.

Using a publishing pipeline to trigger a job

You can also use a publishing pipeline to trigger a job. The Cause class will also be EventTriggerCause, so it will execute the same trigger as the webhook. You can use a field, like “application,” to distinguish the publishing pipeline from the webhook.

pipeline {
	agent none
	stages {
    	stage('First stage') {
        	steps {
            	publishEvent event:jsonEvent('{"my_source": "homemade", "application": "real-fact-job", "version": "1.4", "fact": { "id": 7368975833,"content": "Margaret Hamilton programmed the on-board flight software for Apollo missions." }}'), verbose: true

        	}
    	}
	}
}

Sample console output from a build triggered by a job with publishEvent event:jsonEvent:

Triggered by event
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (First stage)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Build triggered by:{"_class":"com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause","event":{"my_source":"homemade","application":"real-fact-job","version":"1.4","fact":{"id":7368975833,"content":"Margaret Hamilton programmed the on-board flight software for Apollo missions."},"source":{"type":"JenkinsBuild","buildInfo":{"build":32,"job":"send-simple-event","jenkinsUrl":"http://localhost:8200/","instanceId":"09e3666bde9f5fa8a82c3fc889406c4f"}}},"shortDescription":"Triggered by event"}
[Pipeline] echo
Job triggered by event
[Pipeline] echo
{"id":7368975833,"content":"Margaret Hamilton programmed the on-board flight software for Apollo missions."}
[Pipeline] echo
Fact is:Margaret Hamilton programmed the on-board flight software for Apollo missions.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
In the sample log output you can see “source” is a reserved field for notification created by publishEvent and is automatically filled with data.