Story 

You are engineer, you use Jenkins for deployment? There will be time when you are outside or cannot touch your laptop but you need to deploy something to the system, access to Jenkins web interface on mobile is a nightmare. Luckily your company is using Slack for team communication. This might save you from the nightmare by having a Slack command to trigger remote build on Jenkins.


How?

The above story was what happened to me recently, at that time I thought there must be something that help me to do what I want. Started some search on Google about trigger build remotely using Slack command. There are some blog posts about this, some open source projects on github to accomplish this too. Finally I picked up jenkins-slack-command of @joshdholtz. It’s a project written in ruby and sinatra, pretty simple, it’s also very convenient to get started as the readme has a shortcut to create an app on heroku. Just need to follow the readme and you will have a working Slack command. Great work! 

However, there is risk when you just stop at what’s being offered by the app. Everyone who knows the command can deploy everytime, no restriction, no authorization… That’s obviously not what I want. In order to overcome this, I forked there project and modified a little bit. Find my forked repo at: https://github.com/khaiql/jenkins-slack-command

What is the difference?

I added one more environment variable called SLACK_CHANNEL_ID. It is the ID of the only channel that members of it can trigger the build. So, create a private channel, add all people who should be able to deploy to that one. But how can you find your Slack channel ID? I made it very easy by print out all params submitted to the app via Slack command. After having your heroku app up and running, the Slack command is also available to use. Execute the command in the channel that can trigger the build. You will see an error message saying that you haven’t setup the variable properly, with others params, find the channel ID from that. Now you know what to do, right? Simply get to setting page of your heroku app, add the missing environment variable and there you go 😁.
During setting up, you may encounter error. This is because your Jenkins is asking for a known account. Config it to make it buildable with a token. This will expose your jobs to others, but they cannot build, it’s not a big problem, I think 😁.

That’s all. Wonder where is the instruction? Find in the readme, very easy. Enjoy 😁

Scott