Triggering Bitrise Builds via Pull Request Label
Tags: mobile, devops, github, bitrise
Published: 2021-09-01 19:43:57

> Bitrise makes mobile deployment a breeze, but the options they provide for automatic build triggers are very all-or-nothing. In this post, we'll go over how to easily trigger Bitrise builds, _but only when we want to._

## Triggering Bitrise Builds

[Bitrise](https://www.bitrise.io) provides "fast, flexible, and scalable mobile CI/CD that just works." That's actually quite accurate! Creating and triggering build workflows is straightforward with Bitrise. 

Out of the box, it easily integrates with your Git provider. After integration, Bitrise uses webhooks to provide the following **automatic build triggers**:

- Trigger via push to a specific branch (or, ANY branch)
- Trigger via pull request (can filter by target or source branch)
- Trigger via specified git tag 

These are super helpful for many workflows, however our ideal trigger wasn't quite covered...

## Close But No Cigar 

At [CompanyCam](https://companycam.com), our ideal trigger would be _closest_ to the "Trigger via pull request" method, but we really didn't want _every single PR_ to trigger a build (we are paying for each of these builds, after all).

First off, certainly not all PRs would require our entire pipeline to run, especially if all that changed was the README! Other smaller changes could easily be tested on our nightly builds and wouldn't require an entire dedicated build. 

Secondly, filtering and triggering builds via a branch naming convention _could_ work, but would require developers to either:
  (a) have prior knowledge when _creating_ their branch that they would later want a Bitrise build for that branch, or 
  (b) rename/duplicate their branch as they encountered the need for a build (to match the triggering naming convention) 

Neither of these felt ideal.

We were looking for a way to enable our developers to _selectively choose_ whether their PR would or would not have a Bitrise build associated with it, at the moment they opened the PR (or even after!)

## Ad-Hoc Automation

The solution we sought was simple. We were looking for a way to add **"Trigger by PR Label"** functionality to connect our repository and Bitrise. How could we achieve this ad-hoc automation? 

Well, more on that in a second... but first, there's a hidden gem you should know before we go further (one that we'll use shortly):

While using the Bitrise web app, when you click "Start/Schedule a Build", navigate to the Advanced tab, and scroll to the bottom, you should find this handy-dandy, **copy-paste-ready cURL request** for triggering a build (with the specific options you added in the form above) - Slick!

![Example of cURL request provided by Bitrise](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2drwow8cl0vro60zyoxk.png)

## Putting Our Labels to Work

Okay, _now_ we can actually answer "How could we achieve this ad-hoc automation?" 

_Enter: GitHub Actions._

Along with our copy-paste-ready cURL request, and some simple logic to check for a label with the name "Build Bitrise" (code below), **we have a short and simple GitHub action that effectively acts as a "Trigger Build by PR Label" for our repository.**

✨ Now, any PR with any source or target or branch name can have an associated Bitrise build **simply by slapping a label on it!** 

Here's the entire GitHub Action we use: (with some secrets removed). 

{% gist https://gist.github.com/jasongaare/f17e10ea4bb89af0a04302248894c2f4 %}

_Happy Triggering!_

### About CompanyCam

:wave: Hi, we’re [CompanyCam](https://companycam.com). We create simple-to-use, visual-first communication and accountability tools for contractors.

We are _almost always_ hiring developers (we like React and Rails a lot). Check out [our careers page](https://companycam.com/careers/) to learn more.

### Post Script

You might have noticed, there are some extra params we added to the copy-paste-ready cURL request:

```sh
"build_params":{
  "branch":"${{ github.head_ref }}",
  "branch_dest": "${{ github.event.pull_request.base.ref }}",
  "workflow_id":"deploy-internal",
  "commit_hash":"${{ github.event.pull_request.head.sha }}",
  "pull_request_id": ${{ steps.pr_number.outputs.number }}
},
```

These params help better identify this PR in Bitrise, and perhaps most importantly help with associating future builds from the same PR to maintain the [rolling builds](https://devcenter.bitrise.io/builds/rolling-builds/) functionality provided by Bitrise 🎉


### Credits 

Cover Photo by [Dan-Cristian Pădureț](https://unsplash.com/@dancristianp) on [Unsplash](https://unsplash.com/s/photos/tag)