Unity Cloud Build (UCB) has a feature “Post-Build Script” where you can execute shell script file. The script can run npm to install firebase-cli, and you can deploy your ipa/apk to Firebase using the cli.

Summary

In this article, we configure Unity Cloud Build to

  • install firebase cli with npm
  • authenticate with Firebase by a token obtained by firebase login:ci
  • set up scripts deploy ipa/apk to Firebase App Distribution

Environment

  • Unity Cloud Build
  • Unity 2019.2.11f1
    • w/ Unity Personal License
  • Firebase Unity SDK 6.6.0
  • npm 6.11.3
  • firebase-tools 7.6.2

Steps

Configure Firebase project

First of all, you have to create Firebase project and install Firebase SDK into your Unity project. There is detailed guide on Firebase document & console.

Install firebase-tools with npm

To deploy you app to Firebase App Distribution, you have to use firebase cli. There are several ways to do this. In order to run it on CI, I assume using npm is the easiest and reliable way because we can specify the version of the cli.

The final package.json file should be like following.

I’ll explain about "scripts" property later, so let’s get into the detail then. BTW, “burger-kingdom” is a project I’m currently working on :)

{
  "name": "burger-kingdom",
  "scripts": {
    "authenticate": "$(npm bin)/firebase login:ci",
    "distribute": "$(npm bin)/firebase appdistribution:distribute \"$FIREBASE_BUILD\" --token \"$FIREBASE_TOKEN\" --app \"$FIREBASE_APP\" --groups \"$FIREBASE_GROUPS\""
  },
  "dependencies": {
    "firebase-tools": "^7.6.2"
  }
}

After you run npm install as usual, firebase cli binary is installed in $(npm bin). Therefore you can run $(npm bin)/firebase --version to check out the version.

npm install
$(npm bin)/firebase --version
# => 7.6.2

Fortunately, Unity Cloud Build uses machine instances with npm pre-installed. That means we don’t have to begin with installing it everytime we run the build.

Get Firebase authentication token

Firebase allows token based authentication for CI. To obtain a token, I included authenticate script into the package.json that wraps firebase cli.

Hit the command and follow the instructions. You have to sign in to Firebase with your Google account, and you’ll get the token printed on stdout. Make sure to copy the token.

npm run authenticate
✔  Success! Use this token to login on a CI server:

1//0e3nZesZ ...

Example: firebase deploy --token "$FIREBASE_TOKEN"

There are a little more explanation about authentication with firebase login:ci on the document. Check it up if you want.

Configure Unity Cloud Build project

Set up Unity Cloud Build (UCB) project for iOS or Android. If you are new to UCB or any kind of CI, the tutorial by Unity might help.

Configure Post-Build script

UCB has a feature named “Post-Build Script” where you can specify a path to a shell script you want to run after the build.

Our package.json contains distribute script that deploys the built ipa/apk. Again, it’s a thin wrapper of firebase cli, and there are some variables you have to provide.

// package.json (partial)

{
  "scripts": {
    "distribute": "$(npm bin)/firebase appdistribution:distribute \"$FIREBASE_BUILD\" --token \"$FIREBASE_TOKEN\" --app \"$FIREBASE_APP\" --groups \"$FIREBASE_GROUPS\""
  }
}
  • FIREBASE_BUILD is the path to the built ipa/apk. We’ll set it by script later.

  • FIREBASE_TOKEN is the authentication token you’ve gotten on the former step. Set it as an env variable in “Environment Variables” section of UCB. UCB doesn’t have a feature to store “Secret” variables which is common in some other CI services. Though, that should not matter much as long as you keep the UCB project private.

  • FIREBASE_APP is the id of the Firebase app, and FIREBASE_GROUPS is the names of tester groups separated by ",". You can get and set them on Firebase console.

If you want more info about the command and variables, see the document.

The last piece that ties the all together is a shell script file. Create a file at .ci/post_build.sh (or somewhere you want) and add some scripts on it as following.

#!/bin/sh

set -x

export FIREBASE_BUILD="$(find -E . -regex '.*\.(ipa|apk)' -print -quit)"
if [ -z "$FIREBASE_BUILD" ]; then
  echo "Could not find .ipa/.apk file"
  exit 1
fi

npm install
npm run distribute

And fill up “Post-Build Script” field on “Advanced Options” with .ci/post_build.sh in your UCB project.

UCB Advanced Options settings

FYI, don’t forget to commit the files; package.json, package-lock.json and .ci/post_build.sh on git, and push the commit on the branch to build :)

Conclusion

Unity Cloud Build (UCB) provides easily configurable CI for Unity iOS/Android projects. And with the “Post-Build Script” hook, we can deploy the app to Firebase App Distribution. We used npm to install firebase cli because it’s a suitable way on CI and fortunately UCB supports npm by default.

Firebase App Distribution is still beta in Oct. 2019. However the API might change, I wish the basic strategy on this article may help.

References

  • https://unity3d.com/unity/features/cloud-build
  • https://firebase.google.com/docs/unity/setup
  • https://firebase.google.com/docs/cli
  • https://firebase.google.com/docs/app-distribution/ios/distribute-cli