From 98d9c8966d725641353d3f6d97acb4016cefead2 Mon Sep 17 00:00:00 2001 From: akash1810 Date: Thu, 19 Sep 2024 23:18:06 +0100 Subject: [PATCH] Pass build identifier as prop Reverts #513 and removes the need to use a Jest mock. --- cdk/bin/cdk.ts | 1 + cdk/jest.setup.js | 3 -- .../__snapshots__/cdk-playground.test.ts.snap | 12 ------ cdk/lib/cdk-playground.test.ts | 4 +- cdk/lib/cdk-playground.ts | 38 +++++++++---------- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/cdk/bin/cdk.ts b/cdk/bin/cdk.ts index 4661971d..28c9d4b5 100644 --- a/cdk/bin/cdk.ts +++ b/cdk/bin/cdk.ts @@ -5,4 +5,5 @@ import { CdkPlayground } from '../lib/cdk-playground'; const app = new GuRoot(); new CdkPlayground(app, 'CdkPlayground', { cloudFormationStackName: 'playground-PROD-cdk-playground', + buildIdentifier: process.env.GITHUB_RUN_NUMBER ?? 'DEV', }); diff --git a/cdk/jest.setup.js b/cdk/jest.setup.js index 361b89f9..aae1749a 100644 --- a/cdk/jest.setup.js +++ b/cdk/jest.setup.js @@ -1,4 +1 @@ jest.mock('@guardian/cdk/lib/constants/tracking-tag'); - -process.env.GITHUB_RUN_NUMBER = 'TEST'; -process.env.GITHUB_SHA = 'TEST'; diff --git a/cdk/lib/__snapshots__/cdk-playground.test.ts.snap b/cdk/lib/__snapshots__/cdk-playground.test.ts.snap index cf254eed..61837e82 100644 --- a/cdk/lib/__snapshots__/cdk-playground.test.ts.snap +++ b/cdk/lib/__snapshots__/cdk-playground.test.ts.snap @@ -3,8 +3,6 @@ exports[`The Deploy stack matches the snapshot 1`] = ` Object { "Metadata": Object { - "gu:build:number": "TEST", - "gu:build:sha": "TEST", "gu:cdk:constructs": Array [ "GuVpcParameter", "GuSubnetListParameter", @@ -164,16 +162,6 @@ Object { "PropagateAtLaunch": true, "Value": "cdk-playground", }, - Object { - "Key": "gu:build:number", - "PropagateAtLaunch": true, - "Value": "TEST", - }, - Object { - "Key": "gu:build:sha", - "PropagateAtLaunch": true, - "Value": "TEST", - }, Object { "Key": "gu:cdk:version", "PropagateAtLaunch": true, diff --git a/cdk/lib/cdk-playground.test.ts b/cdk/lib/cdk-playground.test.ts index 28238552..8487faae 100644 --- a/cdk/lib/cdk-playground.test.ts +++ b/cdk/lib/cdk-playground.test.ts @@ -5,7 +5,9 @@ import { CdkPlayground } from './cdk-playground'; describe('The Deploy stack', () => { it('matches the snapshot', () => { const app = new App({ outdir: '/tmp/cdk.out' }); - const stack = new CdkPlayground(app, 'CdkPlayground'); + const stack = new CdkPlayground(app, 'CdkPlayground', { + buildIdentifier: 'TEST', + }); expect(Template.fromStack(stack).toJSON()).toMatchSnapshot(); }); }); diff --git a/cdk/lib/cdk-playground.ts b/cdk/lib/cdk-playground.ts index ce70736e..9c9f317b 100644 --- a/cdk/lib/cdk-playground.ts +++ b/cdk/lib/cdk-playground.ts @@ -6,16 +6,23 @@ import { GuStack } from '@guardian/cdk/lib/constructs/core'; import { GuCname } from '@guardian/cdk/lib/constructs/dns'; import { GuEc2AppExperimental } from '@guardian/cdk/lib/experimental/patterns/ec2-app'; import type { App } from 'aws-cdk-lib'; -import { Duration, Tags } from 'aws-cdk-lib'; +import { Duration } from 'aws-cdk-lib'; import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2'; import { Runtime } from 'aws-cdk-lib/aws-lambda'; +interface CdkPlaygroundProps extends Omit { + /** + * Which application build to run. + * This will typically match the build number provided by CI. + * + * @example + * process.env.GITHUB_RUN_NUMBER + */ + buildIdentifier: string; +} + export class CdkPlayground extends GuStack { - constructor( - scope: App, - id: string, - props?: Omit, - ) { + constructor(scope: App, id: string, props: CdkPlaygroundProps) { super(scope, id, { ...props, stack: 'playground', @@ -23,20 +30,20 @@ export class CdkPlayground extends GuStack { env: { region: 'eu-west-1' }, }); + const { buildIdentifier } = props; + const ec2App = 'cdk-playground'; const ec2AppDomainName = 'cdk-playground.gutools.co.uk'; - const buildNumber = process.env.GITHUB_RUN_NUMBER ?? 'DEV'; - - const { loadBalancer, autoScalingGroup } = new GuEc2AppExperimental(this, { + const { loadBalancer } = new GuEc2AppExperimental(this, { applicationPort: 9000, app: ec2App, instanceType: InstanceType.of(InstanceClass.T4G, InstanceSize.MICRO), access: { scope: AccessScope.PUBLIC }, userData: { distributable: { - fileName: `${ec2App}-${buildNumber}.deb`, - executionStatement: `dpkg -i /${ec2App}/${ec2App}-${buildNumber}.deb`, + fileName: `${ec2App}-${buildIdentifier}.deb`, + executionStatement: `dpkg -i /${ec2App}/${ec2App}-${buildIdentifier}.deb`, }, }, certificateProps: { @@ -92,14 +99,5 @@ export class CdkPlayground extends GuStack { domainName: lambdaDomainName, resourceRecord: domain.domainNameAliasDomainName, }); - - const { GITHUB_RUN_NUMBER = 'unknown', GITHUB_SHA = 'unknown' } = - process.env; - - this.addMetadata('gu:build:number', GITHUB_RUN_NUMBER); - this.addMetadata('gu:build:sha', GITHUB_SHA); - - Tags.of(autoScalingGroup).add('gu:build:number', GITHUB_RUN_NUMBER); - Tags.of(autoScalingGroup).add('gu:build:sha', GITHUB_SHA); } }