From c343d8d6a75a2b3b212c91f05ad5ccd2b1244ee4 Mon Sep 17 00:00:00 2001 From: Steve McGhee Date: Fri, 8 Aug 2025 14:03:56 -0700 Subject: [PATCH 1/4] chore: update README and code to use environment variables for configuration --- .../cloud_deploy_flow/WebsiteDemo/.env.sample | 6 +++ .../cloud_deploy_flow/WebsiteDemo/README.md | 3 +- .../cloud_deploy_flow/WebsiteDemo/index.js | 49 +++++++++---------- .../WebsiteDemo/package-lock.json | 13 +++++ .../WebsiteDemo/package.json | 1 + 5 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample new file mode 100644 index 00000000..796bf9bf --- /dev/null +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample @@ -0,0 +1,6 @@ +PROJECT_ID= +TOPIC_NAME=clouddeploy-approvals +BUILD_NOTIFICATIONS_SUB=build-notifications-subscription +DEPLOY_COMMANDS_SUB=deploy-commands-subscription +CD_OPERATIONS_SUB=clouddeploy-operations-subscription +CD_APPROVALS_SUB=clouddeploy-approvals-subscription diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md index 451ffcdd..391daebf 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md @@ -30,8 +30,7 @@ demonstration purposes and is not intended for production use. npm install -2. Set your Google Cloud project ID and subscription names in the `index.js` - file. +2. Create a `.env` file and populate it with the environment variables found in `.env.sample` 3. Start the server: diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js index 2783e6e7..a12c5fbc 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js @@ -1,30 +1,30 @@ +require('dotenv').config(); + const {PubSub} = require('@google-cloud/pubsub'); const express = require('express'); const path = require('path'); const app = express(); -const port = 8080; -const topicName = 'clouddeploy-approvals'; +const port = process.env.PORT || 8080; +const topicName = process.env.TOPIC_NAME || 'clouddeploy-approvals'; // Replace with your actual project ID and subscription names -const projectId = 'crash-demo-env'; +const projectId = process.env.PROJECT_ID; const subscriptionNames = [ - 'build_notifications_subscription', - 'deploy-commands-subscription', - 'clouddeploy-operations-subscription', - 'clouddeploy-approvals-subscription', + process.env.BUILD_NOTIFICATIONS_SUB || 'build_notifications_subscription', + process.env.DEPLOY_COMMANDS_SUB || 'deploy-commands-subscription', + process.env.CD_OPERATIONS_SUB || 'clouddeploy-operations-subscription', + process.env.CD_APPROVALS_SUB || 'clouddeploy-approvals-subscription', ]; const timeout = 60; const pubsubClient = new PubSub({projectId}); // Store messages per subscription -let messages = { - 'build_notifications_subscription': [], - 'deploy-commands-subscription': [], - 'clouddeploy-operations-subscription': [], - 'clouddeploy-approvals-subscription': [], -}; +let messages = {}; +subscriptionNames.forEach(name => { + messages[name] = []; +}); // Function to pull messages from each subscription async function pullMessages(pubSubClient, subscriptionName) { @@ -35,11 +35,13 @@ async function pullMessages(pubSubClient, subscriptionName) { console.log(`\tData: ${message.data}`); console.log(`\tAttributes: ${JSON.stringify(message.attributes)}`); - messages[subscriptionName].push({ - id: message.id, - data: message.data.toString(), - attributes: message.attributes, - }); + if (messages[subscriptionName]) { + messages[subscriptionName].push({ + id: message.id, + data: message.data.toString(), + attributes: message.attributes, + }); + } message.ack(); }; @@ -71,12 +73,9 @@ async function main() { // Endpoint to clear all messages app.post('/clear-messages', (req, res) => { // Clear the messages from all subscriptions - messages = { - 'build_notifications_subscription': [], - 'deploy-commands-subscription': [], - 'clouddeploy-operations-subscription': [], - 'clouddeploy-approvals-subscription': [], - }; + for (const subscriptionName in messages) { + messages[subscriptionName] = []; + } console.log('All messages cleared.'); res.sendStatus(200); // Respond with success }); @@ -112,4 +111,4 @@ async function main() { }); } -main().catch(console.error); +main().catch(console.error); \ No newline at end of file diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/package-lock.json b/reference-architectures/cloud_deploy_flow/WebsiteDemo/package-lock.json index 93d50b5d..20990ecb 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/package-lock.json +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/package-lock.json @@ -7,6 +7,7 @@ "dependencies": { "@google-cloud/pubsub": "^4.8.0", "body-parser": "^1.20.3", + "dotenv": "^17.2.1", "express": "^4.21.2" } }, @@ -517,6 +518,18 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "17.2.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.1.tgz", + "integrity": "sha512-kQhDYKZecqnM0fCnzI5eIv5L4cAe/iRI+HqMbO/hbRdTAeXDG+M9FjipUxNfbARuEg4iHIbhnhs78BCHNbSxEQ==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/duplexify": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-4.1.3.tgz", diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/package.json b/reference-architectures/cloud_deploy_flow/WebsiteDemo/package.json index 7c1e2175..02366c6d 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/package.json +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/package.json @@ -2,6 +2,7 @@ "dependencies": { "@google-cloud/pubsub": "^4.8.0", "body-parser": "^1.20.3", + "dotenv": "^17.2.1", "express": "^4.21.2" } } From 6048307bd9b49e1763497970300d99a04244e217 Mon Sep 17 00:00:00 2001 From: Steve McGhee Date: Fri, 8 Aug 2025 14:26:51 -0700 Subject: [PATCH 2/4] lint fixes --- .../cloud_deploy_flow/WebsiteDemo/.env.sample | 5 +++-- .../cloud_deploy_flow/WebsiteDemo/README.md | 9 +++++---- .../cloud_deploy_flow/WebsiteDemo/index.js | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample index 796bf9bf..03c1a02b 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample @@ -1,6 +1,7 @@ PROJECT_ID= -TOPIC_NAME=clouddeploy-approvals + BUILD_NOTIFICATIONS_SUB=build-notifications-subscription -DEPLOY_COMMANDS_SUB=deploy-commands-subscription CD_OPERATIONS_SUB=clouddeploy-operations-subscription CD_APPROVALS_SUB=clouddeploy-approvals-subscription +DEPLOY_COMMANDS_SUB=deploy-commands-subscription +TOPIC_NAME=clouddeploy-approvals \ No newline at end of file diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md index 391daebf..77de157c 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md @@ -26,17 +26,18 @@ demonstration purposes and is not intended for production use. ## Installation -1. Install the required dependencies: +1. Install the required dependencies: npm install -2. Create a `.env` file and populate it with the environment variables found in `.env.sample` +2. Create a `.env` file and populate it with the environment + variables found in `.env.sample` -3. Start the server: +3. Start the server: node index.js -4. Open your web browser and go to `http://localhost:8080` to access the demo. +4. Open your web browser and go to `http://localhost:8080` to access the demo. ## Usage diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js index a12c5fbc..176b7cda 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js @@ -111,4 +111,4 @@ async function main() { }); } -main().catch(console.error); \ No newline at end of file +main().catch(console.error); From 6aa1ac4c032eae6f9946e28370f4c7a83786aa82 Mon Sep 17 00:00:00 2001 From: Steve McGhee Date: Fri, 8 Aug 2025 15:35:29 -0700 Subject: [PATCH 3/4] moar lint --- .../cloud_deploy_flow/WebsiteDemo/.env.sample | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample index 03c1a02b..87fbf95a 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/.env.sample @@ -1,7 +1,7 @@ PROJECT_ID= BUILD_NOTIFICATIONS_SUB=build-notifications-subscription -CD_OPERATIONS_SUB=clouddeploy-operations-subscription CD_APPROVALS_SUB=clouddeploy-approvals-subscription +CD_OPERATIONS_SUB=clouddeploy-operations-subscription DEPLOY_COMMANDS_SUB=deploy-commands-subscription -TOPIC_NAME=clouddeploy-approvals \ No newline at end of file +TOPIC_NAME=clouddeploy-approvals From 80aec3a4d9f301e58bc3e59f880191c8ae4e6367 Mon Sep 17 00:00:00 2001 From: Steve McGhee Date: Wed, 13 Aug 2025 11:34:11 -0700 Subject: [PATCH 4/4] lint --- .../cloud_deploy_flow/WebsiteDemo/README.md | 4 ++-- .../cloud_deploy_flow/WebsiteDemo/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md index 77de157c..9ad611b4 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md @@ -30,8 +30,8 @@ demonstration purposes and is not intended for production use. npm install -2. Create a `.env` file and populate it with the environment - variables found in `.env.sample` +2. Create a `.env` file and populate it with the environment variables found in + `.env.sample` 3. Start the server: diff --git a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js index 176b7cda..258c5532 100644 --- a/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js +++ b/reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js @@ -22,7 +22,7 @@ const pubsubClient = new PubSub({projectId}); // Store messages per subscription let messages = {}; -subscriptionNames.forEach(name => { +subscriptionNames.forEach((name) => { messages[name] = []; }); @@ -36,7 +36,7 @@ async function pullMessages(pubSubClient, subscriptionName) { console.log(`\tAttributes: ${JSON.stringify(message.attributes)}`); if (messages[subscriptionName]) { - messages[subscriptionName].push({ + messages[subscriptionName].push({ id: message.id, data: message.data.toString(), attributes: message.attributes,