How do I write to a text file in UCB?

Issue: I can’t save a text file during PostProcessBuild containing information I need for a post build bashscript. I am getting “ERROR: Access to the path “/BuildVersionInfo.txt” is denied.” I am trying to save to Application.dataPath. Is there a path I should be writing text to?

My code is simple

string outputPath = Path.Combine(Application.dataPath, "/BuildVersionInfo.txt");
File.WriteAllText(outputPath, cloudbuildVersionValue); //fails

Context: We use a bash script to run altool to upload our build to testflight, but I need the bundle short version (ex. 0.0.1) and I’d like to grab that from the project directly. We can get the UCB_BUILD_NUMBER for the bundle version, but I don’t know how else to get the bundle version string to my bash script.

I don’t think you need the forward slash in the parameter for your Path.Combine function, that could be confusing things because it tells the function to create a directory called BuildVersionInfo.txt. I’d also recommend doing a Debug.Log(Application.dataPath) and then checking your cloud build log file to see what the value is and just confirm that it’s correct. Let me know how you get on.

Use Application.persistentDataPath if you want to write data. And be aware the Path.Combine is there to add seperators as needed, i.e. don’t include them in directory or file names.

What happens here ist that, by starting with a slash, you tell the system that you start with a new absolute path. By definition, everything before is ignored and replaced with the new absoute path. So, essentially youre trying to write to /BuildVersionInfo.txt. Since the root directory is not writable on most systems, you get an “access denied” error. The error you posted also states exactly this (have a loook again where it tries to write).

As for your scenario: We do the same thing, but have come to the conclusion that using the short version throughout the app during building (and runtime) works best for us (you can always identify an individual build with Application.buildGUID, if you want or need to). Uploading is then done in the build pipeline when the build process has finished, not by Unity directly.

EDIT: typos and link