Chat-Ops: Setup iOS build from within slack or any other chat

One of my responsibilities as a DevOps engineer (I know it’s a weird term, if you have a better one, LMK) is to automate workflows for other engineers.

Recently, our iOS team has began doing builds using Bitrise.

Since we are using a chat room to communicate with each other, we automate everything around and inside the chat room. The server team has been using this for years now so the iOS team wanted to do the same.

Our chat room bot is based on the awesome hubot it’s name is gbot.

The requirement was to call gbot build ios master production

Basically, you name the branch you want to build and whether you want to build for production or test environment.

In Bitrise, it’s quite easy to define workflows, so the iOS engineers have set it up.

From there, it was a matter of simple Node code.

http = require "http"

module.exports = (robot) ->
  token = process.env.BITRISE_API_TOKEN
  app_id = process.env.BITRISE_APP_ID

  robot.respond /build ios (.*) (.*)/i, (msg) ->
    branch = msg.match[1].trim()
    env    = msg.match[2].trim()

    if env == "prod"
      workflow = "PROD"
    else
      workflow = "TEST"

    if branch == ""
      msg.reply "You need to tell me which branch you me want to build. (eg: master)"
      return

    if env == ""
      msg.reply "You need to tell me which workflow you me want to build. (eg: prod / test)"
      return

    params = {
      hook_info: {
        type: "bitrise",
        api_token: token,
      },
      build_params: {
        branch: branch
        workflow_id: workflow
      }
    }

    data="payload=#{encodeURIComponent(JSON.stringify(params))}"

    msg.http("https://bitrise.io/app/#{app_id}/build/start.json")
      .header("Content-Type","application/x-www-form-urlencoded")
      .post(data) (err, res, body) ->
        msg.reply "Building #{workflow} iOS. branch: #{branch}"

That’s it. Now the iOS team uses this many many times.

If you have questions or comments, as always, feel free to leave them here.