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

119 lines
3.9 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 CloudRunnerLogger from './cloud-runner-logger';
import Input from '../../input';
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) {
const fileContents = fs.readFileSync(path.join(gameCiCustomStepsPath, file), `utf8`);
const fileContentsObject = YAML.parse(fileContents.toString());
if (fileContentsObject.hook === hookLifecycle) {
RemoteClientLogger.log(`Active Step File ${file} contents: ${fileContents}`);
results.push(...CloudRunnerCustomSteps.ParseSteps(fileContents));
}
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
}
return results;
}
2022-10-08 01:22:18 +00:00
private static ConvertYamlSecrets(object) {
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-07 23:32:52 +00:00
let object: any;
try {
if (CloudRunner.buildParameters.cloudRunnerIntegrationTests) {
CloudRunnerLogger.log(`Parsing build steps: ${steps}`);
}
2022-10-08 01:22:18 +00:00
const isArray = steps[0] === `-`;
object = isArray ? YAML.parse(steps) : [YAML.parse(steps)];
if (isArray) {
for (const step of object) {
CloudRunnerCustomSteps.ConvertYamlSecrets(step);
}
} else {
CloudRunnerCustomSteps.ConvertYamlSecrets(object);
2022-10-08 00:27:48 +00:00
}
2022-10-07 23:32:52 +00:00
if (object === undefined) {
throw new Error(`Failed to parse ${steps}`);
}
} catch (error) {
2022-10-08 00:27:48 +00:00
CloudRunnerLogger.log(`failed to parse a custom job "${steps} \n ${JSON.stringify(error, undefined, 4)}"`);
2022-10-07 23:32:52 +00:00
throw error;
}
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`;
public hook!: string[];
}