Quick question is there a way to stop the cloud build in a post build script.
We‘re incrementing the build number in a PostBuild method when building for iOS and if this process fails we want to stop the cloud build.
After the post build steps the xCode project would be builded and another script would then submit it to the app store, but if the build number couldn’t be incremented, we don’t want to have a state, where the version number is equal to a build that is already submitted.
The last paragraph is just for those who might be wondering why in the world anybody would want to stop a elsewhise perfectly executing build.
You could use a PreExport method in the Cloud Build Target Config to increment the build number. And it should never fail. But you could throw a BuildFailedException if it does.
It just adds the number of the Cloud Build from the Build History to the current build number that was initially (when this is implemented) on the App Store.
Every new cloud build should have an incremented # build number. For example if the current build number on the App Store is 100, and the Cloud Build Number is #10 (from the #10 - My-App-iOS-AppStore Build Target), you will get a 110 build number in the build settings.
111 for the next cloud build and so on…
Here is a snipped that should work:
private static int INITIAL_BUILD_NUMBER = 100; //the build number initially on the App Store
#if UNITY_CLOUD_BUILD
public static void IncrementBuildNumber(UnityEngine.CloudBuild.BuildManifestObject buildManifest)
{
// the cloudBuildNumber is the number of the Cloud Build in the Cloud Builds History
string cloudBuildNumber = buildManifest.GetValue("buildNumber", "0");
int cloudBuildNumberValue = 0;
if (int.TryParse(cloudBuildNumber, out cloudBuildNumberValue))
{
int finalBuildNumber = INITIAL_BUILD_NUMBER + cloudBuildNumberValue;
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
{
PlayerSettings.iOS.buildNumber = finalBuildNumber.ToString();
}
Debug.Log($"Build number incremented from {INITIAL_BUILD_NUMBER} to {finalBuildNumber}");
}
else
{
string errorMessage = "Build number was not incremented";
Debug.Log(errorMessage);
throw new BuildFailedException(errorMessage);
}
}
#endif
Your solution is quit elegant. Using the cloud build build number is way easier than our approach. We have Git as our version control system and now just create and push a commit, containing the increment of the build number, every time we build and submit to the app store. In our case the condition that should stop the cloud build would be a faiilure to commit the changes to the repo.
Will throwing a BuildFailedException stop the execution of the remaining CB?
Throwing a BuildFailedException should stop the Cloud Build, yes. Give it a try.
1 Like
Could you please give more detail on how to use that script ? I tried placing this script into Assets/Editor - but it doesn’t work. What am i missing ?
8261466–1081641–IncreaseBuildNumber.cs (1.29 KB)
The method in the script won’t be executed automatically. You have to specify preExport methods in the unity cloud build settings. One Google search will lead you to the documentation or another thread.
I saw documentation the problem is that it’s not clear to me - and every try (cloud build) takes about an hour 
https://docs.unity3d.com/Manual/UnityCloudBuildPreAndPostExportMethods.html
Can someone show me screenshot of working set up ?