fix(docker): simplify env handling for Docker builds

- Always read system environment variables first, then override with .env file
- Ensure all required keys are always present in generated types
- Pass environment variables correctly in Docker build
- Update .dockerignore to exclude build artifacts
This commit is contained in:
Johannes Millan 2025-08-09 12:16:31 +02:00
parent a3ebf37cc7
commit 75afe06b69
3 changed files with 58 additions and 38 deletions

View file

@ -1,3 +1,14 @@
node_modules
.git
.gitignore
.angular
dist
app
release
backup
*.log
.idea
.vscode
coverage
e2e-results
tmp

View file

@ -32,7 +32,8 @@ RUN npm run prepare
# Copy source and build
COPY . .
RUN npm run env && npm run lint && npm run buildFrontend:prodWeb
# Pass build args as environment variables for the build commands
RUN UNSPLASH_KEY=$UNSPLASH_KEY UNSPLASH_CLIENT_ID=$UNSPLASH_CLIENT_ID npm run env && npm run lint && npm run buildFrontend:prodWeb
# Production stage
FROM nginx:1-alpine

View file

@ -4,39 +4,55 @@ const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
// Load .env file
const REQUIRED_ENV_KEYS = [
'UNSPLASH_KEY',
'UNSPLASH_CLIENT_ID',
// 'GOOGLE_DRIVE_TOKEN',
// 'DROPBOX_API_KEY',
// 'WEBDAV_URL',
// 'WEBDAV_USERNAME',
// 'WEBDAV_PASSWORD',
];
// Start with system environment variables for required keys
const env = {};
REQUIRED_ENV_KEYS.forEach((key) => {
if (process.env[key]) {
env[key] = process.env[key];
}
});
// Load and override with .env file values if they exist
const envPath = path.join(process.cwd(), '.env');
const envConfig = dotenv.config({ path: envPath });
let env = envConfig.parsed || {};
// In CI environment, also read from process.env for specific keys
if (process.env.CI === 'true' || !envConfig.parsed) {
console.log('🔍 CI environment detected, checking for environment variables...');
// List of environment variables to check for in CI
const ciEnvKeys = [
'UNSPLASH_KEY',
'UNSPLASH_CLIENT_ID',
'GOOGLE_DRIVE_TOKEN',
'DROPBOX_API_KEY',
'WEBDAV_URL',
'WEBDAV_USERNAME',
'WEBDAV_PASSWORD',
];
ciEnvKeys.forEach((key) => {
if (process.env[key]) {
env[key] = process.env[key];
console.log(` ✓ Found ${key} in environment`);
}
});
if (envConfig.parsed) {
Object.assign(env, envConfig.parsed);
}
if (Object.keys(env).length === 0) {
console.warn('⚠️ No environment variables found, generating empty env.generated.ts');
// Log what we found
const foundKeys = Object.keys(env).filter((key) => REQUIRED_ENV_KEYS.includes(key));
if (foundKeys.length > 0) {
console.log(
`✅ Found ${foundKeys.length} environment variable(s): ${foundKeys.join(', ')}`,
);
} else {
console.warn(
'⚠️ No environment variables found, generating env.generated.ts with undefined values',
);
}
// Create ENV object with all expected keys (undefined if not set)
const envEntries = REQUIRED_ENV_KEYS.map((key) => {
const value = env[key];
if (value !== undefined) {
// Escape quotes in values
const escapedValue = value.replace(/'/g, "\\'");
return ` ${key}: '${escapedValue}',`;
} else {
return ` ${key}: undefined,`;
}
});
// Generate TypeScript content
const tsContent = `// This file is auto-generated by tools/load-env.js
// Do not modify directly - edit .env file instead
@ -47,13 +63,7 @@ const tsContent = `// This file is auto-generated by tools/load-env.js
* Access these constants instead of process.env in your Angular app
*/
export const ENV = {
${Object.entries(env)
.map(([key, value]) => {
// Escape quotes in values
const escapedValue = value.replace(/'/g, "\\'");
return ` ${key}: '${escapedValue}',`;
})
.join('\n')}
${envEntries.join('\n')}
} as const;
// Type-safe helper to ensure all expected env vars are defined
@ -70,9 +80,7 @@ if (!fs.existsSync(configDir)) {
const outputPath = path.join(configDir, 'env.generated.ts');
fs.writeFileSync(outputPath, tsContent);
console.log(
`✅ Generated ${outputPath} with ${Object.keys(env).length} environment variables`,
);
console.log(`✅ Generated ${outputPath}`);
// Pass through to the next command
if (process.argv.length > 2) {