unity-builder/src/model/cloud-runner/services/cloud-runner-custom-steps.ts

201 lines
7.2 KiB
TypeScript
Raw Normal View History

2022-10-07 23:32:52 +00:00
import YAML from 'yaml';
import CloudRunnerSecret from './cloud-runner-secret';
import CloudRunner from '../cloud-runner';
import * as core from '@actions/core';
import { CustomWorkflow } from '../workflows/custom-workflow';
import { RemoteClientLogger } from '../remote-client/remote-client-logger';
import path from 'path';
import * as fs from 'fs';
import Input from '../../input';
2022-10-13 17:54:25 +00:00
import CloudRunnerOptions from '../cloud-runner-options';
2022-10-07 23:32:52 +00:00
export class CloudRunnerCustomSteps {
static GetCustomStepsFromFiles(hookLifecycle: string): CustomStep[] {
const results: CustomStep[] = [];
2022-10-14 18:51:16 +00:00
RemoteClientLogger.log(
`GetCustomStepFiles: ${hookLifecycle} CustomStepFiles: ${CloudRunnerOptions.customStepFiles}`,
);
2022-10-08 00:07:53 +00:00
try {
const gameCiCustomStepsPath = path.join(process.cwd(), `game-ci`, `steps`);
const files = fs.readdirSync(gameCiCustomStepsPath);
for (const file of files) {
2022-10-13 19:22:42 +00:00
if (!CloudRunnerOptions.customStepFiles.includes(file.replace(`.yaml`, ``))) {
2022-10-14 18:51:16 +00:00
RemoteClientLogger.log(`Skipping CustomStepFile: ${file}`);
2022-10-13 17:54:25 +00:00
continue;
}
2022-10-08 00:07:53 +00:00
const fileContents = fs.readFileSync(path.join(gameCiCustomStepsPath, file), `utf8`);
2022-10-08 04:21:33 +00:00
const fileContentsObject = CloudRunnerCustomSteps.ParseSteps(fileContents)[0];
2022-10-08 00:07:53 +00:00
if (fileContentsObject.hook === hookLifecycle) {
2022-10-08 04:21:33 +00:00
results.push(fileContentsObject);
2022-10-08 00:07:53 +00:00
}
2022-10-07 23:32:52 +00:00
}
2022-10-08 00:07:53 +00:00
} catch (error) {
RemoteClientLogger.log(`Failed Getting: ${hookLifecycle} \n ${JSON.stringify(error, undefined, 4)}`);
2022-10-07 23:32:52 +00:00
}
2022-10-08 04:21:33 +00:00
RemoteClientLogger.log(`Active Steps From Files: \n ${JSON.stringify(results, undefined, 4)}`);
2022-10-07 23:32:52 +00:00
2022-10-13 18:43:26 +00:00
const builtInCustomSteps: CustomStep[] = CloudRunnerCustomSteps.ParseSteps(
`- name: aws-s3-upload-build
2022-10-13 18:43:26 +00:00
image: amazon/aws-cli
2022-10-13 20:10:43 +00:00
hook: after
2022-10-13 18:43:26 +00:00
commands: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID --profile default
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY --profile default
aws configure set region $AWS_DEFAULT_REGION --profile default
2022-10-15 21:55:31 +00:00
aws s3 cp /data/cache/$CACHE_KEY/build/build-$BUILD_GUID.tar.lz4 s3://game-ci-test-storage/cloud-runner-cache/$CACHE_KEY/build-$BUILD_GUID.tar.lz4
2022-10-13 18:43:26 +00:00
secrets:
- name: awsAccessKeyId
value: ${process.env.AWS_ACCESS_KEY_ID || ``}
- name: awsSecretAccessKey
value: ${process.env.AWS_SECRET_ACCESS_KEY || ``}
- name: awsDefaultRegion
value: ${process.env.AWS_REGION || ``}
- name: aws-s3-upload-cache
image: amazon/aws-cli
2022-10-13 20:10:43 +00:00
hook: after
commands: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID --profile default
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY --profile default
aws configure set region $AWS_DEFAULT_REGION --profile default
2022-10-15 21:55:31 +00:00
aws s3 cp --recursive /data/cache/$CACHE_KEY/ s3://game-ci-test-storage/cloud-runner-cache/$CACHE_KEY/
secrets:
- name: awsAccessKeyId
value: ${process.env.AWS_ACCESS_KEY_ID || ``}
- name: awsSecretAccessKey
value: ${process.env.AWS_SECRET_ACCESS_KEY || ``}
- name: awsDefaultRegion
value: ${process.env.AWS_REGION || ``}
- name: aws-s3-pull-cache
image: amazon/aws-cli
2022-10-13 20:10:43 +00:00
hook: before
commands: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID --profile default
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY --profile default
aws configure set region $AWS_DEFAULT_REGION --profile default
2022-10-15 21:55:31 +00:00
BUCKET1="game-ci-test-storage/cloud-runner-cache/$CACHE_KEY/Library"
2022-10-17 14:05:19 +00:00
aws s3 ls $BUCKET1/
2022-10-17 13:54:10 +00:00
OBJECT1="$(aws s3 ls $BUCKET1/ | sort | tail -n 1 | awk '{print $4}' || '')"
2022-10-15 23:01:24 +00:00
aws s3 cp s3://$BUCKET1/$OBJECT1 /data/cache/$CACHE_KEY/Library/ || true
2022-10-15 21:55:31 +00:00
BUCKET2="game-ci-test-storage/cloud-runner-cache/$CACHE_KEY/lfs"
2022-10-17 14:05:19 +00:00
aws s3 ls $BUCKET2/
2022-10-17 13:54:10 +00:00
OBJECT2="$(aws s3 ls $BUCKET2/ | sort | tail -n 1 | awk '{print $4}' || '')"
2022-10-15 23:01:24 +00:00
aws s3 cp s3://$BUCKET2/$OBJECT2 /data/cache/$CACHE_KEY/lfs/ || true
secrets:
2022-10-13 20:10:43 +00:00
- name: awsAccessKeyId
value: ${process.env.AWS_ACCESS_KEY_ID || ``}
- name: awsSecretAccessKey
value: ${process.env.AWS_SECRET_ACCESS_KEY || ``}
- name: awsDefaultRegion
value: ${process.env.AWS_REGION || ``}
- name: debug-cache
image: ubuntu
hook: after
commands: |
apt-get update > /dev/null
apt-get install -y tree > /dev/null
2022-10-14 16:54:34 +00:00
${CloudRunnerOptions.cloudRunnerDebugTree ? `tree -L 3 /data/cache` : `#`}
2022-10-13 20:10:43 +00:00
secrets:
2022-10-13 18:43:26 +00:00
- name: awsAccessKeyId
value: ${process.env.AWS_ACCESS_KEY_ID || ``}
- name: awsSecretAccessKey
value: ${process.env.AWS_SECRET_ACCESS_KEY || ``}
- name: awsDefaultRegion
value: ${process.env.AWS_REGION || ``}`,
2022-10-13 20:32:10 +00:00
).filter((x) => CloudRunnerOptions.customStepFiles.includes(x.name) && x.hook === hookLifecycle);
2022-10-13 17:54:25 +00:00
if (builtInCustomSteps.length > 0) {
results.push(...builtInCustomSteps);
}
2022-10-07 23:32:52 +00:00
return results;
}
2022-10-08 01:22:18 +00:00
private static ConvertYamlSecrets(object) {
2022-10-08 01:35:21 +00:00
if (object.secrets === undefined) {
2022-10-08 02:03:47 +00:00
object.secrets = [];
2022-10-08 01:35:21 +00:00
return;
}
2022-10-08 01:22:18 +00:00
object.secrets = object.secrets.map((x) => {
return {
ParameterKey: x.name,
EnvironmentVariable: Input.ToEnvVarFormat(x.name),
ParameterValue: x.value,
};
});
}
2022-10-07 23:32:52 +00:00
public static ParseSteps(steps: string): CustomStep[] {
2022-10-08 00:26:09 +00:00
if (steps === '') {
return [];
}
2022-10-08 02:57:36 +00:00
// if (CloudRunner.buildParameters?.cloudRunnerIntegrationTests) {
2022-10-14 16:54:34 +00:00
// CloudRunnerLogger.log(`Parsing build steps: ${steps}`);
2022-10-08 02:57:36 +00:00
// }
2022-10-08 05:01:04 +00:00
const isArray = steps.replace(/\s/g, ``)[0] === `-`;
2022-10-08 01:35:21 +00:00
const object: CustomStep[] = isArray ? YAML.parse(steps) : [YAML.parse(steps)];
2022-10-08 02:42:20 +00:00
for (const step of object) {
CloudRunnerCustomSteps.ConvertYamlSecrets(step);
if (step.secrets === undefined) {
step.secrets = [];
}
if (step.image === undefined) {
step.image = `ubuntu`;
2022-10-07 23:32:52 +00:00
}
2022-10-08 01:35:21 +00:00
}
if (object === undefined) {
throw new Error(`Failed to parse ${steps}`);
2022-10-07 23:32:52 +00:00
}
return object;
}
static async RunPostBuildSteps(cloudRunnerStepState) {
let output = ``;
2022-10-08 00:12:39 +00:00
const steps: CustomStep[] = [
...CloudRunnerCustomSteps.ParseSteps(CloudRunner.buildParameters.postBuildSteps),
...CloudRunnerCustomSteps.GetCustomStepsFromFiles(`after`),
];
2022-10-07 23:32:52 +00:00
if (steps.length > 0) {
if (!CloudRunner.buildParameters.isCliMode) core.startGroup('post build steps');
output += await CustomWorkflow.runCustomJob(
steps,
cloudRunnerStepState.environment,
cloudRunnerStepState.secrets,
);
if (!CloudRunner.buildParameters.isCliMode) core.endGroup();
}
return output;
}
static async RunPreBuildSteps(cloudRunnerStepState) {
let output = ``;
2022-10-08 00:12:39 +00:00
const steps: CustomStep[] = [
...CloudRunnerCustomSteps.ParseSteps(CloudRunner.buildParameters.preBuildSteps),
...CloudRunnerCustomSteps.GetCustomStepsFromFiles(`before`),
];
2022-10-07 23:32:52 +00:00
if (steps.length > 0) {
if (!CloudRunner.buildParameters.isCliMode) core.startGroup('pre build steps');
output += await CustomWorkflow.runCustomJob(
steps,
cloudRunnerStepState.environment,
cloudRunnerStepState.secrets,
);
if (!CloudRunner.buildParameters.isCliMode) core.endGroup();
}
return output;
}
}
export class CustomStep {
public commands;
public secrets: CloudRunnerSecret[] = new Array<CloudRunnerSecret>();
public name;
public image: string = `ubuntu`;
2022-10-08 02:11:56 +00:00
public hook!: string;
2022-10-07 23:32:52 +00:00
}