Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .changeset/clean-planes-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"@guardian/cdk": minor
---

Support customised output directory for `riff-raff.yaml` file to support repositories that deploy multiple Riff-Raff projects.

Example usage:

```ts
import { RiffRaffYamlFile } from "./index";

const appForInfra = new App();

const myInfraStack = new MyInfraStack(appForInfra, "my-infra-stack", {
stack: "playground",
stage: "INFRA",
env: { region: "eu-west-1" },
});

const appForMyApp = new App();

const myAppStackCODE = new MyAppStack(appForMyApp, "my-stack-CODE", {
stack: "playground",
stage: "CODE",
env: { region: "eu-west-1" },
});
const myAppStackPROD = new MyAppStack(appForMyApp, "my-stack-PROD", {
stack: "playground",
stage: "CODE",
env: { region: "eu-west-1" },
});

new RiffRaffYamlFile(appForInfra, "playground::core-infra"); // Generates a file to `cdk.out/playground::core-infra/riff-raff.yaml`
new RiffRaffYamlFile(appForMyApp, "playground::my-app"); // Generates a file to `cdk.out/playground::my-app/riff-raff.yaml`
```
32 changes: 32 additions & 0 deletions src/riff-raff-yaml-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ new MyStack(app, "my-stack-PROD", {});
```

### Advanced usage
#### Additional deployment types
As noted above, only specific deployment types are currently supported.

If you want to add additional deployment types, you can do so by instantiating `RiffRaffYamlFile` directly:
Expand Down Expand Up @@ -79,6 +80,37 @@ riffRaff.synth();

When the CDK stack is synthesized, a `riff-raff.yaml` file will be created in the output directory, typically `/<repo-root>/cdk/cdk.out`.

#### Multiple Riff-Raff projects in a single repository
If your repository deploys multiple Riff-Raff projects, multiple `riff-raff.yaml` files can be created thus:

```ts
import { RiffRaffYamlFile } from "./index";

const appForInfra = new App();

const myInfraStack = new MyInfraStack(appForInfra, "my-infra-stack", {
stack: "playground",
stage: "INFRA",
env: { region: "eu-west-1" },
});

const appForMyApp = new App();

const myAppStackCODE = new MyAppStack(appForMyApp, "my-stack-CODE", {
stack: "playground",
stage: "CODE",
env: { region: "eu-west-1" },
});
const myAppStackPROD = new MyAppStack(appForMyApp, "my-stack-PROD", {
stack: "playground",
stage: "CODE",
env: { region: "eu-west-1" },
});

new RiffRaffYamlFile(appForInfra, "playground::core-infra"); // Generates a file to `cdk.out/playground::core-infra/riff-raff.yaml`
new RiffRaffYamlFile(appForMyApp, "playground::my-app"); // Generates a file to `cdk.out/playground::my-app/riff-raff.yaml`
```

## Package layout
`RiffRaffYamlFile` assumes CI has uploaded files in the following structure:

Expand Down
23 changes: 19 additions & 4 deletions src/riff-raff-yaml-file/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from "fs";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";
import type { App } from "aws-cdk-lib";
import { Token } from "aws-cdk-lib";
Expand Down Expand Up @@ -76,6 +76,7 @@ export class RiffRaffYamlFile {
private readonly allStageTags: StageTag[];
private readonly allRegions: Region[];
private readonly outdir: string;
private readonly riffRaffProjectName?: string;

/**
* The `riff-raff.yaml` file as an object.
Expand Down Expand Up @@ -206,8 +207,13 @@ export class RiffRaffYamlFile {
return cdkStack.dependencies.filter((_) => _ instanceof GuStack) as GuStack[];
}

/**
* Creates a `riff-raff.yaml` file.
* @param app - the root node of the CDK construct tree. Every GuStack within will be added to `riff-raff.yaml`.
* @param riffRaffProjectName - the name of the Riff-Raff project the stacks belong to. The `riff-raff.yaml` file will be generated to this directory _within_ `cdk.out`.
*/
// eslint-disable-next-line custom-rules/valid-constructors -- this needs to sit above GuStack on the cdk tree
constructor(app: App) {
constructor(app: App, riffRaffProjectName?: string) {
this.allCdkStacks = app.node.findAll().filter((_) => _ instanceof GuStack) as GuStack[];
const allowedStages = new Set(this.allCdkStacks.map(({ stage }) => stage));
this.allStageTags = Array.from(allowedStages);
Expand All @@ -218,6 +224,7 @@ export class RiffRaffYamlFile {
this.validateAllRegionsAreResolved();

this.outdir = app.outdir;
this.riffRaffProjectName = riffRaffProjectName;

const deployments = new Map<RiffRaffDeploymentName, RiffRaffDeploymentProps>();

Expand Down Expand Up @@ -329,7 +336,15 @@ export class RiffRaffYamlFile {
* It'll be located with the CFN JSON templates generated by `cdk synth`.
*/
synth(): void {
const outPath = path.join(this.outdir, "riff-raff.yaml");
writeFileSync(outPath, this.toYAML());
const outputDirectory = path.join(this.outdir, this.riffRaffProjectName ?? "");
const outputFile = path.join(outputDirectory, "riff-raff.yaml");

if (!existsSync(outputDirectory)) {
console.log(`Output directory ${outputDirectory} does not exist; creating it...`);
mkdirSync(outputDirectory);
}

writeFileSync(outputFile, this.toYAML());
console.log(`Successfully created ${outputFile}`);
}
}
Loading