TRIGGER JENKINS JOB FROM SLACK

Prashant Bhatasana
AppGambit
Published in
3 min readMar 20, 2019

In my previous blog, I talked about how can we Integrate Jenkins with Slack Notifications.

In this article, I talk about how can we trigger Jenkins Job from slack.

As we all know, Slack is one of the most popular communication open-source tools.

Slack provides many custom Apps support But we are going to use one of the most used custom app called Slash Commands.

The Slash Commands allows us to create a custom command that can “hit” a URL / rest-fire with the GET / POST method. Well, with this feature we can create a / command that will access our Jenkins Job URL and will run a build on that job.

before that, On Jenkins side, we had to install the `Build Authorization Token Root` Plugin.

Build Authorization Token Root plugin will generate a URL (rest) along with a token that becomes the access key for our job URL.

Now we move to Jenkins and try to configure the Build Authorization Token Root plugin.

TRIGGER JENKINS JOB FROM SLACK

  1. Install Build Authorization Token Root Plugin on Jenkins.

For that, we need to Go on Manage Jenkins > Plugin Manager > search “Build Authorization Token Root

After installation On the job configuration screen, It will display Trigger builds remotely (e.g., from scripts) option under Build Triggers.

Well, the plugin will provide a URL (rest) which will be hit by the slack command.

Add a token (you can generate random token here) like “6081437572aGd6081437572”

Now it will trigger the following URL:

JENKINS_URL/job/DemoJob/build?token=TOKEN_NAME OR /buildWithParameters?token=TOKEN_NAME
Optionally append &cause=Cause+Text to provide text that will be included in the recorded build cause.

Next, we move to slack.

Login to slack, then go to apps menu> manage apps> search “slash commands” and click add.

After that click on add configuration button.

Here I set my command name as “/DemoBuild” but you can set any preferable name for your command and click on the “Add Slask Command Integration” button.

The most important thing you must select this Autocomplete help text so the user can get the suggestion for your command.

Usage hint: Using this you can pass extra parameters like &Action=Build_Job1 OR &Action=Build_job2.

Using this we can run multiple jobs with the same token by the specify the extra parameter.

🎊 🎉🤖🎊 🎉Wow! Now you can use your command and fire your Jenkins job remotely.

Goto slack and try to run your command /DemoBuild.

Now your command is displayed in suggestion also and once you send this command your Jenkins job is up and running on Jenkins.

Thank you for reading, if you have anything to add please send a response or add a note!

--

--

Prashant Bhatasana
AppGambit

AWS Community Builder | AWS Certified | Terraform Associate | DevOps Engineer, Love to work with #AWS #Terraform #Jenkins #Kubernetes #Docker #Ansible #Selenium