A super easy configuration guide to triggering Jenkins pipelines on Artifactory events
Had enough of sluggish polling? With instant Artifactory event triggers you can give responsiveness in Jenkins a real boost. Here’s an easy way to set it up.
Polling is to responsiveness what a ball and chain is to an Olympic sprinter. I like a responsive build system. Rather than sit around for an arbitrary amount of time waiting to start a build, I’d much rather have my Jenkins pipeline kick off as soon as something happens on Artifactory, like when an artifact gets published, some property gets changed, or a build is promoted. Luckily, it turns out setting that up isn’t too complicated because all it needs is the configuration of two plugins to get along. In this post I’ll walk you through the setup, step by step.
This guide assumes you:
- Are running Artifactory Pro or pricier
- Have administrative access to Artifactory and Jenkins
- Are comfortable making changes to your Jenkins pipeline script
TL;DR
- Install the Artifactory Webhook plugin on Artifactory
- Configure the Artifactory Webhook plugin to publish events to Jenkins
- Install the Generic Webhook Trigger plugin on Jenkins
- Set up a pipeline that triggers when a relevant event is published
Configuring Artifactory
First, we’ll set up Artifactory to broadcast events to Jenkins. To do this we’ll use the Artifactory Webhook plugin.
Install the plugin by dropping the webhook.groovy file in your Artifactory’s $ARTIFACTORY_HOME/etc/plugins directory
.
Then, create a webhook.config.json
file in the same directory. This is where we’ll configure the plugin to broadcast events to our Jenkins instance.
Below, I’ve configured my plugin to publish an event to my Jenkins instance whenever an artifact is created. If you want to trigger on different kinds of events, check the Artifactory Webhook plugin’s README file for other supported events such as “artifact property changed” or “Docker tag created”.
webhook.config.json
{
"webhooks": {
"mywebhookname": {
"url": "https://localhost:8080/jenkins/generic-webhook-trigger/invoke",
"events": [
"storage.afterCreate"
]
}
}
}
Note that …/generic-webhook-trigger/invoke
is an endpoint exposed by the Generic Webhook Trigger, which we’ll set up in the next step.
Configuring Jenkins
Artifactory is making noise now, but no one’s listening. Let’s fix that by setting up the Generic Webhook Trigger plugin for Jenkins. Install the plugin through the Plugin Manager (Manage Jenkins -> Manage Plugins) or however else you manage plugin installations in your setup.
Now all that’s left is creating the pipeline. The Generic Webhook Trigger plugin allows you to set up a GenericTrigger in the triggers clause of your pipeline. I don’t want to trigger on every artifact publication, so I’ll make the trigger a little smarter. The GenericTrigger allows you to extract variables from the event payload and filter out irrelevant events based on their content. Below is an example of a trigger that checks the event and artifact name before triggering:
Jenkinsfile
pipeline {
//…
triggers {
GenericTrigger(
genericVariables: [
[key: 'ARTIFACT_NAME', value: '$.artifactory.webhook.data.name'],
[key: 'EVENT_NAME', value: '$.artifactory.webhook.event']
],
causeString: 'Triggered on $ARTIFACT_NAME',
regexpFilterText: '$EVENT_NAME-$ARTIFACT_NAME',
regexpFilterExpression: 'storage\.afterCreate-SuperPackage'
)
}
//…
}
You’ll have to trigger the pipeline once manually so the trigger can configure itself properly. After that you should be good to go!
Troubleshooting
If your pipeline isn’t triggering properly here are a few things that can come in handy:
Check if Jenkins is actually receiving your Artifactory events by setting up a Jenkins log. Navigate to the logs menu (Manage Jenkins -> System Log), set up a new recorder (Add new log recorder), name it what you will and add org.jenkinsci.plugins.gwt
to its loggers. If Artifactory is configured correctly, and the spirits of enterprise networking are kind, you should be seeing your events roll in.
Next, check if you didn’t mess up your extracted variables. Take a JSON payload over to this interactive JSON path evaluator to verify you set up your variables properly.
Lastly, check you didn’t mess up your filtering RegEx over at RegExr, as it can happen to the best of us.
Conclusion
After configuring the Artifactory Webhook plugin and the Jenkins Generic Webhook Trigger properly, you should be able to trigger your Jenkins pipelines on all kinds of Artifactory events. Enjoy!
Published: Apr 24, 2020
Updated: Mar 26, 2024