How to Trigger a Bitrise build from HipChat using a Google Cloud Function

Brandon Tripp goes through setting up an app to use a curl command to trigger a Bitrise build from HipChat using a Firebase Cloud Function. Read his guide.

Set up an app to use a curl command to trigger a Bitrise build remotely from HipChat using a Firebase Cloud Function.

Guest blog post by Brandon Tripp, Lead Mobile Engineer at CareerBuilder. The original post appeared on Medium.

Brandon lives in Atlanta, Georgia. He is currently the Lead Mobile Engineer at CareerBuilder. He loves a challenge and is constantly looking to learn something new. He loves spending time with his family or playing Fortnite when he has time. He is open for work (contact).

The other day I had just finished setting up our CI/CD pipeline for CareerBuilders mobile apps using Bitrise. Before using Bitrise, a lot of my time was spent building and deploying our apps in order to test it or show internal stakeholders.

The Problem

I currently have multiple workflows setup on Bitrise that are setup to trigger when code is committed to our repository. This currently works fantastic and fits most of our needs. However, I am often asked to produce certain builds to show stakeholders. I use to produce these builds locally and push them wherever they needed to go. As you can imagine, this can become very tedious and time consuming. What if I am away from my computer? Or deep into the weeds on another branch and don’t feel like switching branches and context? After setting up our Bitrise workflows to do what I was doing manually, I needed a way to trigger them without pushing code. Bitrise has a way to start a build manually from their web page. After navigating to your project and clicking on “Start/Schedule Build” you are presented with a dialog box. This lets you configure a build and set certain parameters such as branch name, message, and workflow.

If you select the Advanced tab you are given even more customization such as the ability to schedule a build, insert custom environment variables, etc. If you scroll all the way to the bottom, you will be given a curl command to trigger this exact build. This is awesome… now I can use something else to trigger a build for me without navigating to Bitrise or committing code. For this, I chose to make a hipchat command that uses a Firebase Cloud Function to run the command below.


curl https://app.bitrise.io/app/asdf123/build/start.json — data ‘{“hook_info”:{“type”:”bitrise”,”api_token”:”123asdf”},”build_params”:{“branch”:”master”,”workflow_id”:”AndroidDeploy”},”triggered_by”:”curl”}’
Copy code

Let's Go.

Let's create our Cloud Function first. Go to Firebase and create a basic cloud function by following the directions. Once you have Node and the Firebase CLI setup, you can deploy a cloud function using the command “Firebase deploy” I have posted the code below so you can paste it into your cloud function and change the highlighted pieces. To get your tokens, click “Start/Schedule Build” in Bitrise and then click the Advanced tab. Scroll to the bottom and pull them out of your curl command. Once deployed, you will be given a url for your cloud function. Keep this url for the next step. Please don’t judge me for my node code below. It was written during a hackathon for a very specific use case.


const functions = require('firebase-functions');
const request = require("request");
exports.bitrisebot = functions.https.onRequest((req, response) => {
 var callingUser = req.body.item.message.from.name;
 var messageParts = req.body.item.message.message.split(' ');
if(messageParts[1] === "help"){
  var responseJSON = {
     "color": "green",
     "message": "/bitrise WORKFLOW_NAME BRANCH_NAME",
     "notify": false,
     "message_format": "text"
  };
return response.json(responseJSON);  
 }
var options = { method: 'POST',
   url: 'https://app.bitrise.io/app/YOUR_ID_GOES_HERE/build/start.json',
   headers: 
    { 'Content-Type': 'application/json' },
   body: 
    { hook_info: { type: 'bitrise', api_token: 'YOUR_TOKEN_GOES_HERE' },
      build_params: { branch: messageParts[2], workflow_id: messageParts[1] },
      triggered_by: 'curl' },
   json: true };
request(options, function (error, bitriseResponse, body) {
  console.log(body);
  if (error) throw new Error(error);
var responseJSON = {
     "color": "green",
     "message": "Build " + body.build_number + " Started\n Triggered Workflow: " + body.triggered_workflow + " \nView Build at: " + body.build_url,
     "notify": false,
     "message_format": "text"
  };
response.json(responseJSON);
 });
});
Copy code

Once you cloud function is deployed, create a new Hipchat Room to try this out in. This way, you can manage who can call this command by allowing certain people to join the room. (You can also limit people in your cloud function).

Once your room is created, add a new integration by clicking the dots in the upper right and clicking Integrations. Once the dialog pops up, press “Install new integrations”. Then click “Build your own integration”.

You will now be on the page that allows you to name your integration. I decided to name my integration “Deploy Bot”. Also on this screen, you will be given instructions on how to send messages to your room. What you need to do next is click on the checkbox that says “Extend hipchat with your commands” and give your command a name. I chose Bitrise as my command so I can type /bitrise to trigger my bot. Paste your cloud function URL from the above step into the highlighted field below and click save.

Let's use this Bad Boy!

Now that your cloud function is setup, go to your hipchat room and type in /InsertYourBotName help. If you set it up correctly, you should see the following message telling you how to use it.

Now, for the fireworks…. type your command name again and put your workflow name followed by your branch name with a space in between. You should see something similar to the picture below.

IT WORKS!!!!

Now you can click the link to view the build and you should be able to see that it is triggered.

DAYUM DAYUUUUM DAYYYUUUUUUUM!

You should be able to easily adapt this to be used with slack or aws. I just chose these because we were already using them for something else.

Now I can use this command to spit out builds left and right…. Even if I am away from my computer. This affords me the flexibility that we need at CareerBuilder. The next step for me with this is to post updates to the hipchat room about the builds current status.

Thanks to Vito Pistelli for helping with the Node Code and encouraging me to post this article.

No items found.
The Mobile DevOps Newsletter

Explore more topics

App development

Best practices from engineers on how to use Bitrise to build better apps, faster.

Community

Meet other Bitrise engineers, technology experts, power users, partners and join our BUGs.

Company

All the updates about Bitrise events, sponsorships, employees, and more.

Insights

Mobile development, latest tech, industry insights, and interviews with experts.

Mobile DevOps

Learn why mobile development is unique and requires a set of unique practices.

Releases

Stay tuned for the last updates, new features, and product improvements.

The Mobile DevOps Newsletter

Join 1000s of your peers. Sign up to receive Mobile DevOps tips, news, and best practice guides once every two weeks.