Skip to content

Commit

Permalink
fix: env config
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspearson committed Mar 4, 2022
1 parent 025f068 commit ae4c366
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
37 changes: 25 additions & 12 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,45 @@ on:
release:
types: [published]

env:
DOCKER_CONTEXT_PATH: .
DOCKERFILE: Dockerfile
PACKAGE: node.express.starter

jobs:
docker:
name: docker build and publish
runs-on: ubuntu-latest
steps:
- name: checkout
- name: Checkout git repository
uses: actions/checkout@v3
- name: set release version
id: vars
run: echo ::set-output name=tag::$(echo ${{ github.event.release.tag_name }} | cut -d "v" -f 2)
- name: yarn install, and build
- name: Install and build
run: |
yarn install
yarn build
- name: set up docker buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: login to dockerhub
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: build and push
id: docker_build
uses: docker/build-push-action@v2
registry: ghcr.io
username: nicolaspearson
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push latest version
uses: docker/build-push-action@master
with:
build-args: |
GITHUB_SHA=${{ github.sha }}
PACKAGE=${{ env.PACKAGE }}
cache-from: type=registry,ref=ghcr.io/nicolaspearson/${{ env.PACKAGE }}
cache-to: type=inline
context: ${{ env.DOCKER_CONTEXT_PATH }}
file: ${{ env.DOCKERFILE }}
platforms: linux/amd64
push: true
tags: nicpearson/node.express.starter:${{ steps.vars.outputs.tag }}
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
tags: |
ghcr.io/nicolaspearson/${{ env.PACKAGE }}:latest
ghcr.io/nicolaspearson/${{ env.PACKAGE }}:${{ steps.vars.outputs.tag }}
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
- TYPEORM_PASSWORD=masterkey
- TYPEORM_PORT=5432
- TYPEORM_SCHEMA=public
- TYPEORM_USERNAME=master
- TYPEORM_USERNAME=admin
ports:
- 3000:3000
depends_on:
Expand Down
29 changes: 16 additions & 13 deletions src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi from 'joi';
import path from 'path';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import { resolve } from 'path';

import { Environment } from '@/common/enums/environment.enum';

Expand All @@ -13,27 +13,30 @@ export const environments: Environment[] = [
];

export function init(options: Api.ConfigOptions): void {
const config = loadEnvFile(options);
const config = {
...loadEnvFile(options),
...process.env,
};
const validationOptions: Joi.ValidationOptions = {
abortEarly: false,
allowUnknown: false,
allowUnknown: true,
};
const { error, value: validatedConfig } = getValidationSchema().validate(
const validationResult: Joi.ValidationResult = getValidationSchema().validate(
config,
validationOptions
);
if (error) {
throw new Error(`Config validation error: ${error.message}`);
if (validationResult.error) {
throw new Error(`Config validation error: ${validationResult.error.message}`);
}
assignVariablesToProcess(validatedConfig);
assignVariablesToProcess(validationResult.value);
}

function assignVariablesToProcess(config: Record<string, string>) {
if (!(config !== null && typeof config === 'object')) {
return;
}
const keys = Object.keys(config).filter((key) => !(key in process.env));
keys.forEach((key) => (process.env[key] = config[key]));
for (const key of keys) process.env[key] = config[key];
}

function getValidationSchema(): Joi.ObjectSchema {
Expand Down Expand Up @@ -102,14 +105,14 @@ function getValidationSchema(): Joi.ObjectSchema {
}

function loadEnvFile(options: Api.ConfigOptions): dotenv.DotenvParseOutput {
const envFilePaths = Array.isArray(options.envFilePath)
const environmentFilePaths = Array.isArray(options.envFilePath)
? options.envFilePath
: [options.envFilePath || resolve(process.cwd(), '.env')];
: [options.envFilePath || path.resolve(process.cwd(), '.env')];

let config: ReturnType<typeof dotenv.parse> = {};
for (const envFilePath of envFilePaths) {
if (fs.existsSync(envFilePath)) {
config = Object.assign(dotenv.parse(fs.readFileSync(envFilePath)), config);
for (const environmentFilePath of environmentFilePaths) {
if (fs.existsSync(environmentFilePath)) {
config = Object.assign(dotenv.parse(fs.readFileSync(environmentFilePath)), config);
}
}
return config;
Expand Down

0 comments on commit ae4c366

Please sign in to comment.