Automated Release From Unity Cloud Build To Google Play

Emre SOLAK
Dream Harvesters Team
4 min readNov 7, 2022

--

Automation is one of the most important things in the software industry. It reduces the possibility of errors, saves a huge amount of time and mental effort.

I’ve been using Unity Cloud Build since its very early days to automate my builds. The system developed much since its start. Along with many UI/UX improvements, scheduled builds, distributions to various game stores and some other features has been added.

Although some of the Android stores have been supported through Unity Distribution Portal, Google Play and Apple App Store requires manual scripting.

This article is about setting up automatic distribution for Google Play from Unity Cloud Build.

There’re some prerequisites to achieve this:

  1. You have to setup a Service Account on Google Cloud Platform
  2. Give access to this account on the Google Play Dashboard
  3. Setup a bash script to upload the aab with environment variables

I’ve go through these steps and explain:

Setting Up a Service Account

We’ll use fastlane to upload our build from the Unity Cloud Build which is already available on the virtual machine that builds our bundle. To be able to upload we need a service account so fastlane can access our developer account. You should see the service account as below once you set it up:

Google Play Dashboard with a proper service account setup

To setup a service account, follow the steps below:

  1. Go to Account Details on Google Play Console and get the Developer Account Id from there. We’ll use this id to double check that we’re setting up our service account for the correct project on Google Cloud Platform.
  2. Navigate to the Google Cloud Platform.
  3. You should see a screen like below. After confirming that your project id contains the Developer Account ID retrieved on the Google Play Console, navigate to the Service Accounts Page.

4. Use Create Service Account button. Follow the steps and fill in the required fields.

Select Service Account User as role while creating the account

Now you’ve completed this step. Go back to your Google Play Dashboard, Api Access Page.

5. Return to the Service Accounts page and press to the three dots button in the rightmost of your recently created service account. Select Manage Keys there.

6. Press Add Key button on the page, select JSON and proceed.

7. You’ll download a json file of which contents will be used in the last step.

Giving Access To Your Service Account On Google Play Dashboard

Once you create your service account, you should see it under the Api Access page of Google Play Dashboard.

Press Manage Play Console Permissions and setup the permissions.

Now we’re done with the Google Cloud Platform and Developer Console.

Let’s setup our project and Unity Cloud Build.

Setting Up Unity Cloud Build

  1. Create a bash script next to the Assets folder with the content below:
#!/bin/bash

# Passed in as environment variables from CI, you must get this from Google and put
# it in the environment variable PLAYSTORE_KEY in the Unity build config.
# $PLAYSTORE_KEY - The JSON file with the credentials for the Google Developer account

# You can also just hardcode your package name, e.g. com.candycrush.game or whatever here...
PACKAGE_NAME=$(cat "$WORKSPACE/build.json" | jq -j '.[].bundleid')

# Unity environment variables replace the "\n" signs from the private key with spaces for some reason,
# so we replaces spaces with "\n" signs again so it works properly.
KEY_WITH_NEWLINES=$(echo $PLAYSTORE_KEY | jq '.private_key |= sub(" (?!PRIVATE|KEY)"; "\n"; "g")' -c -j)

# You could also use shorter argument names here, but DO NOT use -e for --release-status, there's some error there where
# fastlane thinks -e should mean the -env option and fails.
# Also, you could put the "draft" and "internal" into environment variables if you want to never have to modify the script
# again and just control it with environment variables.
fastlane supply --package_name "$PACKAGE_NAME" --aab "$UNITY_PLAYER_PATH" --json_key_data "$KEY_WITH_NEWLINES" --release-status draft --track internal

Extension will be “sh”. I’d like to refer this file as “update.sh”.

2. Add this file to your repository

3. Navigate to the cloud build dashboard, to your intended project.

4. Navigate to the build configuration of your project and add this file as “Post-Build script” under the Advanced section.

5. Add an environment variable as “PLAYSTORE_KEY” and set its value as the content of the JSON file you downloaded from Google Cloud Platform.

That’s it!

Now start a build and it’ll uploaded to the Google Play Dashboard automatically.

--

--

Emre SOLAK
Dream Harvesters Team

Co-Founder @Teknodev & @DreamHarvesters / Senior Developer / Expert on Game Development and Project Management