Skip to content

Instantly share code, notes, and snippets.

@dantheman213
Created April 29, 2018 21:07
Show Gist options
  • Select an option

  • Save dantheman213/10954a965d5c809b907cb0993c4be718 to your computer and use it in GitHub Desktop.

Select an option

Save dantheman213/10954a965d5c809b907cb0993c4be718 to your computer and use it in GitHub Desktop.
Node JS Bootstrap Run Environment w/ run script that can inject secret env vars locally or into Docker or Kubernetes
version: '3'
services:
webapp:
container_name: webapp
build: .
volumes:
- .:/opt/app
ports:
- 8080:8080
- 5858:5858
env_file:
- ./secrets.env
restart: always
# NodeJS v6 official image
FROM node:boron
EXPOSE 8080
EXPOSE 9229
WORKDIR /opt/app
RUN npm install -g node-sass grunt grunt-cli grunt-sass
COPY . /opt/app/.
RUN npm install
RUN grunt sass
RUN grunt concat
# RUN CONTAINER
ENV DOCKER true
RUN chmod +x /opt/app/run.sh
CMD ["/opt/app/run.sh"]
#!/bin/bash
HOME_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # get the relative path to the script's dir
function injectSecrets {
# Inject secrets.env into env and run the application
if [ -f "${HOME_DIR}/secrets.env" ]; then
while IFS='' read -r line || [[ -n "$line" ]]; do
splitLine=(${line//=/ })
echo "Loading env var ${splitLine[0]}..."
eval "export ${line}"
done < "${HOME_DIR}/secrets.env"
fi
}
function main {
if ! [[ -z "${DOCKER}" ]]; then
# if script is executed inside docker container cd into target app path first
echo "Loading Docker profile..."
cd /opt/app
else
# script is running locally/natively without docker so secrets need to added to env manually
echo "Loading native/local profile..."
injectSecrets
fi
if [ "$NODE_ENV" = "dev" ]; then
eval "npm run-script debug"
else
eval "npm start"
fi
}
main
NODE_ENV="dev" # or prod
SERVER_PORT=8080
AUTHOR=""
SITE_NAME=""
FB_APP_ID=""
GS_TRACKING_ID=""
GA_TRACKING_ID=""
GM_API_KEY=""
APP_URL_DOMAIN=""
MAILGUN_API_KEY=""
MAILGUN_DOMAIN=""
SALES_EMAIL=""
SUPPORT_EMAIL=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment