From 327bff86b284bbfb1112484863d822cde4e8b435 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:37:39 +0530 Subject: [PATCH 1/9] issue#257 | Madge changes to fail CD when required env are missing --- .../register-commands-production.yaml | 24 ++++++++++ .../workflows/register-commands-staging.yaml | 24 ++++++++++ config/config.ts | 45 +++++++++++++++++++ config/envVarCheck.ts | 26 +++++++++++ package.json | 3 +- src/index.ts | 8 ++-- 6 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 config/envVarCheck.ts diff --git a/.github/workflows/register-commands-production.yaml b/.github/workflows/register-commands-production.yaml index 161dffdb..df97978d 100644 --- a/.github/workflows/register-commands-production.yaml +++ b/.github/workflows/register-commands-production.yaml @@ -3,7 +3,31 @@ on: push: branches: main jobs: + Environment-Variables-Check: + runs-on: ubuntu-latest + environment: production + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v3 + with: + node-version: 18.18.2 + - run: npm install + - run: npm run env-var-check + env: + CURRENT_ENVIRONMENT: production + DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}} + DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}} + DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}} + DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}} + CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}} + CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}} + BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}} + RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}} + CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}} + IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}} + Register-Commands: + needs: [Environment-Variables-Check] runs-on: ubuntu-latest environment: production steps: diff --git a/.github/workflows/register-commands-staging.yaml b/.github/workflows/register-commands-staging.yaml index 79b47b12..cdd034e1 100644 --- a/.github/workflows/register-commands-staging.yaml +++ b/.github/workflows/register-commands-staging.yaml @@ -3,7 +3,31 @@ on: push: branches: develop jobs: + Environment-Variables-Check: + runs-on: ubuntu-latest + environment: staging + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v3 + with: + node-version: 18.18.2 + - run: npm install + - run: npm run env-var-check + env: + CURRENT_ENVIRONMENT: staging + DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}} + DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}} + DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}} + DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}} + CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}} + CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}} + BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}} + RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}} + CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}} + IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}} + Register-Commands: + needs: [Environment-Variables-Check] runs-on: ubuntu-latest environment: staging steps: diff --git a/config/config.ts b/config/config.ts index 1c95bcad..035bf20f 100644 --- a/config/config.ts +++ b/config/config.ts @@ -19,6 +19,51 @@ import { DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP, DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP, } from "../src/constants/variables"; +import { config as configFromDotEnv } from "dotenv"; + +export function loadEnv(env: env, fromWorkerEnv: boolean): env { + const Env: env = { + //if `fromWokerEnv` is true, then load from the `env` passed as argument to the function, + // else if `fromWokerEnv` is false, load from process.env + //(or set to '' if value from process.env is undefined) to avoid Error TS2322 + CURRENT_ENVIRONMENT: fromWorkerEnv + ? env.CURRENT_ENVIRONMENT + : process.env.CURRENT_ENVIRONMENT || "", + DISCORD_APPLICATION_ID: fromWorkerEnv + ? env.DISCORD_APPLICATION_ID + : process.env.DISCORD_APPLICATION_ID || "", + DISCORD_GUILD_ID: fromWorkerEnv + ? env.DISCORD_GUILD_ID + : process.env.DISCORD_GUILD_ID || "", + DISCORD_TOKEN: fromWorkerEnv + ? env.DISCORD_TOKEN + : process.env.DISCORD_TOKEN || "", + DISCORD_PUBLIC_KEY: fromWorkerEnv + ? env.DISCORD_PUBLIC_KEY + : process.env.DISCORD_PUBLIC_KEY || "", + CLOUDFLARE_API_TOKEN: fromWorkerEnv + ? env.CLOUDFLARE_API_TOKEN + : process.env.CLOUDFLARE_API_TOKEN || "", + CLOUDFLARE_ACCOUNT_ID: fromWorkerEnv + ? env.CLOUDFLARE_ACCOUNT_ID + : process.env.CLOUDFLARE_ACCOUNT_ID || "", + BOT_PRIVATE_KEY: fromWorkerEnv + ? env.BOT_PRIVATE_KEY + : process.env.BOT_PRIVATE_KEY || "", + RDS_SERVERLESS_PUBLIC_KEY: fromWorkerEnv + ? env.RDS_SERVERLESS_PUBLIC_KEY + : process.env.RDS_SERVERLESS_PUBLIC_KEY || "", + CRON_JOBS_PUBLIC_KEY: fromWorkerEnv + ? env.CRON_JOBS_PUBLIC_KEY + : process.env.CRON_JOBS_PUBLIC_KEY || "", + IDENTITY_SERVICE_PUBLIC_KEY: fromWorkerEnv + ? env.IDENTITY_SERVICE_PUBLIC_KEY + : process.env.IDENTITY_SERVICE_PUBLIC_KEY || "", + }; + return Env; +} + +configFromDotEnv(); const config = (env: env) => { const environment: environment = { diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts new file mode 100644 index 00000000..1af6684e --- /dev/null +++ b/config/envVarCheck.ts @@ -0,0 +1,26 @@ +import { env } from "../src/typeDefinitions/default.types"; +import { loadEnv } from "./config"; + +validateEnv(); + +/** + * Validate if all the required environment variables are set to a non empty value + * else throw an error + * --- + */ +export function validateEnv() { + //pass empty object as env and fromWorkerEnv = false, since this method is should get executed in github actions and not in worker + const envLoadedFromProcess: env = loadEnv({}, false); + const missingEnvVars = Object.keys(envLoadedFromProcess).filter( + (key) => envLoadedFromProcess[key] == "" + ); + + // Logging missing environment variables and throw error if any are missing + if (missingEnvVars.length > 0) { + throw new Error( + `Missing environment variables: ${missingEnvVars.join(", ")}` + ); + } else { + console.log("All required environment variables are set."); + } +} diff --git a/package.json b/package.json index ed1a11d8..763e0ab4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "format-fix": "prettier --write .", "fix": "npm run lint-fix && npm run format-fix", "ngrok": "ngrok http 8787", - "register": "ts-node-esm src/register.ts" + "register": "ts-node-esm src/register.ts", + "env-var-check": "ts-node-esm config/envVarCheck.ts" }, "keywords": [], "author": "", diff --git a/src/index.ts b/src/index.ts index dba65a39..3b6ba2db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,7 @@ import { generateInviteLink } from "./controllers/generateDiscordInvite"; import { sendProfileBlockedMessage } from "./controllers/profileHandler"; import { sendTaskUpdatesHandler } from "./controllers/taskUpdatesHandler"; -import config from "./../config/config"; +import config, { loadEnv } from "./../config/config"; const router = Router(); @@ -107,10 +107,12 @@ export default { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); } } - return router.handle(request, env, ctx); + const envLoadedFromWorker: env = loadEnv(env, true); + return router.handle(request, envLoadedFromWorker, ctx); }, async scheduled(req: Request, env: env, ctx: ExecutionContext) { - ctx.waitUntil(send(env)); + const envLoadedFromWorker: env = loadEnv(env, true); + ctx.waitUntil(send(envLoadedFromWorker)); }, }; From e29109b7fd5270e12741bd48b93cc2f6f8833c5e Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Sun, 3 Nov 2024 16:43:29 +0530 Subject: [PATCH 2/9] use loadEnv method in register.ts --- src/register.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/register.ts b/src/register.ts index 6bd22737..411c7565 100644 --- a/src/register.ts +++ b/src/register.ts @@ -14,6 +14,7 @@ import { import { config } from "dotenv"; import { DISCORD_BASE_URL } from "./constants/urls"; import { registerCommands } from "./utils/registerCommands"; +import { loadEnv } from "../config/config"; config(); @@ -56,8 +57,10 @@ async function registerGuildCommands( } } +const envLoadedFromProcess = loadEnv({}, false); + registerGuildCommands( - process.env.DISCORD_TOKEN, - process.env.DISCORD_APPLICATION_ID, - process.env.DISCORD_GUILD_ID + envLoadedFromProcess.DISCORD_TOKEN, + envLoadedFromProcess.DISCORD_APPLICATION_ID, + envLoadedFromProcess.DISCORD_GUILD_ID ); From 1802ad4fab03d75475fcccd075776d0eb83e2600 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Wed, 6 Nov 2024 14:26:59 +0530 Subject: [PATCH 3/9] rename to env in register.ts --- src/register.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/register.ts b/src/register.ts index 411c7565..8602d543 100644 --- a/src/register.ts +++ b/src/register.ts @@ -57,10 +57,10 @@ async function registerGuildCommands( } } -const envLoadedFromProcess = loadEnv({}, false); +const env = loadEnv({}, false); registerGuildCommands( - envLoadedFromProcess.DISCORD_TOKEN, - envLoadedFromProcess.DISCORD_APPLICATION_ID, - envLoadedFromProcess.DISCORD_GUILD_ID + env.DISCORD_TOKEN, + env.DISCORD_APPLICATION_ID, + env.DISCORD_GUILD_ID ); From 00bb90f15944e4e1450549daa8453eaf3056f802 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:06:00 +0530 Subject: [PATCH 4/9] renamed env in inedx.ts --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3b6ba2db..986e2c1c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,12 +107,12 @@ export default { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); } } - const envLoadedFromWorker: env = loadEnv(env, true); - return router.handle(request, envLoadedFromWorker, ctx); + const env: env = loadEnv(env, true); + return router.handle(request, env, ctx); }, async scheduled(req: Request, env: env, ctx: ExecutionContext) { - const envLoadedFromWorker: env = loadEnv(env, true); - ctx.waitUntil(send(envLoadedFromWorker)); + const env: env = loadEnv(env, true); + ctx.waitUntil(send(env)); }, }; From a7b5a825a51824127ffde1fe55589da4ba6d1f62 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:30:46 +0530 Subject: [PATCH 5/9] renamed variables --- config/envVarCheck.ts | 14 +++++++------- src/index.ts | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts index 1af6684e..ad814389 100644 --- a/config/envVarCheck.ts +++ b/config/envVarCheck.ts @@ -1,8 +1,6 @@ import { env } from "../src/typeDefinitions/default.types"; import { loadEnv } from "./config"; -validateEnv(); - /** * Validate if all the required environment variables are set to a non empty value * else throw an error @@ -10,17 +8,19 @@ validateEnv(); */ export function validateEnv() { //pass empty object as env and fromWorkerEnv = false, since this method is should get executed in github actions and not in worker - const envLoadedFromProcess: env = loadEnv({}, false); - const missingEnvVars = Object.keys(envLoadedFromProcess).filter( - (key) => envLoadedFromProcess[key] == "" + const env: env = loadEnv({}, false); + const missingEnvVariables = Object.keys(env).filter( + (key) => env[key] == "" ); // Logging missing environment variables and throw error if any are missing - if (missingEnvVars.length > 0) { + if (missingEnvVariables.length > 0) { throw new Error( - `Missing environment variables: ${missingEnvVars.join(", ")}` + `Missing environment variables: ${missingEnvVariables.join(", ")}` ); } else { console.log("All required environment variables are set."); } } + +validateEnv(); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 986e2c1c..966260a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,12 +107,12 @@ export default { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); } } - const env: env = loadEnv(env, true); - return router.handle(request, env, ctx); + const Env: env = loadEnv(env, true); + return router.handle(request, Env, ctx); }, async scheduled(req: Request, env: env, ctx: ExecutionContext) { - const env: env = loadEnv(env, true); - ctx.waitUntil(send(env)); + const Env: env = loadEnv(env, true); + ctx.waitUntil(send(Env)); }, }; From e86e85139eec0865086a791471e27da59ea1b36d Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:31:01 +0530 Subject: [PATCH 6/9] removed unnecessary comments --- config/config.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/config.ts b/config/config.ts index 035bf20f..e81afe71 100644 --- a/config/config.ts +++ b/config/config.ts @@ -23,9 +23,6 @@ import { config as configFromDotEnv } from "dotenv"; export function loadEnv(env: env, fromWorkerEnv: boolean): env { const Env: env = { - //if `fromWokerEnv` is true, then load from the `env` passed as argument to the function, - // else if `fromWokerEnv` is false, load from process.env - //(or set to '' if value from process.env is undefined) to avoid Error TS2322 CURRENT_ENVIRONMENT: fromWorkerEnv ? env.CURRENT_ENVIRONMENT : process.env.CURRENT_ENVIRONMENT || "", From 0a1523a3f63a978861bd2e224d9ff5aff0659a7d Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:33:40 +0530 Subject: [PATCH 7/9] removed redundant comments --- config/envVarCheck.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts index ad814389..114785af 100644 --- a/config/envVarCheck.ts +++ b/config/envVarCheck.ts @@ -7,13 +7,11 @@ import { loadEnv } from "./config"; * --- */ export function validateEnv() { - //pass empty object as env and fromWorkerEnv = false, since this method is should get executed in github actions and not in worker const env: env = loadEnv({}, false); const missingEnvVariables = Object.keys(env).filter( (key) => env[key] == "" ); - // Logging missing environment variables and throw error if any are missing if (missingEnvVariables.length > 0) { throw new Error( `Missing environment variables: ${missingEnvVariables.join(", ")}` From 84b2a251ec6e3e4f27f7b9bdd830281b6466d4fc Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:42:59 +0530 Subject: [PATCH 8/9] fixed code format --- .github/workflows/register-commands-production.yaml | 10 +++++----- .github/workflows/register-commands-staging.yaml | 10 +++++----- config/envVarCheck.ts | 4 +--- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/.github/workflows/register-commands-production.yaml b/.github/workflows/register-commands-production.yaml index df97978d..3fb43a32 100644 --- a/.github/workflows/register-commands-production.yaml +++ b/.github/workflows/register-commands-production.yaml @@ -7,8 +7,8 @@ jobs: runs-on: ubuntu-latest environment: production steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -31,8 +31,8 @@ jobs: runs-on: ubuntu-latest environment: production steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -46,7 +46,7 @@ jobs: runs-on: ubuntu-latest environment: production steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: npm install - uses: cloudflare/wrangler-action@2.0.0 with: diff --git a/.github/workflows/register-commands-staging.yaml b/.github/workflows/register-commands-staging.yaml index cdd034e1..c8c2adc6 100644 --- a/.github/workflows/register-commands-staging.yaml +++ b/.github/workflows/register-commands-staging.yaml @@ -7,8 +7,8 @@ jobs: runs-on: ubuntu-latest environment: staging steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -31,8 +31,8 @@ jobs: runs-on: ubuntu-latest environment: staging steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -46,7 +46,7 @@ jobs: runs-on: ubuntu-latest environment: staging steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: npm install - uses: cloudflare/wrangler-action@2.0.0 with: diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts index 114785af..05985fa9 100644 --- a/config/envVarCheck.ts +++ b/config/envVarCheck.ts @@ -8,9 +8,7 @@ import { loadEnv } from "./config"; */ export function validateEnv() { const env: env = loadEnv({}, false); - const missingEnvVariables = Object.keys(env).filter( - (key) => env[key] == "" - ); + const missingEnvVariables = Object.keys(env).filter((key) => env[key] == ""); if (missingEnvVariables.length > 0) { throw new Error( From a9e0c52455bbceee3bceed6c56b7c85b2e64b5d9 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 01:46:36 +0530 Subject: [PATCH 9/9] format fix --- config/envVarCheck.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts index 05985fa9..a7bd2af6 100644 --- a/config/envVarCheck.ts +++ b/config/envVarCheck.ts @@ -19,4 +19,4 @@ export function validateEnv() { } } -validateEnv(); \ No newline at end of file +validateEnv();