Merge pull request #814 from trgwii/feature/docker-env-secrets-from-files

Support reading env_FILE keys from file paths, closes #813
This commit is contained in:
Pouria 2025-02-06 10:54:15 +03:30 committed by GitHub
commit 86e4ba82fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
require("dotenv").config();
const { cleanEnv, num, str, bool } = require("envalid");
const { readFileSync } = require("node:fs");
const supportedDBClients = [
"pg",
@ -25,7 +26,7 @@ if (process.argv.includes("--production")) {
process.env.NODE_ENV = "production";
}
const env = cleanEnv(process.env, {
const spec = {
PORT: num({ default: 3000 }),
SITE_NAME: str({ example: "Kutt", default: "Kutt" }),
DEFAULT_DOMAIN: str({ example: "kutt.it", default: "localhost:3000" }),
@ -64,6 +65,18 @@ const env = cleanEnv(process.env, {
REPORT_EMAIL: str({ default: "" }),
CONTACT_EMAIL: str({ default: "" }),
NODE_APP_INSTANCE: num({ default: 0 }),
});
};
for (const key in spec) {
const file_key = key + '_FILE';
if (!(file_key in process.env)) continue;
try {
process.env[key] = readFileSync(process.env[file_key], 'utf8').trim();
} catch {
// on error, env_FILE just doesn't get applied.
}
}
const env = cleanEnv(process.env, spec);
module.exports = env;