Run custom scripts during the build process
Follow this workflow to run custom scripts before and after the build process.
Read time 5 minutesLast updated 16 hours ago
Build Automation supports both pre build and post build custom scripts created by users. To use custom scripts, check these scripts into the source repository, and configure the relative path to the scripts from the root of the repository in the Advanced Settings of a build.
Prerequisites
The following reference pages give more context to this workflow:- A list of installed software.
- A list of available environment variables.
- An overview of the advanced build settings, including script hooks and when Unity Build Automation runs them.
Pre-export and post-export methods
Use the pre-export and post-export methods to trigger actions before and after your Unity project is built in the cloud. These methods must exist in classes in theAssets/EditorPre-export method name
To use a pre-export method, create a public static method in your Unity Project that contains the code you want to execute before the Unity Editor exports your project and after the Unity Editor script compilation phase.You can enable Build Automation to pass the build manifest of the current build to the pre-export method by specifying apublic static void PreExport()
BuildManifestObjectWhen Unity Build Automation calls the method, it passes apublic static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest)
BuildManifestObjectBuildManifestObjectPost-export method name
To use the post-export method, create a public static method in your Unity Project that contains the code you want executed after the Unity Editor exports your Project.When Unity Build Automation calls the method, it passes a string:public static void PostExport(string exportPath)
- For non-iOS build targets, the string contains the path to the exported Project.
- For iOS Projects, the string contains the path to the exported Xcode project. You can use the path to locate the exported Xcode Project to perform additional preprocessing before Xcode is called to complete the build process.
Custom scripting #define
directives
Using Build Automation, you can create custom scripting #define directives. On the Unity Dashboard, go to the build target’s Advanced settings.
In the Scripting Define Symbols field, you can add your own custom scripting #define directives to the built-in selection available. For each build target, enter the names of the symbols you want to define. You can then use these symbols as the conditions in #define#ifPre-build and post-build scripts
You need to reference your pre-build and post-build bash scripts in the build targets Advanced settings. You can use these scripts for custom pre and post processing:- UBA calls pre-build bash scripts after cloning the code in the build machine, and after restoring cache operations, but before UBA starts up Unity to run the build process.
- UBA calls post-build bash scripts after the Unity Editor process completes, but before UBA uploads the generated artifacts.
Change node versions
NVM is installed on the build machines to let you change to a different node version during the build process. The following sample script installs and switches to Node 16.13.0:#!/usr/bin/env bash ## Source profile . ~/.profile ## Install NVM modules and set version nvm install 16.13.0 nvm use 16.13.0 ## Output Node version used node -v exit 0
Use other Ruby versions
RVMgemDO#!/usr/bin/env bash ## Source profile rvm install ruby-3.3.0 rvm ruby-3.3.0 do gem install fastlane rvm ruby-3.3.0 do fastlane --help exit 0
rvm install ruby-3.3.0 --with-openssl-dir=$(brew --prefix openssl@1.1)
Set environment variables
To make an environment variable available to the Unity build and your post-build scripts, define or update the environment variable and write this value to theecho "{environment_variable_name}={value}" >> "$DEVOPS_ENV"
DEVOPS_ENVtestenvTESTENVExample of writing an environment variable to DEVOPS_ENV
In the pre-build script set the environment variable.
DEVOPS_ENVecho "TEST_ENV=Sample environment variable" >> $DEVOPS_ENV
Then in the post-build script echo the environment variable.
echo $TEST_ENV
Access build artifacts in a post-build script
You can access build artifacts for automatic upload to a storefront or to automate some part of the build process using a post-build script.Sample script to get the path to the player
#!/bin/bash #This is a sample that simply outputs the path to the build artifact. echo "START" PLAYER_PATH=$UNITY_PLAYER_PATH #If you use a Windows Builder and use the path to pass it to a native Windows application, you need to properly convert the cygwin player path to Windows format. if [[ "$BUILDER_OS" == "WINDOWS" ]]; then PLAYER_PATH=$(cygpath -wa "$UNITY_PLAYER_PATH") fi echo "$PLAYER_PATH"
Mask values in logs
Masking a value prevents a string or variable from being printed in the log. Each masked value is replaced with*****::mask-value::echo "::mask-value::{value}"
Example of masking a value
To mask the value"SuperS3cretPassword!"::mask-value::When this is printed in the log, the output will be displayed asecho "::mask-value::SuperS3cretPassword!"
"*****"Example of masking a variable
To mask the variableDB_PASSWORD"SuperS3cretPassword!"::mask-value::When either of these are printed to the log, the output will be displayed asDB_PASSWORD=SuperS3cretPassword! echo "::mask-value::$DB_PASSWORD"
"*****"Example of masking an environment variable
Environment variables set with$DEVOPS_ENV$DEVOPS_ENVDB_PASSWORD=SuperS3cretPassword! echo "::mask-value::$DB_PASSWORD" echo "DB_PASSWORD=$DB_PASSWORD" >> $DEVOPS_ENV