| 
									
										
										
										
											2019-12-22 14:05:15 +00:00
										 |  |  | import { exec } from '@actions/exec'; | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  | import ImageTag from './image-tag'; | 
					
						
							| 
									
										
										
										
											2022-02-01 02:31:20 +00:00
										 |  |  | import ImageEnvironmentFactory from './image-environment-factory'; | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  | import { existsSync, mkdirSync } from 'fs'; | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2019-12-22 14:05:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 23:06:14 +00:00
										 |  |  | class Docker { | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  |   static async build(buildParameters, silent = false) { | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  |     const { path: buildPath, dockerfile, baseImage } = buildParameters; | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  |     const { version, platform } = baseImage; | 
					
						
							| 
									
										
										
										
											2019-12-22 14:05:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  |     const tag = new ImageTag({ repository: '', name: 'unity-builder', version, platform }); | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  |     const command = `docker build ${buildPath} \
 | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  |       --file ${dockerfile} \ | 
					
						
							|  |  |  |       --build-arg IMAGE=${baseImage} \ | 
					
						
							|  |  |  |       --tag ${tag}`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-01 14:52:08 +00:00
										 |  |  |     await exec(command, undefined, { silent }); | 
					
						
							| 
									
										
										
										
											2019-12-22 14:05:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return tag; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-22 21:24:46 +00:00
										 |  |  |   static async run(image, parameters, silent = false) { | 
					
						
							| 
									
										
										
										
											2022-02-01 02:31:20 +00:00
										 |  |  |     const { workspace, unitySerial, runnerTempPath, sshAgent } = parameters; | 
					
						
							| 
									
										
										
										
											2019-12-22 17:07:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |     const baseOsSpecificArguments = this.getBaseOsSpecificArguments( | 
					
						
							|  |  |  |       process.platform, | 
					
						
							|  |  |  |       workspace, | 
					
						
							|  |  |  |       unitySerial, | 
					
						
							|  |  |  |       runnerTempPath, | 
					
						
							|  |  |  |       sshAgent, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-12-22 21:24:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |     const runCommand = `docker run \
 | 
					
						
							|  |  |  |     --workdir /github/workspace \ | 
					
						
							|  |  |  |     --rm \ | 
					
						
							| 
									
										
										
										
											2022-02-01 02:31:20 +00:00
										 |  |  |     ${ImageEnvironmentFactory.getEnvVarString(parameters)} \ | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |     ${baseOsSpecificArguments} \ | 
					
						
							|  |  |  |     ${image}`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await exec(runCommand, undefined, { silent }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static getBaseOsSpecificArguments(baseOs, workspace, unitySerial, runnerTemporaryPath, sshAgent): string { | 
					
						
							|  |  |  |     switch (baseOs) { | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  |       case 'linux': { | 
					
						
							|  |  |  |         const githubHome = path.join(runnerTemporaryPath, '_github_home'); | 
					
						
							|  |  |  |         if (!existsSync(githubHome)) mkdirSync(githubHome); | 
					
						
							|  |  |  |         const githubWorkflow = path.join(runnerTemporaryPath, '_github_workflow'); | 
					
						
							|  |  |  |         if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |         return `--env UNITY_SERIAL \
 | 
					
						
							| 
									
										
										
										
											2022-02-02 09:15:37 +00:00
										 |  |  |                 --env GITHUB_WORKSPACE=/github/workspace \ | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |                 ${sshAgent ? '--env SSH_AUTH_SOCK=/ssh-agent' : ''} \ | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  |                 --volume "/var/run/docker.sock":"/var/run/docker.sock:z" \ | 
					
						
							|  |  |  |                 --volume "${githubHome}":"/root:z" \ | 
					
						
							|  |  |  |                 --volume "${githubWorkflow}":"/github/workflow:z" \ | 
					
						
							|  |  |  |                 --volume "${workspace}":"/github/workspace:z" \ | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |                 ${sshAgent ? `--volume ${sshAgent}:/ssh-agent` : ''} \ | 
					
						
							|  |  |  |                 ${sshAgent ? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro' : ''}`;
 | 
					
						
							| 
									
										
										
										
											2022-03-11 17:18:18 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |       case 'win32': | 
					
						
							|  |  |  |         return `--env UNITY_SERIAL="${unitySerial}" \
 | 
					
						
							| 
									
										
										
										
											2022-02-02 09:15:37 +00:00
										 |  |  |                 --env GITHUB_WORKSPACE=c:/github/workspace \ | 
					
						
							| 
									
										
										
										
											2022-01-25 21:18:15 +00:00
										 |  |  |                 --volume "${workspace}":"c:/github/workspace" \ | 
					
						
							|  |  |  |                 --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"`;
 | 
					
						
							|  |  |  |       //Note: When upgrading to Server 2022, we will need to move to just "program files" since VS will be 64-bit
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ''; | 
					
						
							| 
									
										
										
										
											2019-12-22 14:05:15 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-20 23:06:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default Docker; |