Skip to content

Commit

Permalink
build: deploy using docker with dynamic env vars
Browse files Browse the repository at this point in the history
closes #38
  • Loading branch information
juancarlosfarah committed Mar 17, 2021
1 parent ca8f7e7 commit 6ad7211
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ cypress/screenshots
cypress/videos
/.nyc_output
/coverage


# jetbrains
.idea
10 changes: 10 additions & 0 deletions .productioncontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:
app:
build:
context: ..
dockerfile: Dockerfile.prod
env_file:
- ../.env.production
ports:
- '80:80'
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM mhart/alpine-node:12

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# install app dependencies
COPY package.json /usr/src/app/
RUN yarn install

# bundle app source
COPY . /usr/src/app

EXPOSE 3000

CMD ["yarn", "start"]
13 changes: 13 additions & 0 deletions Dockerfile.build.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM mhart/alpine-node:12

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# install app dependencies
COPY package.json /usr/src/app/
RUN yarn install

# bundle app source
COPY . /usr/src/app

CMD ["yarn", "build"]
13 changes: 13 additions & 0 deletions Dockerfile.build.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM mhart/alpine-node:12

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# install app dependencies
COPY package.json /usr/src/app/
RUN yarn install

# bundle app source
COPY . /usr/src/app

CMD ["yarn", "build"]
27 changes: 27 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# stage1 - build environment
FROM node:14-alpine as build

RUN apk add --no-cache ca-certificates jq

WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY ./package.json /app/
COPY ./yarn.lock /app/
RUN yarn
COPY . /app
RUN jq 'to_entries | map_values({ (.key) : ("$" + .key) }) | reduce .[] as $item ({}; . + $item)' ./src/env.json > ./src/env.tmp.json && mv ./src/env.tmp.json ./src/env.json
RUN yarn build

# stage 2 - production environment
FROM nginx:stable-alpine

RUN apk add --no-cache ca-certificates bash

ENV JSFOLDER=/usr/share/nginx/html/static/js/*.js
COPY ./scripts/start.sh /usr/bin/start.sh
RUN chmod +x /usr/bin/start.sh
COPY --from=build /app/build /usr/share/nginx/html
# override default configuration
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["start.sh"]
21 changes: 21 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {

listen 80;

location / {
root /usr/share/nginx/html;
index index.html index.htm;

# to redirect all the requests to index.html,
# useful when you are using react-router

try_files $uri $uri/ /index.html;
}

error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;
}

}
7 changes: 7 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
export EXISTING_VARS=$(printenv | awk -F= '{print $1}' | sed 's/^/\$/g' | paste -sd,);
for file in $JSFOLDER;
do
envsubst $EXISTING_VARS < $file > file.tmp && mv file.tmp $file
done
nginx -g 'daemon off;'
5 changes: 4 additions & 1 deletion src/config/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { API_HOST as API_HOST_ENV } from '../env.json';

export const APP_NAME = 'Graasp';

export const API_HOST = process.env.REACT_APP_API_HOST || 'default';
export const API_HOST =
API_HOST_ENV || process.env.REACT_APP_API_HOST || 'http://localhost:3111';

export const DESCRIPTION_MAX_LENGTH = 30;

Expand Down
3 changes: 3 additions & 0 deletions src/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"API_HOST": false
}

0 comments on commit 6ad7211

Please sign in to comment.