Unity Build Automation does not directly support uploading IPA files to distribution services like Apple App Store Connect. To achieve this, users must implement a post-build script utilizing 'xcrun altool'. That script can use either Apple credentials (username and app-generated password) or, preferably, API keys for authentication, as Apple no longer accepts plain text username/password combinations. This article also provides a C# script for automatically incrementing the build version number during the pre-export phase.
Symptoms
I created an IPA file in Unity Build Automation and need to upload it to a distribution service such as App Store Connect.
Cause
Build Automation does not have a way to instruct the platform to upload an IPA from the Dashboard to a distribution service.
Resolution
To upload your IPA to App Store Connect, you can use xrun altool from a post-build script such as the following:
#!/bin/bash
echo "Uploading IPA to App Store Connect..."
if xcrun altool --upload-app -f $UNITY_PLAYER_PATH -u $ITUNES_USERNAME -p $ITUNES_PASSWORD ; then
echo "Upload IPA to App Store Connect finished with success"
else
echo "Upload IPA to App Store Connect failed"
fi
These are its variables:
ITUNES_USERNAME - Your email
ITUNES_PASSWORD - The app-generated password
TARGET_NAME - Name of the target in Build Automation
UNITY_PLAYER_PATH - This variable is set by Build Automation
The post-build script was adapted from this forum post.
If you would like to increment the build version number automatically, you could use the following pre-export method:
using UnityEngine;
using UnityEditor;
using System;
public class AutoIncrementVersionCodeInBuildAutomation : MonoBehaviour
{
#if UNITY_CLOUD_BUILD
public static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest)
{
string buildNumber = manifest.GetValue("buildNumber", "0");
Debug.LogWarning("Setting build number to " + buildNumber);
PlayerSettings.Android.bundleVersionCode = int.Parse(buildNumber);
PlayerSettings.iOS.buildNumber = buildNumber;
}
#endif
}
Given that Apple is no longer accepting plain text username password combinations, and only accepting generated API tokens, you can implement the following post-build script, which uses the path to your API key file (.p8 file) and your issuer ID for the API key:
#!/bin/bash
echo "Uploading IPA to App Store Connect..."
KEY_WITH_NEWLINES=$(echo $CONNECT_API_KEY | jq '.private_key |= sub(" (?!PRIVATE|KEY)"; "\n"; "g")' -c -j)
echo $KEY_WITH_NEWLINES > ~/private_keys/YOUR_AUTH_KEY.p8
if [ ! -f ~/private_keys/YOUR_AUTH_KEY.p8 ]; then
echo "Key File not found!"
fi
# Assuming you have set API_ISSUER_ID environment variable
if [ -z "$API_ISSUER_ID" ]; then
echo "Error: API_ISSUER_ID is not set"
exit 1
fi
if xcrun altool --upload-app -type ios -f "$UNITY_PLAYER_PATH" --apiKey YOUR_API_KEY --apiIssuer "$API_ISSUER_ID"; then
echo "Upload IPA to App Store Connect finished with success"
else
echo "Upload IPA to App Store Connect failed"
fi