Giter Club home page Giter Club logo

inject-env's Introduction

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 "$@"

inject-env's People

Contributors

anton-fomin avatar

Watchers

 avatar James Cloos avatar

Forkers

muehlemann-popp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.