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.
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.
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.
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.
Now you can click the link to view the build and you should be able to see that it is triggered.
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.