forked from game-ci/unity-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.ts
112 lines (105 loc) · 4.86 KB
/
docker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { execWithErrorCheck } from './exec-with-error-check';
import ImageEnvironmentFactory from './image-environment-factory';
import { existsSync, mkdirSync } from 'node:fs';
import path from 'node:path';
import { ExecOptions } from '@actions/exec';
import { DockerParameters, StringKeyValuePair } from './shared-types';
class Docker {
static async run(
image: string,
parameters: DockerParameters,
silent: boolean = false,
overrideCommands: string = '',
additionalVariables: StringKeyValuePair[] = [],
// eslint-disable-next-line unicorn/no-useless-undefined
options: ExecOptions | undefined = undefined,
entrypointBash: boolean = false,
errorWhenMissingUnityBuildResults: boolean = true,
) {
let runCommand = '';
switch (process.platform) {
case 'linux':
runCommand = this.getLinuxCommand(image, parameters, overrideCommands, additionalVariables, entrypointBash);
break;
case 'win32':
runCommand = this.getWindowsCommand(image, parameters);
}
if (options) {
options.silent = silent;
await execWithErrorCheck(runCommand, undefined, options, errorWhenMissingUnityBuildResults);
} else {
await execWithErrorCheck(runCommand, undefined, { silent }, errorWhenMissingUnityBuildResults);
}
}
static getLinuxCommand(
image: string,
parameters: DockerParameters,
overrideCommands: string = '',
additionalVariables: StringKeyValuePair[] = [],
entrypointBash: boolean = false,
): string {
const {
workspace,
actionFolder,
runnerTempPath,
sshAgent,
sshPublicKeysDirectoryPath,
gitPrivateToken,
dockerWorkspacePath,
} = parameters;
const githubHome = path.join(runnerTempPath, '_github_home');
if (!existsSync(githubHome)) mkdirSync(githubHome);
const githubWorkflow = path.join(runnerTempPath, '_github_workflow');
if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow);
const commandPrefix = image === `alpine` ? `/bin/sh` : `/bin/bash`;
return `docker run \
--workdir ${dockerWorkspacePath} \
--rm \
--gpus all \
${ImageEnvironmentFactory.getEnvVarString(parameters, additionalVariables)} \
--env UNITY_SERIAL \
--env GITHUB_WORKSPACE=${dockerWorkspacePath} \
--env GIT_CONFIG_EXTENSIONS \
${gitPrivateToken ? `--env GIT_PRIVATE_TOKEN="${gitPrivateToken}"` : ''} \
${sshAgent ? '--env SSH_AUTH_SOCK=/ssh-agent' : ''} \
--volume "${githubHome}":"/root:z" \
--volume "${githubWorkflow}":"/github/workflow:z" \
--volume "${workspace}":"${dockerWorkspacePath}:z" \
--volume "${actionFolder}/default-build-script:/UnityBuilderAction:z" \
--volume "${actionFolder}/platforms/ubuntu/steps:/steps:z" \
--volume "${actionFolder}/platforms/ubuntu/entrypoint.sh:/entrypoint.sh:z" \
--volume "${actionFolder}/unity-config:/usr/share/unity3d/config/:z" \
${sshAgent ? `--volume ${sshAgent}:/ssh-agent` : ''} \
${
sshAgent && !sshPublicKeysDirectoryPath
? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro'
: ''
} \
${sshPublicKeysDirectoryPath ? `--volume ${sshPublicKeysDirectoryPath}:/root/.ssh:ro` : ''} \
${entrypointBash ? `--entrypoint ${commandPrefix}` : ``} \
${image} \
${entrypointBash ? `-c` : `${commandPrefix} -c`} \
"${overrideCommands !== '' ? overrideCommands : `/entrypoint.sh`}"`;
}
static getWindowsCommand(image: string, parameters: DockerParameters): string {
const { workspace, actionFolder, unitySerial, gitPrivateToken, dockerWorkspacePath } = parameters;
return `docker run \
--workdir c:${dockerWorkspacePath} \
--rm \
${ImageEnvironmentFactory.getEnvVarString(parameters)} \
--env UNITY_SERIAL="${unitySerial}" \
--env GITHUB_WORKSPACE=c:${dockerWorkspacePath} \
${gitPrivateToken ? `--env GIT_PRIVATE_TOKEN="${gitPrivateToken}"` : ''} \
--volume "${workspace}":"c:${dockerWorkspacePath}" \
--volume "c:/regkeys":"c:/regkeys" \
--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:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio" \
--volume "${actionFolder}/default-build-script":"c:/UnityBuilderAction" \
--volume "${actionFolder}/platforms/windows":"c:/steps" \
--volume "${actionFolder}/BlankProject":"c:/BlankProject" \
${image} \
powershell c:/steps/entrypoint.ps1`;
}
}
export default Docker;