This article explains why Unity Build Automation (UBA) post-build scripts run even when a build fails and how to prevent uploading empty build artifacts in such cases. UBA always executes post-build scripts after the Unity Editor finishes in the builder, regardless of build success. There is no direct “build success” flag, but UBA exposes UNITY_PLAYER_PATH, which only exists on successful builds. By checking that path’s existence at the start of the post-build script, you can safely skip zipping and uploading when builds fail.
Symptoms
- Your post-build script runs even when the UBA build fails.
- An empty build ZIP is uploaded after a failed build.
- There is no obvious environment variable indicating whether the Player or an Addressables build succeeded.
- There is a desire to avoid iterating through directories to verify output files.
Cause
Unity Build Automation is designed to always execute a configured post-build script after the Unity Editor process terminates, just before artifact upload. It does not provide a built-in configuration or a boolean environment variable (such as BUILD_SUCCESS) to conditionally skip the post-build script based on build success or failure. As a result, the post-build script runs unconditionally, which can lead to zipping and uploading empty or missing build artifacts when a build fails.
Resolution
- Keep using the post-build script, but add an early existence check for the build artifact.
- Use the UNITY_PLAYER_PATH environment variable, which points to the expected Player build output on success.
- At the top of your post-build script, resolve UNITY_PLAYER_PATH and verify that the file or directory exists.
- If UNITY_PLAYER_PATH does not exist, exit the script before zipping or uploading artifacts.
-
Below is an example post-build bash script:
#!/bin/bash # 1. Grab the artifact path PLAYER_PATH="$UNITY_PLAYER_PATH" # 2. Convert Cygwin path to Windows format if using a Windows builder if [[ "$BUILDER_OS" == "WINDOWS" ]]; then PLAYER_PATH=$(cygpath -wa "$UNITY_PLAYER_PATH") fi # 3. Check if the artifact actually exists if [ ! -e "$PLAYER_PATH" ]; then echo "Build artifact not found at $PLAYER_PATH. The build likely failed." echo "Skipping zip and upload process." exit 0 # or exit 1 if you want the job to fail explicitly fi # 4. (Optional) Add similar checks for Addressables output paths # if [ ! -e "path/to/addressables/output" ]; then # echo "Addressables output not found. Skipping related steps." # exit 0 # fi echo "Build artifacts found! Proceeding with zipping and uploading..." # Execute your zipping/uploading logic here
- For Addressables (as shown above), add analogous checks for the known output directory or files.
This approach avoids directory-wide scanning and prevents uploading empty ZIPs when the build or Addressables step fails.
More Information
For more information about environment variables, please review this documentation section.