Building and uploading a non signed react-native apk to slack using github action 
Published: 2021-03-16 05:48:57

Building an apk takes huge amount of time and resource this can hog your local machine too much and make it a bit difficult to work on it. Also when working in react-native we can build a debug free apk without signing it, this comes in handy before rolling an authentic release. So to tackle the resource we use github action which uses a linux image to build our app for us and then send it to slack channel. For that add a devDependency @slack/bolt, and we move forward.

Github Action: 

```yaml

name: Push to slack channel
on:
  push:
    branches:
     - master
jobs:
  ship-pr:
    name: Push to slack
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: '14.x.x'
      - name: Build android apk and send to slack
        run: |
          yarn
          yarn package:android
          cd android && ./gradlew wrapper --gradle-version 5.5 && ./gradlew assembleRelease -x bundleReleaseJsAndAssets
          cd ..
          yarn shipit:android
        env:
          SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
          SLACK_SIGNING_SECRET: ${{ secrets.SLACK_SIGNING_SECRET }}

```

We use node 14 version as a package @slack/bolt (which will be used to send apk to slack) uses node version > 10.

In build step here yarn package:android is run which in returns packages react-native assets in android for us the corresponding script for this will be written in root package.json as : 

```json
"scripts": {
    "android": "react-native run-android",
     ......
    "test": "jest",
    "package:android": " react-native bundle --platform android --dev false --entry-file index.ts --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res",
  },

```

Currently the latest image of ubuntu and even macos use gradle version 6 or higher, this might be an issue for us as most of react native app 0.63.* version are mostly based on gradle 5, this is an issue as using gradle 6 or higher doesn't resolve dependencies issue for us. So we use gradle-version 5.5 and then apk is built and placed inside android/app/build/outputs/apk/release/app-release.apk

so we send this to our slack channel, to do that we can create a simple bit with file write and channel join access https://api.slack.com/apps?new_app=1 after giving the bot access we need to grab token which we receive after creating access to bot and signing secrets which is available in general information screen. We set that in github environments and then, yarn shipit:android command to send the file to slack channel, the corresponding script for this will be written in root as:

```json
"scripts": {
    "android": "react-native run-android",
     ......
    "package:android": ...,
    "build:android": "yarn && node ./shipit.js",
  },
```

Contents of shipit.js will be something as.

```js

// Require the Bolt for JavaScript package (github.com/slackapi/bolt)
const { App } = require("@slack/bolt");
// For Node.JS, you need to import the fs (file system) module
const { createReadStream } = require('fs');
const path = require('path');

const token = process.env.SLACK_TOKEN 
const slackBot = new App({
  token,
  signingSecret: process.env.SLACK_SIGNING_SECRET 
});


async function sendApk() {
  const fileName = path.resolve(__dirname, '..', 'android', 'app', 'build', 'outputs', 'apk', 'release','app-release.apk');

  try {
    // Call the files.upload method using the built-in WebClient
    const result =  await slackBot.client.files.upload({
      // The token you used to initialize your app is stored in the `context` object
      token,
      channels: '#general',
      initial_comment: "Here\'s new updated build :smile:",
      // Include your filename in a ReadStream here
      file: createReadStream(fileName)
    });
    console.log('Sucessfully sent')
  }
  catch (error) {
    console.error('oops: ', error);
  }

}

sendApk();


```

And in each trigger of our action apk is sent to slack, but be sure to mention bot to that channel first and is part of the channel