Skip to content

Latest commit

 

History

History
76 lines (51 loc) · 1.99 KB

README.md

File metadata and controls

76 lines (51 loc) · 1.99 KB

inject-env

Simple tool to dump env variables to json, potentially filtering by prefix, stripping it and also with ability to put this json into placeholder in existing file (which is useful to pass settings from environment variables in staticfiles builds).

Usage examples

For example we define settings for our application in environment variables

# env
APP_ENV_API_URL=https://example.com/api
APP_ENV_SENTRY_URL=https://sentry.com
SOME_UNSAFE_ENV_VAR=secret

We can inject these settings into our static application by replacing some placeholder. First we put a placeholder into an html file.

<script>/*APP_ENV*/</script>

Replace /*APP_ENV*/ with window.APP_ENV = {...environment variables values...} in index.html

inject-env -o index.html -r '/*APP_ENV*/' --prefix APP_ENV_ --format 'window.APP_ENV = {}'

The result html should be

<scritp>window.APP_ENV = {"API_URL":"https://example.com/api", "SENTRY_URL": "https://sentry.com"}</script>

Only environment variables with prefix end up in the result json. For security reasons you should always use prefix to avoid secrets in your publicly accessible files.

See inject-env -h for more options

Example nginx Dockerfile

FROM node:16-alpine as builder

WORKDIR /app

COPY package.json package-lock.json /app/

RUN npm ci

COPY . /app

RUN npm run build

FROM nginx:1.23-alpine

RUN wget -O inject-env.tar.gz https://github.com/anton-fomin/inject-env/releases/download/v0.1.1/inject-env-v0.1.1-x86_64-unknown-linux-musl.tar.gz && \
  tar -xzf inject-env.tar.gz && mv inject-env /usr/local/bin/

COPY deploy/bin/entrypoint.sh /
RUN chmod +x /entrypoint.sh
COPY deploy/nginx/default.conf /etc/nginx/conf.d/default.conf

ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 80

COPY --from=builder /app/dist /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

Where entrypoint is

#!/bin/sh
inject-env -o /usr/share/nginx/html/index.html -r '/*APP_ENV*/' --prefix APP_ENV_ --format 'window.APP_ENV = {}'
exec "$@"