All Ruby on Rails Node JS Android iOS React Native Frontend

How to Trigger Bitrise Workflow from Github Using Google Cloud Function.

A few weeks ago, we were considering whether the current approach works for us and how we can improve the workflow. The previous pipeline has not worked well and we decided to create a new one from scratch. I will not be writing about the whole workflow, it is not a part of topic but I'm going to tell you about one specific step in our pipeline. To solve our problem we will use a single event which comes from Github when pull request has been approved. The event triggers workflow on Bitrise. Unfortunately, Bitrise doesn't allow you to recognize the distinctions between pull request update or pull request approval.

Problem

We used feature branch workflow. That means, a developer can create a branch for new feature and start working on it. When done, they create PR waiting for review and after review they merge the feature branch to master. There is one problem here - we keep on master the changes that might cause regression because nobody checked it before (QA). We don't want to create additional develop branch that allows us to keep the code stable on master branch because the issue will move from master to develop branch and won't disappear.

Bitrise

The solution comes with Google Cloud Function but before lets see what Bitrise brought. Bitrise has a way to start a build manually. After navigating to your project and clicking on Start/Schedule Build you will see a dialog. This lets you configure a build and set certain parameters such as branch name, message, and workflow. Take a look at the Advanced tab and scroll to the bottom.

 

 

Notice that we are able to perform curl command that runs our build configuration. We can use it and create function which does this automatically. Keep APP_ID, TOKEN and WORKFLOW name for the next step.

 

Google Cloud Function

Now we can move to Google Cloud Function console and select our existing project or create the other one following their guide. The dashboard contains list of cloud function, lets go then and create a basic cloud function. The details view allows you to choose runtime environment for the function. Below you can find a source code which you can deploy using Firebase CLI or just copy and paste to a cloud function dashboard.

var request = require('request');

exports.notifyApprove = (req, res) => {
  var payload = JSON.parse(req.body.payload)
  if(payload.action == 'submitted') {
    if(payload.review.state == 'approved') {
      handleApprove(res, payload.pull_request);
    } 
  } else {
  	res.status(200).send("");
  }
};

function handleApprove(res, pullRequest) {
  var branch = pullRequest.head.ref;
  var options = { 
    method: 'POST',
    url: 'https://app.bitrise.io/app/' + process.env.APP_ID + '/build/start.json',
    headers: { 'Content-Type': 'application/json' },
    body: {
      hook_info: { type: 'bitrise', api_token: process.env.TOKEN },
      build_params: { branch: branch, commit_message: 'Triggered by Bitrise Build URL', workflow_id: process.env.WORKFLOW },
      triggered_by: 'curl' 
    },
    json: true 
  };

  request(options, function (error, response, 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"
    };

    res.status(200).send(responseJSON);
  });
}

You don’t have to change anything. Notice the code contains custom environment variables that you need to add in the advanced settings of the function details.

 

The last step in Google Cloud Function is update package.json. We use request package and we have to add it as dependency.

{
  "name": "sample-http",
  "version": "0.0.1",
  
  "dependencies": {
    "request": "^2.x",
    "request-promise": "^1.x"
  }
}

Github integration

Now we can move to Github repository and setup a webhook on pull request review event. Lets go to: Setting -> Webhooks -> Add webhook.

 

Paste your cloud function URL from the above step into the highlighted fields below, select a checkbox to choose individual events.

  

  

Scroll to the bottom and ensure that you have selected only Pull request reviews and click save.

 

 

It’s time to use it

Now your function is setup and you can use it in your pull request. If everything went well, you should see that your build is running on Bitrise upon approval.

Let's make the world better for everyone - join us
New Call-to-action
READ ALSO FROM GoogleCloud
Read also
Need a successful project?
Estimate project or contact us