This article addresses the issue of running bash scripts via C# IPreProcessBuildWithReport in Unity Build Automation (UBA). The problem arises due to file permissions and the execution method within a Linux-based UBA environment. The resolution involves modifying the C# script to call the shell interpreter explicitly and ensuring the script has execute permissions.
Symptoms:
You get the following exception:
Win32Exception: ApplicationName='/BUILD_PATH/.../build.sh', Native error= Access denied
- Bash scripts work locally but fail in UBA.
Cause:
The issue is caused by file permissions and the way shell scripts are executed in a Linux-based UBA environment. The scripts must have execute permissions and should be run through an interpreter such as bash.
Resolution:
Firstly, we suggest using the UBA's built-in script hooks for pre and post-build bash scripts.
Here is the recommended approach, which simplifies the build process and aligns it with UBA’s intended workflow:
- Keep the
build.shscript in your repository. - In the UBA Dashboard, navigate to your build target’s Advanced Settings.
- Enter the path to your script in the 'Pre-build script' path field (e.g., build.sh).
- Remove the
IPreprocessBuildWithReportC# script.
If, for some reason, the steps above don't work for your use case, here is what you can try:
1. Create a C# pre-build script with a RunExecutable method that differentiates between platforms:
private bool RunExecutable(string executablePath, string args, string workingDirectory) {
var process = new Process();
var workingDirectoryFullPath = Path.GetFullPath(workingDirectory);
try {
if (Application.platform == RuntimePlatform.WindowsEditor) {
process.StartInfo.FileName = executablePath;
process.StartInfo.Arguments = args;
} else {
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"\"{executablePath}\" {args}";
}
process.StartInfo.WorkingDirectory = workingDirectoryFullPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (_, e) => {
if (e.Data != null) {
LogHelper.Log($"RunCommand: {e.Data}", this);
}
};
process.ErrorDataReceived += (_, e) => {
if (e.Data != null) {
LogHelper.Error($"RunCommand: {e.Data}", this);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode == 0;
} catch (System.Exception ex) {
LogHelper.Error($"Failed to run command in '{workingDirectoryFullPath}'", this);
LogHelper.Exception(ex, this);
return false;
} finally {
process.Dispose();
}
}2. If you are using Git, ensure your build script is executable in the UBA environment:
git update-index --chmod=+x build.sh