Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PROJECT_ID=

BUILD_NOTIFICATIONS_SUB=build-notifications-subscription
CD_APPROVALS_SUB=clouddeploy-approvals-subscription
CD_OPERATIONS_SUB=clouddeploy-operations-subscription
DEPLOY_COMMANDS_SUB=deploy-commands-subscription
TOPIC_NAME=clouddeploy-approvals
10 changes: 5 additions & 5 deletions reference-architectures/cloud_deploy_flow/WebsiteDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +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. 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:
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

Expand Down
47 changes: 23 additions & 24 deletions reference-architectures/cloud_deploy_flow/WebsiteDemo/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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();
};
Expand Down Expand Up @@ -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
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"@google-cloud/pubsub": "^4.8.0",
"body-parser": "^1.20.3",
"dotenv": "^17.2.1",
"express": "^4.21.2"
}
}