From a8993a46df544e40bbb1833bb84d7f3da6ea7cbf Mon Sep 17 00:00:00 2001 From: Enric Bisbe Gil Date: Fri, 12 Jul 2024 12:56:41 +0200 Subject: [PATCH] generate .env.e2e to be able to run e2e tests --- .github/workflows/github-actions.yml | 3 ++- __tests__/e2e.test.js | 4 +++- package.json | 1 + pnpm-lock.yaml | 8 +++++++ post-deploy.js | 34 ++++++++++++++++++++++++++++ serverless.ts | 18 ++++++++++++++- 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 post-deploy.js diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 9ab1ab8..a8b6d2b 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -17,4 +17,5 @@ jobs: run: | pnpm install pnpm test - pnpm sls deploy \ No newline at end of file + pnpm sls deploy + pnpm test-e2e \ No newline at end of file diff --git a/__tests__/e2e.test.js b/__tests__/e2e.test.js index 3fa6ed2..514a6a5 100644 --- a/__tests__/e2e.test.js +++ b/__tests__/e2e.test.js @@ -1,6 +1,8 @@ import { describe, expect, it } from "@jest/globals"; +import { config } from "dotenv"; +config({ path: ".env.e2e" }); -const { endpoint } = process.env; +const { API_GATEWAY_URL: endpoint } = process.env; const wrapper = ({ path = "", method, body }) => { const headers = { Accept: "application/json" }; diff --git a/package.json b/package.json index 98e9dd8..65f44c5 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "prettier": "3.3.2", "serverless": "^4.1.11", "serverless-iam-roles-per-function": "^3.2.0", + "serverless-plugin-scripts": "^1.0.2", "serverless-step-functions": "^3.21.0", "ts-jest": "^29.1.5", "typescript": "^5.5.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c8cc7c5..10d9809 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -87,6 +87,9 @@ importers: serverless-iam-roles-per-function: specifier: ^3.2.0 version: 3.2.0 + serverless-plugin-scripts: + specifier: ^1.0.2 + version: 1.0.2 serverless-step-functions: specifier: ^3.21.0 version: 3.21.0(serverless@4.1.11) @@ -2681,6 +2684,9 @@ packages: resolution: {integrity: sha512-AXmxACHEUsDcFDcv8QNwDgn2L0brRJ7pz/phD3lFB/wQ3TtPJkorC+J7PxgFQbaWIQk15EIlU83BtKXeQoPTAg==} engines: {node: '>=10'} + serverless-plugin-scripts@1.0.2: + resolution: {integrity: sha512-+OL9fFz5r6BXNHfpu9MDLehS/haC0fy/T3V5uJsTfLAnNsn+PzM6BmvefUfWG372hBT7piTbywB1Vl1+4LmI5Q==} + serverless-step-functions@3.21.0: resolution: {integrity: sha512-u6rMkTQaM4z/GSSy36Q/Utl0c7zaOv3i7sqVpZfvKVSV7LH+E40tptSSKRddECFWIKVERaT14JI7Dnwx9luoCQ==} peerDependencies: @@ -6486,6 +6492,8 @@ snapshots: dependencies: lodash: 4.17.21 + serverless-plugin-scripts@1.0.2: {} + serverless-step-functions@3.21.0(serverless@4.1.11): dependencies: '@serverless/utils': 6.15.0 diff --git a/post-deploy.js b/post-deploy.js new file mode 100644 index 0000000..3175f44 --- /dev/null +++ b/post-deploy.js @@ -0,0 +1,34 @@ +import { execSync } from "child_process"; +import { writeFileSync } from "fs"; +import { parseArgs } from "util"; + +const args = process.argv; +const options = { + name: { + type: "string", + }, + region: { + type: "string", + }, +}; +const { values } = parseArgs({ + args, + options, + allowPositionals: true, +}); + +// Execute the AWS CLI command to get the CloudFormation outputs +const output = execSync( + `aws cloudformation describe-stacks --stack-name ${values.name} --region ${values.region} --query "Stacks[0].Outputs" --output json`, +); + +// Parse the output +const outputs = JSON.parse(output); + +// Find the API Gateway URL +const apiGatewayUrl = outputs.find( + (o) => o.OutputKey === "HttpApiUrl", +).OutputValue; + +// Write the URL to the .env file +writeFileSync(".env.e2e", `API_GATEWAY_URL=${apiGatewayUrl}\n`); diff --git a/serverless.ts b/serverless.ts index 9cd0ca9..10180b6 100644 --- a/serverless.ts +++ b/serverless.ts @@ -1,3 +1,5 @@ +import { env } from "process"; + import resources from "./src/resources"; import { WeddingTable } from "./src/resources/ddb"; import { stepFunctions } from "./src/stepFunctions"; @@ -20,6 +22,7 @@ const serverlessConfig: ServerlessExtended = { provider: { name: "aws", stage: "dev", + region: "us-east-1", runtime: "nodejs20.x", logs: { httpApi: { @@ -47,7 +50,20 @@ const serverlessConfig: ServerlessExtended = { }, }, - plugins: ["serverless-iam-roles-per-function", "serverless-step-functions"], + plugins: [ + "serverless-iam-roles-per-function", + "serverless-step-functions", + "serverless-plugin-scripts", + ], + + custom: { + scripts: { + hooks: { + "deploy:finalize": + "node post-deploy.js --name=${self:service} --region=${self:provider.region}", + }, + }, + }, stepFunctions,