Deploy code by typing ‘/deploy_code_to_dev’ in a Slack channel.
Slack allows you to define slash commands, which will do a POST to a URL that you specify – so you can type ‘/deploy_code_to_dev’ in a Slack channel, and a POST request will trigger a build on Jenkins.
The Jenkins job is configured to run a Capistrano task as the build step – like ‘cap dev deploy’.
The job can be parameterised, so that the task name and target environment can be specified in the trigger URL.
First, install the “Slack Notification” plugins in Jenkins. This allows feedback from Jenkins into a Slack channel to let you know whether the task was successful.
Set up a build project in Jenkins, called something like ‘capistrano-build’. Flag it as parameterised, with ENVIRONMENT, TASK and PARAMS as parameters – these will be used in the capistrano call later.
Specify the git repo where your capistrano tasks are (note that we’re specifying the branch as $ENVIRONMENT, so that we can select a branch like dev, staging etc when we invoke the job):
Generate an authentication token to include with the call to the URL that triggers the build. I used a hex-encoded token to avoid any uncertainty about the encoding of URL parameters:
openssl rand -hex 32
Add a section for triggering the build remotely, using the token:
Set the build environment with a build number:
And configure the build step – this is where we’ll actually invoke the Capistrano command:
Now you can trigger the build by POSTing to the URL:
curl -X POST "https://build.knowmalaria.co.uk/job/capistrano-build/buildWithParameters?token=b069c41c2d82e9641602c198da09e8274f31032e1b9841e7528e39cf3c30d15d&ENVIRONMENT=dev&TASK=deploy"
Set the post-build actions to send some details to Slack – you’ll need to get the integration token from Slack
Now to trigger the build, we’ll add a Slash Command in Slack – these are user-defined commands that you can invoke by typing ‘/’ and your command name, and they can do some useful stuff like calling external URLs.
Add a new slash command, and configure it with a name like ‘/deploy_code_to_dev’ :
Set the Request URL to be the one that we used above in the curl request
Now just type ‘/deploy_code_to_dev’ from a Slack channel, and the slash command will post to the Jenkins build trigger. Results from the build will be posted back into the Slack channel that you configured.
To call a task that needs some extra parameters passed in, you can set up a slack command like: