Setup memory/cpu limits for windows containers

This commit is contained in:
Andrew Kahr 2023-10-16 21:40:12 -07:00
parent 1f5e83eaf1
commit 2b57efdf8e
6 changed files with 39 additions and 3 deletions

View File

@ -106,6 +106,16 @@ inputs:
default: '' default: ''
description: description:
'User and optionally group (user or user:group or uid:gid) to give ownership of the resulting build artifacts' '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 Windows docker container. Defaults to all available cores.'
dockerMemoryLimit:
required: false
default: ''
description:
'Amount of memory to assign the Windows docker container. Defaults to 75% of total system memory rounded down to
the nearest gigabyte.'
allowDirtyBuild: allowDirtyBuild:
required: false required: false
default: '' default: ''

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

View File

@ -116,10 +116,12 @@ class BuildParameters {
if (!Input.unitySerial && GitHub.githubInputEnabled) { if (!Input.unitySerial && GitHub.githubInputEnabled) {
// No serial was present, so it is a personal license that we need to convert // No serial was present, so it is a personal license that we need to convert
if (!Input.unityLicense) { 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 is a personal license, make sure to follow the activation
steps and set the UNITY_LICENSE GitHub secret or enter a Unity 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); unitySerial = this.getSerialFromLicenseFile(Input.unityLicense);
} else { } else {
@ -156,6 +158,8 @@ class BuildParameters {
sshPublicKeysDirectoryPath: Input.sshPublicKeysDirectoryPath, sshPublicKeysDirectoryPath: Input.sshPublicKeysDirectoryPath,
gitPrivateToken: Input.gitPrivateToken || (await GithubCliReader.GetGitHubAuthToken()), gitPrivateToken: Input.gitPrivateToken || (await GithubCliReader.GetGitHubAuthToken()),
chownFilesTo: Input.chownFilesTo, chownFilesTo: Input.chownFilesTo,
dockerCpuLimit: Input.dockerCpuLimit,
dockerMemoryLimit: Input.dockerMemoryLimit,
providerStrategy: CloudRunnerOptions.providerStrategy, providerStrategy: CloudRunnerOptions.providerStrategy,
buildPlatform: CloudRunnerOptions.buildPlatform, buildPlatform: CloudRunnerOptions.buildPlatform,
kubeConfig: CloudRunnerOptions.kubeConfig, kubeConfig: CloudRunnerOptions.kubeConfig,

View File

@ -86,7 +86,15 @@ class Docker {
} }
static getWindowsCommand(image: string, parameters: DockerParameters): string { static getWindowsCommand(image: string, parameters: DockerParameters): string {
const { workspace, actionFolder, unitySerial, gitPrivateToken, dockerWorkspacePath } = parameters; const {
workspace,
actionFolder,
unitySerial,
gitPrivateToken,
dockerWorkspacePath,
dockerCpuLimit,
dockerMemoryLimit,
} = parameters;
return `docker run \ return `docker run \
--workdir c:${dockerWorkspacePath} \ --workdir c:${dockerWorkspacePath} \
@ -97,12 +105,15 @@ class Docker {
${gitPrivateToken ? `--env GIT_PRIVATE_TOKEN="${gitPrivateToken}"` : ''} \ ${gitPrivateToken ? `--env GIT_PRIVATE_TOKEN="${gitPrivateToken}"` : ''} \
--volume "${workspace}":"c:${dockerWorkspacePath}" \ --volume "${workspace}":"c:${dockerWorkspacePath}" \
--volume "c:/regkeys":"c:/regkeys" \ --volume "c:/regkeys":"c:/regkeys" \
--volume "C:/Program Files/Microsoft Visual Studio":"C:/Program Files/Microsoft Visual Studio" \
--volume "C:/Program Files (x86)/Microsoft Visual Studio":"C:/Program Files (x86)/Microsoft Visual Studio" \ --volume "C:/Program Files (x86)/Microsoft Visual Studio":"C:/Program Files (x86)/Microsoft Visual Studio" \
--volume "C:/Program Files (x86)/Windows Kits":"C:/Program Files (x86)/Windows Kits" \ --volume "C:/Program Files (x86)/Windows Kits":"C:/Program Files (x86)/Windows Kits" \
--volume "C:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio" \ --volume "C:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio" \
--volume "${actionFolder}/default-build-script":"c:/UnityBuilderAction" \ --volume "${actionFolder}/default-build-script":"c:/UnityBuilderAction" \
--volume "${actionFolder}/platforms/windows":"c:/steps" \ --volume "${actionFolder}/platforms/windows":"c:/steps" \
--volume "${actionFolder}/BlankProject":"c:/BlankProject" \ --volume "${actionFolder}/BlankProject":"c:/BlankProject" \
--cpus=${dockerCpuLimit} \
--memory=${dockerMemoryLimit} \
${image} \ ${image} \
powershell c:/steps/entrypoint.ps1`; powershell c:/steps/entrypoint.ps1`;
} }

View File

@ -4,6 +4,7 @@ import { Cli } from './cli/cli';
import CloudRunnerQueryOverride from './cloud-runner/options/cloud-runner-query-override'; import CloudRunnerQueryOverride from './cloud-runner/options/cloud-runner-query-override';
import Platform from './platform'; import Platform from './platform';
import GitHub from './github'; import GitHub from './github';
import os from 'node:os';
import * as core from '@actions/core'; import * as core from '@actions/core';
@ -226,6 +227,16 @@ class Input {
return Input.getInput('dockerWorkspacePath') || '/github/workspace'; return Input.getInput('dockerWorkspacePath') || '/github/workspace';
} }
static get dockerCpuLimit(): string {
return Input.getInput('dockerCpuLimit') || os.cpus().length.toString();
}
static get dockerMemoryLimit(): string {
const bytesInGigabyte = 1024 * 1024 * 1024;
return Input.getInput('dockerMemoryLimit') || `${Math.floor((os.totalmem() / bytesInGigabyte) * 0.75)}G`;
}
public static ToEnvVarFormat(input: string) { public static ToEnvVarFormat(input: string) {
if (input.toUpperCase() === input) { if (input.toUpperCase() === input) {
return input; return input;