How to Trigger Bitrise Build from Bitbucket Pipeline?
Tags: javascript, devops, reactnative, challenge
Published: 2022-04-13 11:52:07

Hi fellow coders!
In my recent project, I felt the need to trigger Bitrise build from Bitbucket Pipeline. I hope the first question that comes in your mind would be what's the reason behind that.
so _Let's see..._

**Motivation:**
Let's suppose you have a project "abc" which contains the 3 different sub projects or folder in it.(e.g mobile, web, server)and you want to use same bitbucket repo for these 3 projects. But whenever you push some changes in web or server's folder it triggers the bitrise mobile's build which is not required.
Basically the reason behind that is whenever bitrise feels any change in bitbucket repo it starts automatic(trigger) build.
In bitrise Enable selective builds feature is not currently available for bitbucket repo.
_"**So our target is to run mobile build when you find changes in mobile's folder only.**"_

 **Solution:**
we are going to use Bitrise's api instead of automatic Bitrise’s triggers and we will hit Bitrise api endpoint through Bitbucket pipeline.

_**Let's start...**_
**Trigger Bitrise using Postman (optional):**
we are using postman here for testing bitrise api

- You can collect slug number from Url of bitrise(https://api.bitrise.io/v0.1/apps/$Slug_number/builds).
- Add Authentication token https://devcenter.bitrise.io/api/authentication/ in header of call.
- Add the following object in the body of request.

```
const data = {
    hook_info: {
      type: 'bitrise',
      triggered_by: '',
    },
    build_params: {
      branch: 'master',
      commit_message: "",
      commit_hash: "",
      workflow_id: 'primary',
    },
  };
//Now post the api
```
- Open bitbucket.yml file of your project in your editor.
- For testing purpose, lets first create a step for Mobile Build in Bitbucket pipeline using node image.

```
- step:
          name: Run Android Build
          image: node:10.15.3
          script:
            - echo "Going to trigger Android Build"
```

- If Pipeline run successfully after step 2, proceed further. If not, there is something wrong with yml file.
- We will apply Change set condition on step (the step should run only if we make changes in this folder) .

```
- step:
          name: Run Android Build
          image: node:10.15.3
          condition:
            changesets:
              includePaths:
                - "mobile/**"
          script:
            - echo "Going to trigger Android Build"
```

-  We will Create folder inside Project with name “MobileBuilds” .
- Inside the Folder we will add 2 files (Android.js ,Git.js).

- Now implement these steps here.
      - Get Latest git commit inside (git.js.https://dev.to/mehakfatima/how-to-get-last-git-commit-in-js-file-3il6)
      - Hit bitrise api inside the Android.js (https://dev.to/mehakfatima/trigger-bitrise-build-using-bitrise-api-350p)
- Invoke Android.js file from Bitbucket Pipeline. Also include required dependencies like axios and git-last-commit.

```
 - step:         
          name: Run Android Build
          image: node:10.15.3
          condition:
            changesets:
              includePaths:
                - "mobile/**"
          script:
            - echo "Going to trigger Android Build"
            - npm install axios
            - npm install git-last-commit
            - cd mobile/MobileBuilds
            - node Android.js $BITRISE_TOKEN
```

- $BITRISE_TOKEN is passing value  from Bitbucket’s variable to Android.js file.(https://dev.to/mehakfatima/can-we-pass-bitbucket-variables-to-js-file-hnk)
- Now just run the pipeline.