Triggering Jenkins Pipelines on Artifactory Events

Have you grown tired of sluggish polling? Do you want to give responsiveness in Jenkins a real boost? Look no further! In this guide, I will walk you through an easy way to set up...

Have you grown tired of sluggish polling? Do you want to give responsiveness in Jenkins a real boost? Look no further! In this guide, I will walk you through an easy way to set up instant Artifactory event triggers for your Jenkins pipelines.

Polling is to responsiveness what a ball and chain is to an Olympic sprinter. As developers, we crave a highly responsive build system. Instead of waiting for an arbitrary amount of time to start a build, wouldn't it be great if our Jenkins pipeline kicked off as soon as something happened on Artifactory? Whether it's an artifact getting published, a property being changed, or a build being promoted, we want instant action. Thankfully, setting up this functionality is not as complicated as it sounds. All you need is the configuration of two plugins, and I'll guide you through it step by step.

TL;DR

Before we dive into the details, here's a quick summary of the steps involved:

  • 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.

Now let's get into the nitty-gritty of configuring Artifactory.

Configuring Artifactory

To broadcast events from Artifactory to Jenkins, we'll use the Artifactory Webhook plugin. Start by installing the plugin. Simply drop the webhook.groovy file into your Artifactory's $ARTIFACTORY_HOME/etc/plugins directory.

Next, 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 is an example configuration where an event is published to Jenkins whenever an artifact is created. Feel free to explore the Artifactory Webhook plugin's README file for other supported events.

webhook.config.json

Note that .../generic-webhook-trigger/invoke is an endpoint exposed by the Generic Webhook Trigger plugin, which we'll set up in the next step.

Configuring Jenkins

Artifactory is making noise, 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 using your preferred method.

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. With the GenericTrigger, you can extract variables from the event payload and filter out irrelevant events based on their content. Here's an example of a trigger that checks the event and artifact name before triggering:

Remember to trigger the pipeline once manually so that 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 tips that can come in handy:

  1. Check if Jenkins is 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, and add org.jenkinsci.plugins.gwt to its loggers. If Artifactory is configured correctly, and the spirits of enterprise networking are kind, you should see your events roll in.

  2. Double-check your extracted variables. Use an interactive JSON path evaluator to verify that you've set up your variables properly.

  3. Verify your filtering RegEx at RegExr. Sometimes, even the best of us can make mistakes.

Conclusion

After successfully configuring the Artifactory Webhook plugin and the Jenkins Generic Webhook Trigger, you'll be able to trigger your Jenkins pipelines on various Artifactory events. Say goodbye to sluggish polling and embrace a highly responsive build system. Happy coding!

Jenkins Artifactory events Caption: Image source: Jenkins Artifactory events

1