Skip to content

Commit

Permalink
target env for deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
ais-one committed Nov 9, 2023
1 parent 9442d04 commit 9f9ab62
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 51 deletions.
28 changes: 25 additions & 3 deletions .github/workflows/deploy-cr.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# This is a basic workflow that is manually triggered
# This manually triggered workflow deploys front end application to a object store bucket
# try to use as much shell scripting a possible
# actions appear from default branch - https://github.community/t/workflow-files-only-picked-up-from-master/16129/2
#
# Supported Container Registries:
# - [to test] Aliyun OSS
# - [backlog] AWS S3
# - [backlog] Azure
# - [backlog] GCP Cloud Storage
#
# Setup the following secrets
# - CR_USERNAME
# - CR_PASSWORD
# Setup the following vars
# - CR_HOST
# - CR_NS
# - CR_IMAGENAME
# Specify the following during build
# - tag_gr
# - cr_env (dev, uat, prd)
#
name: CR deployment

on:
Expand All @@ -23,6 +41,10 @@ on:
description: 'path to dockerfile'
default: '.'
required: false
cr_env:
description: 'environment: dev, uat, stg, prd'
default: 'dev'
required: true
env:
CR_IMAGENAME: ${{ github.event.inputs.cr_imagename || vars.CR_IMAGENAME || github.event.repository.name }}
CR_PASSWORD: ${{ secrets.CR_PASSWORD }}
Expand Down Expand Up @@ -51,8 +73,8 @@ jobs:
echo $CR_PASSWORD | docker login --username $CR_USERNAME --password-stdin $CR_HOST
docker build -t $CR_HOST/$CR_NS/$CR_IMAGENAME:${{ github.event.inputs.tag_ci }} \
--target $DOCKERFILE_TARGET \
--build-arg ARG_NODE_ENV=$DOCKERFILE_TARGET \
--build-arg ARG_NODE_ENV=${{ github.event.inputs.cr_env }} \
--build-arg ARG_API_PORT=3000 \
$DOCKERFILE_PATH || exit 1001
docker push $CR_HOST/$CR_NS/$CR_IMAGENAME:${{ github.event.inputs.tag_ci }}
docker push $CR_HOST/$CR_NS/$CR_IMAGENAME-${{ github.event.inputs.cr_env }}:${{ github.event.inputs.tag_ci }}
docker logout
68 changes: 34 additions & 34 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"use strict";
'use strict';

const url = require("url");
const http = require("http");
const https = require("https");
const express = require("express");
const url = require('url');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();

// using CJS in ESM sibling-module.js is a CommonJS module
// import { createRequire } from 'module'
// const require = createRequire(import.meta.url)
// const siblingModule = require('./sibling-module')

const { sleep } = require("esm")(module)("@es-labs/esm/sleep");
const { sleep } = require('esm')(module)('@es-labs/esm/sleep');

require("@es-labs/node/express/init")();
require('@es-labs/node/express/init')();

// setup graceful exit
const handleExitSignal = async (signal) => await cleanup(`Signal ${signal}`, 0); // NOSONAR
Expand All @@ -24,11 +24,11 @@ const handleExitException = async (err, origin) =>
); // NOSONAR
const handleExitRejection = async (reason, promise) =>
await cleanup(`Unhandled Rejection. reason: ${reason?.stack || reason}`, 1); // NOSONAR
process.on("SIGINT", handleExitSignal);
process.on("SIGTERM", handleExitSignal);
process.on("SIGQUIT", handleExitSignal);
process.on("uncaughtException", handleExitException);
process.on("unhandledRejection", handleExitRejection);
process.on('SIGINT', handleExitSignal);
process.on('SIGTERM', handleExitSignal);
process.on('SIGQUIT', handleExitSignal);
process.on('uncaughtException', handleExitException);
process.on('unhandledRejection', handleExitRejection);

const { HTTPS_PRIVATE_KEY, HTTPS_CERTIFICATE } = process.env;
const https_opts = {};
Expand All @@ -46,10 +46,10 @@ const server = HTTPS_CERTIFICATE

// USERLAND - Add APM tool

require("@es-labs/node/express/preRoute")(app, express);
const graphqlWsServer = require("@es-labs/node/express/graphql")(app, server);
const services = require("@es-labs/node/services");
const authService = require("@es-labs/node/auth");
require('@es-labs/node/express/preRoute')(app, express);
const graphqlWsServer = require('@es-labs/node/express/graphql')(app, server);
const services = require('@es-labs/node/services');
const authService = require('@es-labs/node/auth');

// CLEANUP
const cleanup = async (
Expand All @@ -75,17 +75,17 @@ const cleanup = async (
}
});
}
console.log("cleaning up and awaiting exit...");
console.log('cleaning up and awaiting exit...');
await sleep(timeOutMs); // from here on... does not get called on uncaught exception crash
console.log("exiting"); // require('fs').writeSync(process.stderr.fd, `bbbbbbbbbbbb`)
console.log('exiting'); // require('fs').writeSync(process.stderr.fd, `bbbbbbbbbbbb`)
return coreDump ? process.abort : process.exit(exitCode);
// setTimeout(() => console.log('exiting'), timeOutMs).unref()
};

// SERVICES
services.start()
try {
authService.setup(services.get("keyv"), services.get("knex1")); // setup authorization
authService.setup(services.get('keyv'), services.get('knex1')); // setup authorization
} catch (e) {
console.log(e)
}
Expand Down Expand Up @@ -134,13 +134,13 @@ Layer.prototype.handle_request = function(req, res, next) {
}

try {
require(`./apps/apploader`)(app); // add your APIs here
require("./router")(app); // common routes
app.use("/api/**", (req, res) =>
res.status(404).json({ error: "Not Found" })
require('./apps/apploader')(app); // add your APIs here
require('./router')(app); // common routes
app.use('/api/**', (req, res) =>
res.status(404).json({ error: 'Not Found' })
);
} catch (e) {
console.log("Route loading exception", e.toString())
console.log('Route loading exception', e.toString())
}
// END ROUTES

Expand All @@ -149,36 +149,36 @@ const { OPENAPI_OPTIONS } = process.env
const openApiOptions = JSON.parse(OPENAPI_OPTIONS || null)
if (openApiOptions) {
openApiOptions.baseDir = __dirname
const expressJSDocSwagger = require("express-jsdoc-swagger");
const expressJSDocSwagger = require('express-jsdoc-swagger');
expressJSDocSwagger(app)(openApiOptions);
}

// websockets
server.on("upgrade", (request, socket, head) => {
server.on('upgrade', (request, socket, head) => {
const pathname = url.parse(request.url).pathname;
if (pathname === "/subscriptions") {
if (pathname === '/subscriptions') {
// upgrade the graphql server
graphqlWsServer.handleUpgrade(request, socket, head, (ws) => {
graphqlWsServer.emit("connection", ws, request);
graphqlWsServer.emit('connection', ws, request);
});
}
console.log("upgrade event");
console.log('upgrade event');
});

require("@es-labs/node/express/postRoute")(app, express);
require('@es-labs/node/express/postRoute')(app, express);

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
// 'Bad Request': 400, 'Unauthorized': 401, 'Forbidden': 403, 'Not Found': 404, 'Conflict': 409, 'Unprocessable Entity': 422, 'Internal Server Error': 500,
app.use((error, req, res, next) => {
// error middleware - 200s should not reach here
// console.log('typeof error', error instanceof Error)
console.log("error middleware", error);
let message = "Unknown Error";
console.log('error middleware', error);
let message = 'Unknown Error';
if (error.message) {
// console.log('Error Object', error.name, error.name, error.stack)
message =
process.env.NODE_ENV === "development" ? error.stack : error.message;
} else if (typeof error === "string") {
process.env.NODE_ENV === 'development' ? error.stack : error.message;
} else if (typeof error === 'string') {
message = error;
} else if (error?.toString) {
message = error.toString();
Expand Down
Loading

0 comments on commit 9f9ab62

Please sign in to comment.