diff --git a/.github/workflows/register-commands-production.yaml b/.github/workflows/register-commands-production.yaml index 161dffdb..3fb43a32 100644 --- a/.github/workflows/register-commands-production.yaml +++ b/.github/workflows/register-commands-production.yaml @@ -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 + - 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 @@ -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/wrangler-action@2.0.0 with: diff --git a/.github/workflows/register-commands-staging.yaml b/.github/workflows/register-commands-staging.yaml index 79b47b12..c8c2adc6 100644 --- a/.github/workflows/register-commands-staging.yaml +++ b/.github/workflows/register-commands-staging.yaml @@ -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 @@ -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/wrangler-action@2.0.0 with: diff --git a/config/config.ts b/config/config.ts index 1c95bcad..e81afe71 100644 --- a/config/config.ts +++ b/config/config.ts @@ -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 || "", + }; + 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..a7bd2af6 --- /dev/null +++ b/config/envVarCheck.ts @@ -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(); 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..966260a9 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 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)); }, }; diff --git a/src/register.ts b/src/register.ts index 6bd22737..8602d543 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 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 );