Skip to content

Commit 436f46f

Browse files
authored
Use custom build script (attempt #2) (#83)
* Use custom build script * Fix events * Fix with -> env * Fix event * Fix event * Fix syntax * Fix * Fix description -> body * Fix cut * Fix grep * Fix output * Cancel concurrent test workflows * Fix? * Fix output * Fix output * Fix sha * Skip build if 'No build' appears in PR body * Test on push * Revert "Test on push" This reverts commit 05ecdeb. * Convert spaces to underscores * Fix grep
1 parent 53ddffa commit 436f46f

File tree

15 files changed

+287
-11
lines changed

15 files changed

+287
-11
lines changed

.github/workflows/build.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ on:
66
target_platform:
77
required: true
88
type: string
9+
initial_scene:
10+
required: true
11+
type: string
12+
branch_name:
13+
required: true
14+
type: string
15+
commit_hash:
16+
required: true
17+
type: string
918

1019
jobs:
1120
build:
@@ -29,6 +38,8 @@ jobs:
2938
buildName: 'Pop Pixie'
3039
targetPlatform: ${{ inputs.target_platform }}
3140
versioning: None
41+
buildMethod: DevelopmentBuild.Build
42+
customParameters: ${{ inputs.initial_scene && format('-initialScene {0}', inputs.initial_scene) || '' }} -branchName ${{ inputs.branch_name }} -commitHash ${{ inputs.commit_hash }}
3243

3344
- uses: actions/upload-artifact@v4
3445
with:

.github/workflows/dev.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
name: Development builds
22

3-
on: push
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, edited]
49

510
concurrency:
611
group: ${{ github.workflow }}-${{ github.ref }}
712
cancel-in-progress: true
813

914
jobs:
15+
prepare:
16+
name: 'Prepare'
17+
runs-on: ubuntu-latest
18+
if: ${{ !contains(github.event.pull_request.body, 'No build') }}
19+
steps:
20+
- id: initial_scene
21+
run: |
22+
initial_scene=$(echo $PR_DESCRIPTION | grep -Eo 'Initial scene: [A-Za-z ]+' | cut -b 16- | sed -e 's/ /_/g')
23+
echo "initial_scene=$initial_scene" | tee -a $GITHUB_OUTPUT
24+
env:
25+
PR_DESCRIPTION: ${{ github.event.pull_request.body }}
26+
outputs:
27+
initial_scene: ${{ steps.initial_scene.outputs.initial_scene }}
28+
1029
windows_build:
1130
name: 'Build for Windows'
31+
needs: prepare
1232
uses: ./.github/workflows/windows.yml
1333
secrets: inherit
34+
with:
35+
initial_scene: ${{ needs.prepare.outputs.initial_scene }}
36+
branch_name: ${{ github.head_ref || github.ref_name }}
37+
commit_hash: ${{ github.event.pull_request.head.sha || github.sha }}
1438

1539
macos_build:
1640
name: 'Build for macOS'
41+
needs: prepare
1742
uses: ./.github/workflows/macos.yml
1843
secrets: inherit
44+
with:
45+
initial_scene: ${{ needs.prepare.outputs.initial_scene }}
46+
branch_name: ${{ github.head_ref || github.ref_name }}
47+
commit_hash: ${{ github.event.pull_request.head.sha || github.sha }}
1948

2049
steam:
2150
name: 'Upload to Steam'

.github/workflows/macos.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,28 @@ name: Build and package macOS
1616
# 3. gh secret set APP_STORE_CONNECT_API_KEY
1717
# 4. base64 -i AuthKey_**********.p8 | gh secret set APP_STORE_CONNECT_PASSWORD
1818

19-
on: workflow_call
19+
on:
20+
workflow_call:
21+
inputs:
22+
initial_scene:
23+
required: true
24+
type: string
25+
branch_name:
26+
required: true
27+
type: string
28+
commit_hash:
29+
required: true
30+
type: string
2031

2132
jobs:
2233
build:
2334
name: 'Build for macOS'
2435
uses: ./.github/workflows/build.yml
2536
with:
2637
target_platform: StandaloneOSX
38+
initial_scene: ${{ inputs.initial_scene }}
39+
branch_name: ${{ inputs.branch_name }}
40+
commit_hash: ${{ inputs.commit_hash }}
2741
secrets: inherit
2842

2943
package_macos:

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: Test
22

33
on: push
44

5+
concurrency:
6+
group: ${{ github.workflow }}-${{ github.ref }}
7+
cancel-in-progress: true
8+
59
jobs:
610
test:
711
runs-on: ubuntu-latest

.github/workflows/windows.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
name: Build and package Windows
22

3-
on: workflow_call
3+
on:
4+
workflow_call:
5+
inputs:
6+
initial_scene:
7+
required: true
8+
type: string
9+
branch_name:
10+
required: true
11+
type: string
12+
commit_hash:
13+
required: true
14+
type: string
415

516
jobs:
617
build:
718
name: 'Build for Windows'
819
uses: ./.github/workflows/build.yml
920
with:
1021
target_platform: StandaloneWindows64
22+
initial_scene: ${{ inputs.initial_scene }}
23+
branch_name: ${{ inputs.branch_name }}
24+
commit_hash: ${{ inputs.commit_hash }}
1125
secrets: inherit
1226

1327
package_windows:

Assets/Editor/DevelopmentBuild.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2020-present GameCI, Anderbell Studios
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.IO;
28+
using System.Linq;
29+
using UnityEditor;
30+
using UnityEditor.Build.Reporting;
31+
32+
public static class DevelopmentBuild {
33+
private const string MetaDataPath = "Assets/src/BuildMetaData.cs";
34+
35+
public static void Build() {
36+
ParseCommandLineArguments(out Dictionary<string, string> options);
37+
38+
BuildTarget buildTarget = (BuildTarget) Enum.Parse(
39+
typeof(BuildTarget),
40+
options["buildTarget"]
41+
);
42+
43+
List<string> scenes = EditorBuildSettings.scenes
44+
.Where(scene => scene.enabled)
45+
.Select(scene => scene.path)
46+
.ToList();
47+
48+
// Load a scene that isn't in build settings as the initial scene
49+
if (options.TryGetValue("initialScene", out string name)) {
50+
// Spaces cause issues with shell variables, so encode them as underscores
51+
string nameWithSpaces = name.Replace("_", " ");
52+
string initialScenePath = $"Assets/Unity/Scenes/{nameWithSpaces}.unity";
53+
scenes.Insert(0, initialScenePath);
54+
}
55+
56+
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions {
57+
scenes = scenes.ToArray(),
58+
locationPathName = options["customBuildPath"],
59+
target = buildTarget,
60+
options = UnityEditor.BuildOptions.Development
61+
};
62+
63+
BuildMetaData.SetBranchName(options["branchName"]);
64+
BuildMetaData.SetCommitHash(options["commitHash"]);
65+
66+
BuildSummary buildSummary;
67+
68+
try {
69+
buildSummary = BuildPipeline.BuildPlayer(buildPlayerOptions).summary;
70+
} finally {
71+
BuildMetaData.Reset();
72+
}
73+
74+
ReportSummary(buildSummary);
75+
ExitWithResult(buildSummary.result);
76+
}
77+
78+
private static void ParseCommandLineArguments(out Dictionary<string, string> providedArguments) {
79+
providedArguments = new Dictionary<string, string>();
80+
string[] args = Environment.GetCommandLineArgs();
81+
82+
// Extract flags with optional values
83+
for (int current = 0; current < args.Length; current++) {
84+
int next = current + 1;
85+
86+
// Ignore arguments not starting with '-'
87+
bool isFlag = args[current].StartsWith("-");
88+
if (!isFlag) continue;
89+
90+
string flag = args[current].TrimStart('-');
91+
92+
// Parse optional value
93+
bool flagHasValue = next < args.Length && !args[next].StartsWith("-");
94+
string value = flagHasValue ? args[next].TrimStart('-') : "";
95+
96+
Console.WriteLine($"Found flag \"{flag}\" with value \"{value}\".");
97+
providedArguments.Add(flag, value);
98+
}
99+
}
100+
101+
private static void ReportSummary(BuildSummary summary) {
102+
Console.WriteLine($"Duration: {summary.totalTime.ToString()}");
103+
Console.WriteLine($"Warnings: {summary.totalWarnings.ToString()}");
104+
Console.WriteLine($"Errors: {summary.totalErrors.ToString()}");
105+
Console.WriteLine($"Size: {summary.totalSize.ToString()} bytes");
106+
}
107+
108+
private static void ExitWithResult(BuildResult result) {
109+
switch (result) {
110+
case BuildResult.Succeeded:
111+
Console.WriteLine("Build succeeded!");
112+
EditorApplication.Exit(0);
113+
break;
114+
case BuildResult.Failed:
115+
Console.WriteLine("Build failed!");
116+
EditorApplication.Exit(101);
117+
break;
118+
case BuildResult.Cancelled:
119+
Console.WriteLine("Build cancelled!");
120+
EditorApplication.Exit(102);
121+
break;
122+
case BuildResult.Unknown:
123+
default:
124+
Console.WriteLine("Build result is unknown!");
125+
EditorApplication.Exit(103);
126+
break;
127+
}
128+
}
129+
}

Assets/Editor/DevelopmentBuild.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/branch_name.txt

Whitespace-only changes.

Assets/Resources/branch_name.txt.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/commit_hash.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)