| 
									
										
										
										
											2021-09-22 20:05:21 +00:00
										 |  |  | import BuildParameters from '../../build-parameters'; | 
					
						
							| 
									
										
										
										
											2021-09-29 22:39:15 +00:00
										 |  |  | import CloudRunnerEnvironmentVariable from '../cloud-runner-services/cloud-runner-environment-variable'; | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class KubernetesJobSpecFactory { | 
					
						
							|  |  |  |   static getJobSpec( | 
					
						
							|  |  |  |     command: string[], | 
					
						
							|  |  |  |     image: string, | 
					
						
							|  |  |  |     mountdir: string, | 
					
						
							|  |  |  |     workingDirectory: string, | 
					
						
							| 
									
										
										
										
											2021-08-17 22:13:46 +00:00
										 |  |  |     environment: CloudRunnerEnvironmentVariable[], | 
					
						
							| 
									
										
										
										
											2021-08-15 21:59:58 +00:00
										 |  |  |     buildGuid: string, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |     buildParameters: BuildParameters, | 
					
						
							|  |  |  |     secretName, | 
					
						
							|  |  |  |     pvcName, | 
					
						
							|  |  |  |     jobName, | 
					
						
							|  |  |  |     k8s, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     environment.push( | 
					
						
							|  |  |  |       ...[ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'GITHUB_SHA', | 
					
						
							| 
									
										
										
										
											2021-08-15 21:59:58 +00:00
										 |  |  |           value: buildGuid, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'GITHUB_WORKSPACE', | 
					
						
							|  |  |  |           value: '/data/repo', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'PROJECT_PATH', | 
					
						
							|  |  |  |           value: buildParameters.projectPath, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'BUILD_PATH', | 
					
						
							|  |  |  |           value: buildParameters.buildPath, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'BUILD_FILE', | 
					
						
							|  |  |  |           value: buildParameters.buildFile, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'BUILD_NAME', | 
					
						
							|  |  |  |           value: buildParameters.buildName, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'BUILD_METHOD', | 
					
						
							|  |  |  |           value: buildParameters.buildMethod, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'CUSTOM_PARAMETERS', | 
					
						
							|  |  |  |           value: buildParameters.customParameters, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'CHOWN_FILES_TO', | 
					
						
							|  |  |  |           value: buildParameters.chownFilesTo, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'BUILD_TARGET', | 
					
						
							|  |  |  |           value: buildParameters.platform, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'ANDROID_VERSION_CODE', | 
					
						
							|  |  |  |           value: buildParameters.androidVersionCode.toString(), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'ANDROID_KEYSTORE_NAME', | 
					
						
							|  |  |  |           value: buildParameters.androidKeystoreName, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'ANDROID_KEYALIAS_NAME', | 
					
						
							|  |  |  |           value: buildParameters.androidKeyaliasName, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     const job = new k8s.V1Job(); | 
					
						
							|  |  |  |     job.apiVersion = 'batch/v1'; | 
					
						
							|  |  |  |     job.kind = 'Job'; | 
					
						
							|  |  |  |     job.metadata = { | 
					
						
							|  |  |  |       name: jobName, | 
					
						
							|  |  |  |       labels: { | 
					
						
							|  |  |  |         app: 'unity-builder', | 
					
						
							| 
									
										
										
										
											2021-08-15 21:59:58 +00:00
										 |  |  |         buildGuid, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     job.spec = { | 
					
						
							| 
									
										
										
										
											2021-06-26 03:18:14 +00:00
										 |  |  |       backoffLimit: 0, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |       template: { | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           volumes: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               name: 'build-mount', | 
					
						
							|  |  |  |               persistentVolumeClaim: { | 
					
						
							|  |  |  |                 claimName: pvcName, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               name: 'credentials', | 
					
						
							|  |  |  |               secret: { | 
					
						
							|  |  |  |                 secretName, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |           containers: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               name: 'main', | 
					
						
							|  |  |  |               image, | 
					
						
							|  |  |  |               command: ['/bin/sh'], | 
					
						
							|  |  |  |               args: ['-c', ...command], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               workingDir: `/${workingDirectory}`, | 
					
						
							|  |  |  |               resources: { | 
					
						
							|  |  |  |                 requests: { | 
					
						
							| 
									
										
										
										
											2021-08-17 22:13:46 +00:00
										 |  |  |                   memory: buildParameters.cloudRunnerMemory, | 
					
						
							|  |  |  |                   cpu: buildParameters.cloudRunnerCpu, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |                 }, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               env: environment, | 
					
						
							|  |  |  |               volumeMounts: [ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                   name: 'build-mount', | 
					
						
							|  |  |  |                   mountPath: `/${mountdir}`, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                   name: 'credentials', | 
					
						
							|  |  |  |                   mountPath: '/credentials', | 
					
						
							|  |  |  |                   readOnly: true, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               ], | 
					
						
							|  |  |  |               lifecycle: { | 
					
						
							|  |  |  |                 preStop: { | 
					
						
							|  |  |  |                   exec: { | 
					
						
							|  |  |  |                     command: [ | 
					
						
							|  |  |  |                       'bin/bash', | 
					
						
							|  |  |  |                       '-c', | 
					
						
							|  |  |  |                       `cd /data/builder/action/steps;
 | 
					
						
							|  |  |  |                       chmod +x /return_license.sh; | 
					
						
							|  |  |  |                       /return_license.sh;`, | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                   }, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-09-08 22:17:23 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |               name: 'controller-cleanup', | 
					
						
							|  |  |  |               image: 'alpine', | 
					
						
							|  |  |  |               command: ['/bin/sh'], | 
					
						
							|  |  |  |               args: ['-c', 'echo "test"'], | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |           ], | 
					
						
							|  |  |  |           restartPolicy: 'Never', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return job; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export default KubernetesJobSpecFactory; |