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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ParseSteps(steps: string): CustomStep[] {
|
|
|
|
|
let object: any;
|
|
|
|
|
try {
|
|
|
|
|
if (CloudRunner.buildParameters.cloudRunnerIntegrationTests) {
|
|
|
|
|
CloudRunnerLogger.log(`Parsing build steps: ${steps}`);
|
|
|
|
|
}
|
|
|
|
|
object = YAML.parse(steps);
|
|
|
|
|
if (object === undefined) {
|
|
|
|
|
throw new Error(`Failed to parse ${steps}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
CloudRunnerLogger.log(`failed to parse a custom job "${steps}"`);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
for (const step of object) {
|
|
|
|
|
step.secrets = step.secrets.map((x) => {
|
|
|
|
|
return {
|
|
|
|
|
ParameterKey: x.name,
|
|
|
|
|
EnvironmentVariable: Input.ToEnvVarFormat(x.name),
|
|
|
|
|
ParameterValue: x.value,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async RunPostBuildSteps(cloudRunnerStepState) {
|
|
|
|
|
let output = ``;
|
|
|
|
|
let steps: CustomStep[] = [];
|
|
|
|
|
if (CloudRunner.buildParameters.postBuildSteps !== '') {
|
|
|
|
|
steps = CloudRunnerCustomSteps.ParseSteps(CloudRunner.buildParameters.postBuildSteps);
|
|
|
|
|
}
|
|
|
|
|
const fileSteps = CloudRunnerCustomSteps.GetCustomStepsFromFiles(`after`);
|
|
|
|
|
if (fileSteps.length > 0) {
|
|
|
|
|
steps = [...steps, ...fileSteps];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = ``;
|
|
|
|
|
let steps: CustomStep[] = [];
|
|
|
|
|
if (CloudRunner.buildParameters.preBuildSteps !== '') {
|
|
|
|
|
steps = CloudRunnerCustomSteps.ParseSteps(CloudRunner.buildParameters.preBuildSteps);
|
|
|
|
|
}
|
|
|
|
|
steps = [...steps, ...CloudRunnerCustomSteps.GetCustomStepsFromFiles(`before`)];
|
|
|
|
|
|
|
|
|
|
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[];
|
|
|
|
|
}
|