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 CloudRunnerLogger from './cloud-runner-logger';
|
|
|
|
|
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[] = [];
|
|
|
|
|
RemoteClientLogger.log(`GetCustomStepFiles: ${hookLifecycle}`);
|
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 17:54:25 +00:00
|
|
|
if (!CloudRunnerOptions.customStepFiles.includes(file)) {
|
|
|
|
|
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(
|
2022-10-13 18:53:57 +00:00
|
|
|
`- name: aws-s3-upload-build
|
2022-10-13 18:43:26 +00:00
|
|
|
image: amazon/aws-cli
|
|
|
|
|
commands: |
|
|
|
|
|
printenv
|
|
|
|
|
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
|
|
|
|
|
aws s3 cp /data/cache/$CACHE_KEY/build/build-$BUILD_GUID.tar.lz4 s3://game-ci-test-storage/$CACHE_KEY/build-$BUILD_GUID.tar.lz4
|
|
|
|
|
secrets:
|
2022-10-13 18:53:57 +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: aws-s3-upload-cache
|
|
|
|
|
image: amazon/aws-cli
|
|
|
|
|
commands: |
|
|
|
|
|
printenv
|
|
|
|
|
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
|
|
|
|
|
# upload from cache folder
|
|
|
|
|
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
|
|
|
|
|
commands: |
|
|
|
|
|
printenv
|
|
|
|
|
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
|
|
|
|
|
# determine most recent cache entry
|
|
|
|
|
# download to cache folder
|
|
|
|
|
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 || ``}`,
|
|
|
|
|
).filter((x) => CloudRunnerOptions.customStepFiles.includes(x.name));
|
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) {
|
|
|
|
|
|
|
|
|
|
CloudRunnerLogger.log(`Parsing build steps: ${steps}`);
|
|
|
|
|
|
|
|
|
|
// }
|
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
|
|
|
}
|