From 60f5537e3b571df18c6f3f438613c19bcddbec7c Mon Sep 17 00:00:00 2001 From: Andreas Stange Date: Tue, 10 May 2022 19:44:41 +0200 Subject: [PATCH 1/4] option to manually run GitHub Actions --- .github/workflows/build-companion-app.yml | 1 + .github/workflows/build-main-game.yml | 1 + .github/workflows/test-companion-app.yml | 1 + .github/workflows/test-main-game.yml | 1 + 4 files changed, 4 insertions(+) diff --git a/.github/workflows/build-companion-app.yml b/.github/workflows/build-companion-app.yml index 0abb8d3be..738acdc2a 100644 --- a/.github/workflows/build-companion-app.yml +++ b/.github/workflows/build-companion-app.yml @@ -4,6 +4,7 @@ on: push: branches: - master + workflow_dispatch: {} env: UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} diff --git a/.github/workflows/build-main-game.yml b/.github/workflows/build-main-game.yml index ff8cb86c7..9adeb9ff8 100644 --- a/.github/workflows/build-main-game.yml +++ b/.github/workflows/build-main-game.yml @@ -4,6 +4,7 @@ on: push: branches: - master + workflow_dispatch: {} env: UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} diff --git a/.github/workflows/test-companion-app.yml b/.github/workflows/test-companion-app.yml index 1f5e31c67..84e0defe2 100644 --- a/.github/workflows/test-companion-app.yml +++ b/.github/workflows/test-companion-app.yml @@ -7,6 +7,7 @@ on: pull_request: branches: - master + workflow_dispatch: {} env: UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} diff --git a/.github/workflows/test-main-game.yml b/.github/workflows/test-main-game.yml index bec80c625..7a135c126 100644 --- a/.github/workflows/test-main-game.yml +++ b/.github/workflows/test-main-game.yml @@ -7,6 +7,7 @@ on: pull_request: branches: - master + workflow_dispatch: {} env: UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} From fe5584231ef59c671150f2ee8b989472ccc48884 Mon Sep 17 00:00:00 2001 From: Andreas Stange Date: Tue, 10 May 2022 20:13:54 +0200 Subject: [PATCH 2/4] get commit hash from file inside .git folder to avoid issue with Git command line interface --- .github/workflows/build-companion-app.yml | 1 - .github/workflows/build-main-game.yml | 1 - .github/workflows/test-companion-app.yml | 1 - .github/workflows/test-main-game.yml | 1 - .../playshared/Editor/BuildInfoGenerator.cs | 45 +++++++++++-------- .../Packages/playshared/Editor/GitUtils.cs | 40 +++++++++++++---- 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/.github/workflows/build-companion-app.yml b/.github/workflows/build-companion-app.yml index 738acdc2a..ae586307d 100644 --- a/.github/workflows/build-companion-app.yml +++ b/.github/workflows/build-companion-app.yml @@ -32,7 +32,6 @@ jobs: with: fetch-depth: 0 lfs: true - submodules: recursive - uses: actions/cache@v2 with: path: ${{ env.PROJECT_PATH }}/Library diff --git a/.github/workflows/build-main-game.yml b/.github/workflows/build-main-game.yml index 9adeb9ff8..bf9dc2bf3 100644 --- a/.github/workflows/build-main-game.yml +++ b/.github/workflows/build-main-game.yml @@ -32,7 +32,6 @@ jobs: with: fetch-depth: 0 lfs: true - submodules: recursive - uses: actions/cache@v2 with: path: ${{ env.PROJECT_PATH }}/Library diff --git a/.github/workflows/test-companion-app.yml b/.github/workflows/test-companion-app.yml index 84e0defe2..387d1f1de 100644 --- a/.github/workflows/test-companion-app.yml +++ b/.github/workflows/test-companion-app.yml @@ -31,7 +31,6 @@ jobs: with: fetch-depth: 0 lfs: true - submodules: recursive - uses: actions/cache@v2 with: path: ${{ env.PROJECT_PATH }}/Library diff --git a/.github/workflows/test-main-game.yml b/.github/workflows/test-main-game.yml index 7a135c126..9f359ace9 100644 --- a/.github/workflows/test-main-game.yml +++ b/.github/workflows/test-main-game.yml @@ -31,7 +31,6 @@ jobs: with: fetch-depth: 0 lfs: true - submodules: recursive - uses: actions/cache@v2 with: path: ${{ env.PROJECT_PATH }}/Library diff --git a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs index f1277656d..6fb7933ef 100644 --- a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs +++ b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs @@ -1,17 +1,19 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; +using UnityEngine; // Fills a file with build information before Unity performs the actual build. public class BuildInfoGenerator : IPreprocessBuildWithReport { - public static readonly string versionPropertyName = "release"; - public static readonly string timeStampPropertyName = "build_timestamp"; - public static readonly string commitShortHashPropertyName = "commit_hash"; - public static readonly string versionFile = "Assets/VERSION.txt"; + private static readonly string versionPropertyName = "release"; + private static readonly string timeStampPropertyName = "build_timestamp"; + private static readonly string commitShortHashPropertyName = "commit_hash"; + private static readonly string versionFile = "Assets/VERSION.txt"; public int callbackOrder { @@ -26,31 +28,36 @@ public void OnPreprocessBuild(BuildReport report) UpdateVersionFile(); } - private void UpdateVersionFile() + private static void UpdateVersionFile() { string timeStamp = DateTime.Now.ToString("yyMMddHHmm", CultureInfo.InvariantCulture); string commitShortHash = GitUtils.GetCurrentCommitShortHash(); - + + Dictionary propertyNameToValueMap = new(); + propertyNameToValueMap.Add(versionPropertyName, PlayerSettings.bundleVersion); + propertyNameToValueMap.Add(timeStampPropertyName, timeStamp); + propertyNameToValueMap.Add(commitShortHashPropertyName, commitShortHash); + + Debug.Log($"Updating {versionFile} with {JsonConverter.ToJson(propertyNameToValueMap)}"); + string[] versionFileLines = File.ReadAllLines(versionFile); for (int i = 0; i < versionFileLines.Length; i++) { string line = versionFileLines[i]; - if (line.StartsWith(versionPropertyName, true, CultureInfo.InvariantCulture)) - { - versionFileLines[i] = $"{versionPropertyName} = {PlayerSettings.bundleVersion}"; - } - - if (line.StartsWith(timeStampPropertyName, true, CultureInfo.InvariantCulture)) + propertyNameToValueMap.ForEach(entry => { - versionFileLines[i] = $"{timeStampPropertyName} = {timeStamp}"; - } - - if (line.StartsWith(commitShortHashPropertyName, true, CultureInfo.InvariantCulture)) - { - versionFileLines[i] = $"{commitShortHashPropertyName} = {commitShortHash}"; - } + string propertyName = entry.Key; + string propertyValue = entry.Value; + if (line.StartsWith(entry.Key, true, CultureInfo.InvariantCulture)) + { + versionFileLines[i] = $"{propertyName} = {propertyValue}"; + } + }); } File.WriteAllLines(versionFile, versionFileLines); + + Debug.Log($"New contents of {versionFile}:\n{versionFileLines}"); + // Unity needs a hint that this asset has changed. AssetDatabase.ImportAsset(versionFile); } diff --git a/UltraStar Play/Packages/playshared/Editor/GitUtils.cs b/UltraStar Play/Packages/playshared/Editor/GitUtils.cs index 5932c0c19..d8902cfc2 100644 --- a/UltraStar Play/Packages/playshared/Editor/GitUtils.cs +++ b/UltraStar Play/Packages/playshared/Editor/GitUtils.cs @@ -65,16 +65,40 @@ public static string RunGitCommand(string gitCommand, bool throwOnFailure = true public static string GetCurrentCommitShortHash() { - string result = RunGitCommand("rev-parse --short --verify HEAD", false); - if (result.IsNullOrEmpty()) + string currentDirectory = Directory.GetCurrentDirectory(); + string firstGitFolderPath = null; + do { - // Failed to get commit hash from Git. - return "???"; - } + ListDirectoryContent(currentDirectory); + DirectoryInfo parentDirectory = Directory.GetParent(currentDirectory); + currentDirectory = parentDirectory?.FullName; + if (firstGitFolderPath.IsNullOrEmpty() + && Directory.Exists(currentDirectory + "/.git")) + { + firstGitFolderPath = currentDirectory + "/.git"; + } + } while (!currentDirectory.IsNullOrEmpty() && Directory.Exists(currentDirectory)); + + // Get commit hash from .git/refs/heads/master + string commitHashFile = firstGitFolderPath + "/refs/heads/master"; + Debug.Log($"commitHashFile: {commitHashFile}"); + string commitHash = File.ReadAllText(commitHashFile); + Debug.Log($"commitHash: {commitHash}"); + string commitShortHash = commitHash[..8]; + Debug.Log($"commitShortHash: {commitShortHash}"); + + // Alternative: run git command + // string result = RunGitCommand("rev-parse --short --verify HEAD"); // Clean up whitespace around hash. (seems to just be the way this command returns :/ ) - result = string.Join("", result.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); - Debug.Log("Current commit short hash: " + result); - return result; + // result = string.Join("", result.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); + // Debug.Log("Current commit short hash: " + result); + return commitShortHash; + } + + private static void ListDirectoryContent(string path) + { + string[] allFolders = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); + allFolders.ForEach(folder => Debug.Log(folder)); } } From 2f4fcddc01ec3a27fc0bf19dc65cce90f47f8b01 Mon Sep 17 00:00:00 2001 From: Andreas Stange Date: Tue, 10 May 2022 21:07:15 +0200 Subject: [PATCH 3/4] use bundleVersion directly from ProjectSettings.asset because Unity's C# API returns an older value --- .../playshared/Editor/BuildInfoGenerator.cs | 19 +++++- .../Packages/playshared/Editor/GitUtils.cs | 63 ++++++++++++------- .../Util/Extensions/CollectionExtensions.cs | 7 ++- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs index 6fb7933ef..4fb39959e 100644 --- a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs +++ b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; @@ -30,11 +31,12 @@ public void OnPreprocessBuild(BuildReport report) private static void UpdateVersionFile() { + string bundleVersion = GetPlayerSettingsFileBundleVersion(); string timeStamp = DateTime.Now.ToString("yyMMddHHmm", CultureInfo.InvariantCulture); string commitShortHash = GitUtils.GetCurrentCommitShortHash(); Dictionary propertyNameToValueMap = new(); - propertyNameToValueMap.Add(versionPropertyName, PlayerSettings.bundleVersion); + propertyNameToValueMap.Add(versionPropertyName, bundleVersion); propertyNameToValueMap.Add(timeStampPropertyName, timeStamp); propertyNameToValueMap.Add(commitShortHashPropertyName, commitShortHash); @@ -56,9 +58,22 @@ private static void UpdateVersionFile() } File.WriteAllLines(versionFile, versionFileLines); - Debug.Log($"New contents of {versionFile}:\n{versionFileLines}"); + Debug.Log($"New contents of {versionFile}:\n{versionFileLines.ToCsv(", ")}"); // Unity needs a hint that this asset has changed. AssetDatabase.ImportAsset(versionFile); } + + private static string GetPlayerSettingsFileBundleVersion() + { + // Return the value from the file because the C# API (PlayerSettings.bundleVersion) returns an older value + // during the GitHub Actions build for whatever reason. + string[] projectSettingsAssetLines = File.ReadAllLines("ProjectSettings/ProjectSettings.asset"); + Debug.Log($"ProjectSettings.asset content:\n{projectSettingsAssetLines.JoinWith("\n")}"); + string bundleVersionLine = projectSettingsAssetLines.FirstOrDefault(line => line.Contains("bundleVersion:")); + Debug.Log($"bundleVersionLine: {bundleVersionLine}"); + string bundleVersion = bundleVersionLine.Replace("bundleVersion:", "").Trim(); + Debug.Log($"bundleVersion: {bundleVersion}"); + return bundleVersion; + } } diff --git a/UltraStar Play/Packages/playshared/Editor/GitUtils.cs b/UltraStar Play/Packages/playshared/Editor/GitUtils.cs index d8902cfc2..dee08b8e1 100644 --- a/UltraStar Play/Packages/playshared/Editor/GitUtils.cs +++ b/UltraStar Play/Packages/playshared/Editor/GitUtils.cs @@ -65,40 +65,57 @@ public static string RunGitCommand(string gitCommand, bool throwOnFailure = true public static string GetCurrentCommitShortHash() { - string currentDirectory = Directory.GetCurrentDirectory(); - string firstGitFolderPath = null; - do + return GetMasterBranchCommitShortHash(); + + // TODO: running Git command to get the commit hash of the current branch fails with "fatal: unsafe repository ('/github/workspace' is owned by someone else)" + // string result = RunGitCommand("rev-parse --short --verify HEAD"); + + // Clean up whitespace around hash. (seems to just be the way this command returns :/ ) + // commitShortHash = string.Join("", commitShortHash.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); + // Debug.Log("Current commit short hash: " + commitShortHash); + // return commitShortHash; + } + + private static string GetMasterBranchCommitShortHash() + { + string gitFolderPath = GetGitFolder(); + if (gitFolderPath.IsNullOrEmpty()) { - ListDirectoryContent(currentDirectory); - DirectoryInfo parentDirectory = Directory.GetParent(currentDirectory); - currentDirectory = parentDirectory?.FullName; - if (firstGitFolderPath.IsNullOrEmpty() - && Directory.Exists(currentDirectory + "/.git")) - { - firstGitFolderPath = currentDirectory + "/.git"; - } - } while (!currentDirectory.IsNullOrEmpty() && Directory.Exists(currentDirectory)); + throw new BuildFailedException("No .git folder found"); + } // Get commit hash from .git/refs/heads/master - string commitHashFile = firstGitFolderPath + "/refs/heads/master"; + string commitHashFile = gitFolderPath + "/refs/heads/master"; + if (!File.Exists(commitHashFile)) + { + throw new BuildFailedException($"No file with commit hash found inside .git folder (tried file path: {commitHashFile})"); + } + Debug.Log($"commitHashFile: {commitHashFile}"); string commitHash = File.ReadAllText(commitHashFile); Debug.Log($"commitHash: {commitHash}"); string commitShortHash = commitHash[..8]; Debug.Log($"commitShortHash: {commitShortHash}"); - - // Alternative: run git command - // string result = RunGitCommand("rev-parse --short --verify HEAD"); - - // Clean up whitespace around hash. (seems to just be the way this command returns :/ ) - // result = string.Join("", result.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); - // Debug.Log("Current commit short hash: " + result); return commitShortHash; } - private static void ListDirectoryContent(string path) + private static string GetGitFolder() { - string[] allFolders = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); - allFolders.ForEach(folder => Debug.Log(folder)); + Debug.Log("Searching .git folder"); + string currentDirectory = Directory.GetCurrentDirectory(); + do + { + Debug.Log($"currentDirectory: {currentDirectory}"); + if (Directory.Exists(currentDirectory + "/.git")) + { + string gitFolder = currentDirectory + "/.git"; + Debug.Log($"Found .git folder: {gitFolder}"); + return gitFolder; + } + DirectoryInfo parentDirectory = Directory.GetParent(currentDirectory); + currentDirectory = parentDirectory?.FullName; + } while (!currentDirectory.IsNullOrEmpty() && Directory.Exists(currentDirectory)); + + return null; } } diff --git a/UltraStar Play/Packages/playshared/Runtime/Util/Extensions/CollectionExtensions.cs b/UltraStar Play/Packages/playshared/Runtime/Util/Extensions/CollectionExtensions.cs index ebc190440..19aa0fea9 100644 --- a/UltraStar Play/Packages/playshared/Runtime/Util/Extensions/CollectionExtensions.cs +++ b/UltraStar Play/Packages/playshared/Runtime/Util/Extensions/CollectionExtensions.cs @@ -29,11 +29,16 @@ public static void ForEach(this IEnumerable enumerable, Action action) } } - public static string ToCsv(this IEnumerable enumerable, string separator = ",", string prefix = "[", string suffix = "]") + public static string JoinWith(this IEnumerable enumerable, string separator, string prefix = "", string suffix = "") { return prefix + string.Join(separator, enumerable) + suffix; } + public static string ToCsv(this IEnumerable enumerable, string separator = ",", string prefix = "[", string suffix = "]") + { + return enumerable.JoinWith(separator, prefix, suffix); + } + public static void AddIfNotContains(this ICollection collection, T item) { if (!collection.Contains(item)) From 027d8bb8f20e8f9b78d64bf1672ac9a4526c13f3 Mon Sep 17 00:00:00 2001 From: Andreas Stange Date: Wed, 11 May 2022 15:32:37 +0200 Subject: [PATCH 4/4] do not log content of ProjectSettings/ProjectSettings.asset in every build --- .../Packages/playshared/Editor/BuildInfoGenerator.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs index 4fb39959e..8fd7578e0 100644 --- a/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs +++ b/UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs @@ -69,11 +69,9 @@ private static string GetPlayerSettingsFileBundleVersion() // Return the value from the file because the C# API (PlayerSettings.bundleVersion) returns an older value // during the GitHub Actions build for whatever reason. string[] projectSettingsAssetLines = File.ReadAllLines("ProjectSettings/ProjectSettings.asset"); - Debug.Log($"ProjectSettings.asset content:\n{projectSettingsAssetLines.JoinWith("\n")}"); string bundleVersionLine = projectSettingsAssetLines.FirstOrDefault(line => line.Contains("bundleVersion:")); - Debug.Log($"bundleVersionLine: {bundleVersionLine}"); string bundleVersion = bundleVersionLine.Replace("bundleVersion:", "").Trim(); - Debug.Log($"bundleVersion: {bundleVersion}"); + Debug.Log($"bundleVersion from ProjectSettings/ProjectSettings.asset: {bundleVersion}"); return bundleVersion; } }