David Jacob
David Jacob

Reputation: 173

Using Slack to start Jenkins builds

we're about to wipe our Mac Mini and reinstall new XCode and Jenkins from scratch. We have just recently started using Slack and we would like to use it to receive Jenkins build notifications and to start Jenkins builds.

So Slack notifications, no problem. All working.

My problem comes when I am trying to start builds using Slack.

This tutorial (http://sonnguyen.ws/how-to-trigger-a-jenkins-build-from-slack/) explains a way to use Slack's slash commands. However our Mac Mini is on our internal network. Up until this point we have being using Skype to automatically build with Jenkins using this sevabot.

With Slack, the popular choice seems to be to use the slash commands but.... I still have some questions since it's my first time setting this stuff up:

  1. Should we use the slash commands approach and have some sort of leader/follower approach as described here: http://i-phone-dev.blogspot.de/2013/07/jenkin-build-xcode-project-in-mac-slave.html
  2. Is there a more sevabot-style approach using the Slack Outgoing Webhooks or Slackbot functionality?

Just looking for some guidance on ease of implementation, scalability, maintainability, etc for this stuff and some sample setups that people have had success with.

Thanks.

Upvotes: 6

Views: 10513

Answers (1)

David Jacob
David Jacob

Reputation: 173

So I finally managed to do it using Slack's slash commands, strings parsing and this answer: How to set environment variables in Jenkins?

Jenkins plugins I used:

  1. pre-scm-build
  2. EnvInject Plugin
  3. Build Authorization Token Root Plugin

I configured my Slack slash command to call https://my-jenkins-server//buildByToken/buildWithParameters?job=JOB_NAME&token=MY_SECRET_TOKEN. So when I type '/build_my_app branch_name project_param2', the text I type after my slash command gets sent over as on URL parameter ('branch_name project_param2' in this case).

Once it gets to the other side I need to split this 'text' parameter up into multiple environment variables that Jenkins can use for the build process. To achieve this I add two pre-scm build steps.

Pre-scm build step 1 is an 'Execute shell' command which consists of the following script:

build_parameters=($text)

echo BRANCH=${build_parameters[0]} > props
echo PARAM2=${build_parameters[1]} >> props

'props' is a file I use to store the parsed strings, as trying to set the environment variables in this step using 'export' fails as the env variables do not persist when the build actually starts. This script stores the $text parameter in a string array (split by space characters), and they I just reference each index of the array to get my desired variable. Need a new job parameter? Just add another line here.

Pre-scm build step 2 is an 'Inject Environment Variables' job and in 'Properties File Path' I simply specify '$WORKSPACE/props'. This then reads the file with my parsed env variables and injects them into the job environment.

So yeah, no having to create any webapps to manually parse the 'text' parameter and then call a different URL with all the parameters set separately in the URL. No having to make a million different Jenkins jobs with each possible combination of parameter.

Hope this saves someone some time!

Upvotes: 8

Related Questions