Skip to content

Commit

Permalink
Feature/windows upgrades (#588)
Browse files Browse the repository at this point in the history
- Allow updating container memory and cpu limits for Windows. Previously, they defaulted to 1cpu and 1gb ram which was far too low and it seems docker wouldn't allocate all available resources. Now it will use all available cores and 80% of system memory.
- Allow setting docker isolation mode for windows. Defaults to default to ensure behavior doesn't change from prior versions but now you can do stuff like force process mode on non-server versions which grants a performance uplift during runs
- Added logic to allow building Android on Windows. Android doesn't support burst when built on Linux, only on Windows and macOS. Thus we need to allow building Android on WIndows due to the major performance benefits of Burst.
- Support Windows 2022 and VS2022 by mounting the x64 Visual Studio path in addition to the x86 path to maintain compatibility with VS2019 and older
- Attempted fixes for windows builds hanging by killing the regsvr32 process after registering VS dll and using a different method to launch Unity. Unsure if this is a definite fix so I am leaving in several debug calls to print out running processes so we have more data to work with on chasing down this bug. I suspect there's a process that's hanging around that isn't cleaning itself up or is getting into some kind of deadlock situation and needs to be killed. But the changes I've made have seen no hangs on building during docker test workflows when previously there would be at least 3-5 hanging builds.
  • Loading branch information
AndrewKahr authored Oct 28, 2023
1 parent 6419c87 commit 4c4611c
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 52 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"configurations": [
{
"name": "PowerShell Launch Current File",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"cwd": "${cwd}"
},
{
"type": "node",
"request": "launch",
Expand Down
18 changes: 18 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ inputs:
default: ''
description:
'User and optionally group (user or user:group or uid:gid) to give ownership of the resulting build artifacts'
dockerCpuLimit:
required: false
default: ''
description: 'Number of CPU cores to assign the docker container. Defaults to all available cores on all platforms.'
dockerMemoryLimit:
required: false
default: ''
description:
'Amount of memory to assign the docker container. Defaults to 95% of total system memory rounded down to the
nearest megabyte on Linux and 80% on Windows. On unrecognized platforms, defaults to 75% of total system memory.
To manually specify a value, use the format <number><unit>, where unit is either m or g. ie: 512m = 512 megabytes'
dockerIsolationMode:
required: false
default: 'default'
description:
'Isolation mode to use for the docker container. Can be one of process, hyperv, or default. Default will pick the
default mode as described by Microsoft where server versions use process and desktop versions use hyperv. Only
applicable on Windows'
allowDirtyBuild:
required: false
default: ''
Expand Down
50 changes: 47 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

121 changes: 78 additions & 43 deletions dist/platforms/windows/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ else
Get-ChildItem -Path $Env:UNITY_PROJECT_PATH\Assets\Editor -Recurse
}

if ( "$Env:BUILD_TARGET" -eq "Android" -and -not ([string]::IsNullOrEmpty("$Env:ANDROID_KEYSTORE_BASE64")) )
{
Write-Output "Creating Android keystore."

# Write to consistent location as Windows Unity seems to have issues with pwd and can't find the keystore
$keystorePath = "C:/android.keystore"
[System.IO.File]::WriteAllBytes($keystorePath, [System.Convert]::FromBase64String($Env:ANDROID_KEYSTORE_BASE64))

# Ensure the project settings are pointed at the correct path
$unitySettingsPath = "$Env:UNITY_PROJECT_PATH\ProjectSettings\ProjectSettings.asset"
$fileContent = Get-Content -Path "$unitySettingsPath"
$fileContent = $fileContent -replace "AndroidKeystoreName:\s+.*", "AndroidKeystoreName: $keystorePath"
$fileContent | Set-Content -Path "$unitySettingsPath"

Write-Output "Created Android keystore."
}
else {
Write-Output "Not creating Android keystore."
}

#
# Pre-build debug information
#
Expand Down Expand Up @@ -112,48 +132,63 @@ Write-Output ""
# If $Env:CUSTOM_PARAMETERS contains spaces and is passed directly on the command line to Unity, powershell will wrap it
# in double quotes. To avoid this, parse $Env:CUSTOM_PARAMETERS into an array, while respecting any quotations within the string.
$_, $customParametersArray = Invoke-Expression('Write-Output -- "" ' + $Env:CUSTOM_PARAMETERS)
$unityArgs = @(
"-quit",
"-batchmode",
"-nographics",
"-silent-crashes",
"-projectPath", $Env:UNITY_PROJECT_PATH,
"-executeMethod", $Env:BUILD_METHOD,
"-buildTarget", $Env:BUILD_TARGET,
"-customBuildTarget", $Env:BUILD_TARGET,
"-customBuildPath", $Env:CUSTOM_BUILD_PATH,
"-buildVersion", $Env:VERSION,
"-androidVersionCode", $Env:ANDROID_VERSION_CODE,
"-androidKeystorePass", $Env:ANDROID_KEYSTORE_PASS,
"-androidKeyaliasName", $Env:ANDROID_KEYALIAS_NAME,
"-androidKeyaliasPass", $Env:ANDROID_KEYALIAS_PASS,
"-androidTargetSdkVersion", $Env:ANDROID_TARGET_SDK_VERSION,
"-androidExportType", $Env:ANDROID_EXPORT_TYPE,
"-androidSymbolType", $Env:ANDROID_SYMBOL_TYPE,
"-logfile", "-"
) + $customParametersArray

# Remove null items as that will fail the Start-Process call
$unityArgs = $unityArgs | Where-Object { $_ -ne $null }

$process = Start-Process -FilePath "C:\Program Files\Unity\Hub\Editor\$Env:UNITY_VERSION\Editor\Unity.exe" `
-ArgumentList $unityArgs `
-PassThru `
-NoNewWindow

while (!$process.HasExited) {
if ($process.HasExited) {
Get-Process

Start-Sleep -Seconds 10

Get-Process

# Display results
if ($process.ExitCode -eq 0)
{
Write-Output "Build Succeeded!!"
} else
{
Write-Output "$('Build failed, with exit code ')$($process.ExitCode)$('"')"
}

Write-Output ""
Write-Output "###########################"
Write-Output "# Build output #"
Write-Output "###########################"
Write-Output ""

Get-ChildItem $Env:BUILD_PATH_FULL
Write-Output ""

exit $process.ExitCode
}

& "C:\Program Files\Unity\Hub\Editor\$Env:UNITY_VERSION\Editor\Unity.exe" -quit -batchmode -nographics `
-projectPath $Env:UNITY_PROJECT_PATH `
-executeMethod $Env:BUILD_METHOD `
-buildTarget $Env:BUILD_TARGET `
-customBuildTarget $Env:BUILD_TARGET `
-customBuildPath $Env:CUSTOM_BUILD_PATH `
-buildVersion $Env:VERSION `
-androidVersionCode $Env:ANDROID_VERSION_CODE `
-androidKeystoreName $Env:ANDROID_KEYSTORE_NAME `
-androidKeystorePass $Env:ANDROID_KEYSTORE_PASS `
-androidKeyaliasName $Env:ANDROID_KEYALIAS_NAME `
-androidKeyaliasPass $Env:ANDROID_KEYALIAS_PASS `
-androidTargetSdkVersion $Env:ANDROID_TARGET_SDK_VERSION `
-androidExportType $Env:ANDROID_EXPORT_TYPE `
-androidSymbolType $Env:ANDROID_SYMBOL_TYPE `
$customParametersArray `
-logfile | Out-Host

# Catch exit code
$Env:BUILD_EXIT_CODE=$LastExitCode

# Display results
if ($Env:BUILD_EXIT_CODE -eq 0)
{
Write-Output "Build Succeeded!"
} else
{
Write-Output "$('Build failed, with exit code ')$($Env:BUILD_EXIT_CODE)$('"')"
Start-Sleep -Seconds 5
}

# TODO: Determine if we need to set permissions on any files

#
# Results
#

Write-Output ""
Write-Output "###########################"
Write-Output "# Build output #"
Write-Output "###########################"
Write-Output ""

Get-ChildItem $Env:BUILD_PATH_FULL
Write-Output ""
10 changes: 9 additions & 1 deletion dist/platforms/windows/entrypoint.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Get-Process

# Import any necessary registry keys, ie: location of windows 10 sdk
# No guarantee that there will be any necessary registry keys, ie: tvOS
Get-ChildItem -Path c:\regkeys -File | Foreach {reg import $_.fullname}
Get-ChildItem -Path c:\regkeys -File | ForEach-Object {reg import $_.fullname}

# Register the Visual Studio installation so Unity can find it
regsvr32 C:\ProgramData\Microsoft\VisualStudio\Setup\x64\Microsoft.VisualStudio.Setup.Configuration.Native.dll

# Kill the regsvr process
Get-Process -Name regsvr32 | ForEach-Object { Stop-Process -Id $_.Id -Force }

# Setup Git Credentials
& "c:\steps\set_gitcredential.ps1"

Expand All @@ -16,3 +21,6 @@ regsvr32 C:\ProgramData\Microsoft\VisualStudio\Setup\x64\Microsoft.VisualStudio.

# Free the seat for the activated license
& "c:\steps\return_license.ps1"

Start-Sleep 3
Get-Process
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ async function runMain() {
if (process.platform === 'darwin') {
MacBuilder.run(actionFolder);
} else {
await Docker.run(baseImage.toString(), { workspace, actionFolder, ...buildParameters });
await Docker.run(baseImage.toString(), {
workspace,
actionFolder,
...buildParameters,
});
}
} else {
await CloudRunner.run(buildParameters, baseImage.toString());
Expand All @@ -38,4 +42,5 @@ async function runMain() {
core.setFailed((error as Error).message);
}
}

runMain();
12 changes: 10 additions & 2 deletions src/model/build-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class BuildParameters {
public androidSdkManagerParameters!: string;
public androidExportType!: string;
public androidSymbolType!: string;
public dockerCpuLimit!: string;
public dockerMemoryLimit!: string;
public dockerIsolationMode!: string;

public customParameters!: string;
public sshAgent!: string;
Expand Down Expand Up @@ -116,10 +119,12 @@ class BuildParameters {
if (!Input.unitySerial && GitHub.githubInputEnabled) {
// No serial was present, so it is a personal license that we need to convert
if (!Input.unityLicense) {
throw new Error(`Missing Unity License File and no Serial was found. If this
throw new Error(
`Missing Unity License File and no Serial was found. If this
is a personal license, make sure to follow the activation
steps and set the UNITY_LICENSE GitHub secret or enter a Unity
serial number inside the UNITY_SERIAL GitHub secret.`);
serial number inside the UNITY_SERIAL GitHub secret.`,
);
}
unitySerial = this.getSerialFromLicenseFile(Input.unityLicense);
} else {
Expand Down Expand Up @@ -156,6 +161,9 @@ class BuildParameters {
sshPublicKeysDirectoryPath: Input.sshPublicKeysDirectoryPath,
gitPrivateToken: Input.gitPrivateToken || (await GithubCliReader.GetGitHubAuthToken()),
chownFilesTo: Input.chownFilesTo,
dockerCpuLimit: Input.dockerCpuLimit,
dockerMemoryLimit: Input.dockerMemoryLimit,
dockerIsolationMode: Input.dockerIsolationMode,
providerStrategy: CloudRunnerOptions.providerStrategy,
buildPlatform: CloudRunnerOptions.buildPlatform,
kubeConfig: CloudRunnerOptions.kubeConfig,
Expand Down
Loading

0 comments on commit 4c4611c

Please sign in to comment.