Build Deploy Pipeline / Editor Script / Unity

Unity3D + Gear VR Build/ Deployment Pipeline

March 23, 2017

Tags: , , , , , , , , , , , , , ,

We’re using ConstructVR to push changes to a Gear VR app for pre-release distribution. It’s nice, but at the moment we’re manually downloading the latest APK from Cloud and dropping it into our Construct project. Not the worst workflow, but it’s always good automate this kind of thing if possible.
This post will cover how to get deployment from cloud build working with a postbuild script. For comparison, it will also cover how to get a build and push to ConstructVR running on CircleCI.

Set up ConstructVR

Get the ConstructVR command
  • Make a local build of your project (if you don’t already have an apk ready)
  • Log in to ConstructVR
  • Drop the APK into the dashboard. The APK should be added to the list of apps available in the dashboard
  • Next to the APK in the list, click on Actions > Integrations
  • Copy the command on this page into a file called deploy_constructvr.sh
  • Save this in a file called deploy_constructvr.sh and make sure it’s executable (chmod +x)

Amend your deploy_constructvr.sh script to match the following (keep hold of your appid and apikey – you’ll need them in a bit):


outputfile=$1
apiKey=$2
appKey=$3
curl -L -X POST -F "file=@$outputfile" -F "apikey=$apiKey" -F "appId=$appId" https://www.constructvr.io/api/apps

Deploy from Cloud Build

I’m assuming you know how to get a build running on Cloud Build, so will cover just the post build step here. I had a nose around the build config on cloud build and couldn’t see any obvious options for deployment of the apk after the build so wrote a post build script to handle it:

Create a post process build script

Create BuildProcessor.cs in an editor folder in your Unity project.

Add the following function to your file:


[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
string withoutSpaces = pathToBuiltProject.Replace(' ', '_');
File.Copy(pathToBuiltProject, withoutSpaces);
var p = new Process();
p.StartInfo.FileName = "/bin/sh";
p.StartInfo.Arguments = string.Format("deploy_constructvr.sh {0} {1} {2}", withoutSpaces, yourconstructvrapikey, yourconstructvrappid);
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
UnityEngine.Debug.Log(p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
}

You’ll notice there’s a file copy in there. This is because the Cloud Build artifact has a space in the name and the curl script in deploy_constructvr.sh doesn’t like it. The replace also assumes the space is only in the file name.

That’s it – you should be able to run the Cloud build and see a new apk revision in your ConstructVR dashboard.

Build and Deploy CircleCI

I found a project (link at the bottom) that builds an ios project on CircleCi – I’ve amended that for android.

You’ll need :

  • A CircleCI account with OSX running
  • A ConstructVR account
  • A GitHub/BitBucket repository
  • A Unity3D pro license

Setting up Circle CI

Add a Project for the repository you want to build. This should be on the main page if you have no builds set up yet, or in the Projects section (from the left hand menu).
Once you’ve set up the build, click on the cog next to the project name and go to Build Environment. Select OSX. I started a trial for this test, but after that’s over you’ll unfortunately need to pay for the environment ($39 per month minimum package).

Setting up the build scripts

On top of you deploy_constructvr.sh file, you’ll need two extra config files an a post build script:

circle.yml


dependencies:
pre:
- brew install android-sdk
- export ANDROID_HOME=/usr/local/opt/android-sdk
- echo y | android update sdk --no-ui --all --filter "android-25,build-tools-25.0.2platform-tools,extra-android-m2repository,extra-google-m2repository"

override:
- curl -o Unity.pkg http://netstorage.unity3d.com/unity/3829d7f588f3/MacEditorInstaller/Unity-5.5.2f1.pkg
- curl -o Unity-Android.pkg http://netstorage.unity3d.com/unity/3829d7f588f3/MacEditorTargetInstaller/UnitySetup-Android-Support-for-Editor-5.5.2f1.pkg
- sudo installer -dumplog -package Unity.pkg -target /
- sudo installer -dumplog -verbose -package Unity-Android.pkg -target /
- ./build.sh test-app.apk $constructvrapikey $constructvrappid

test:
override:
- echo nope

deployment:
develop:
branch: master
commands:
- ./deploy_constructvr.sh

build.sh


project="Simple Mobile Placeholder"

echo "Attempting to build $project for android"
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -silent-crashes -serial "$serial" -username "$username" -password "$password" -logFile $(pwd)/unity.log -projectPath $(pwd) -executeMethod BuildProcessor.DevPreProcessor -quit

echo 'Logs from build'
cat $(pwd)/unity.log

echo "returning the license"

/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -returnlicense -silent-crashes -serial "$serial" -username "$username" -password "$password" -logFile $(pwd)/unity.log -projectPath $(pwd) -quit

echo 'Logs from build returning license'
cat $(pwd)/unity.log

Post build script

Add the following function to a file called BuildProcessor.cs. Be sure to place it in an Editor folder.


private static void DevPreProcessor()
{
EditorPrefs.SetString("AndroidSdkRoot", EditorPrefs.GetString("AndroidSdkRoot"));
EditorUserBuildSettings.selectedBuildTargetGroup = BuildTargetGroup.Android;
PlayerSettings.virtualRealitySupported = true;
AssetDatabase.SaveAssets();
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, string.Format("test-app.apk", buildId), BuildTarget.Android, BuildOptions.None);
}

Add Environment variables
  • constructvrapikey Your construct vr api key
  • constructvrappid Your construct vr app id
  • username Your unity username
  • password Your unity password
  • serial Your unity serial number (in the email you received from Unity when you accepted the license seat)

That’s it! Your circleci build should now output an apk and push to constructvr.
Please note that the build.sh script is downloading and installing unity and the android build support packages – this will currently happen every time, so the script needs some cleaning before being used in an actual build pipeline.

Positives of using CircleCI
  • Circle CI UI is much cleaner and faster to use than Cloud build (IMO), and no more wait times!
  • CircleCI is much more powerful than Cloud Build
  • No manual drag and drop to Construct VR
  • Automated build/release pipeline!
Negatives of using CircleCI
  • Unity license needs to be used – I’ve had to reactivate my license in Editor a couple of times.
  • Not set up for Unity by default, so not as many options (changing Unity version is much more work, for example)
  • CircleCI OSX license cost – Cloud Build is free
Resources

Thanks to the people who posted the following blogs/answers:
how to setup circleci and slack with constructvr
CircleCI ios build example

1 likes

Author

Your email address will not be published.