Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a mechanism to fail the CD when required environment variables are missing | issue#257 - 2 #285

Merged
30 changes: 27 additions & 3 deletions .github/workflows/register-commands-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@ on:
push:
branches: main
jobs:
Environment-Variables-Check:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.2
Saitharun279 marked this conversation as resolved.
Show resolved Hide resolved
- 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:
- 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
Expand All @@ -22,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: npm install
- uses: cloudflare/[email protected]
with:
Expand Down
30 changes: 27 additions & 3 deletions .github/workflows/register-commands-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@ on:
push:
branches: develop
jobs:
Environment-Variables-Check:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
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:
- 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
Expand All @@ -22,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: npm install
- uses: cloudflare/[email protected]
with:
Expand Down
42 changes: 42 additions & 0 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,48 @@ 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 = {
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 || "",
prakashchoudhary07 marked this conversation as resolved.
Show resolved Hide resolved
};
return Env;
}

configFromDotEnv();

const config = (env: env) => {
const environment: environment = {
Expand Down
22 changes: 22 additions & 0 deletions config/envVarCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { env } from "../src/typeDefinitions/default.types";
import { loadEnv } from "./config";

/**
* Validate if all the required environment variables are set to a non empty value
* else throw an error
* ---
*/
export function validateEnv() {
const env: env = loadEnv({}, false);
const missingEnvVariables = Object.keys(env).filter((key) => env[key] == "");

if (missingEnvVariables.length > 0) {
throw new Error(
`Missing environment variables: ${missingEnvVariables.join(", ")}`
);
} else {
console.log("All required environment variables are set.");
}
}

validateEnv();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Saitharun279 marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [],
"author": "",
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -107,10 +107,12 @@ export default {
return new JSONResponse(response.BAD_SIGNATURE, { status: 401 });
}
}
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) {
ctx.waitUntil(send(env));
const Env: env = loadEnv(env, true);
ctx.waitUntil(send(Env));
},
};
9 changes: 6 additions & 3 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -56,8 +57,10 @@ async function registerGuildCommands(
}
}

const env = loadEnv({}, false);

registerGuildCommands(
process.env.DISCORD_TOKEN,
process.env.DISCORD_APPLICATION_ID,
process.env.DISCORD_GUILD_ID
env.DISCORD_TOKEN,
env.DISCORD_APPLICATION_ID,
env.DISCORD_GUILD_ID
);
Loading