Skip to content

Commit 6cd17bb

Browse files
committed
Modified application tooling to allow for stateless production builds. Environment variables will be set by AWS ECS and at container startup they will be injected into the application using a sequence of scripts integrated in the Dockerfile. This can be tested locally by using a .env.docker.local file in the .env folder and then building and running the docker image locally. This setup will allow the continued normal development using the npm run dev command. Stateful build information has been removed from the package.json as well as the corresponding .env files that are no longer necessary.
1 parent eae37b9 commit 6cd17bb

File tree

14 files changed

+112
-78
lines changed

14 files changed

+112
-78
lines changed

.dockerignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
*/.git
33
node_modules
44
*/node_modules
5-
npm-debug.log
5+
npm-debug.log
6+
dist
7+
.swc
8+
.env
9+
!.env/.env

.env/.env

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
VITE_UNITY_UI_VERSION=${npm_package_version}
1+
# GENERAL
2+
VITE_UNITY_UI_VERSION=${npm_package_version}
3+
VITE_ADMIN_EMAIL=ENV_UNITY_UI_ADMIN_EMAIL
4+
5+
# Auth
6+
VITE_AUTH_OAUTH_CLIENT_ID=ENV_UNITY_UI_AUTH_OAUTH_CLIENT_ID
7+
VITE_AUTH_OAUTH_REDIRECT_URI=ENV_UNITY_UI_AUTH_OAUTH_REDIRECT_URI
8+
VITE_AUTH_OAUTH_LOGOUT_ENDPOINT=ENV_UNITY_UI_AUTH_OAUTH_LOGOUT_ENDPOINT
9+
VITE_AUTH_OAUTH_PROVIDER_URL=ENV_UNITY_UI_AUTH_OAUTH_PROVIDER_URL
10+
VITE_AUTH_APP_ADMIN_GROUP_NAME=ENV_UNITY_UI_AUTH_APP_ADMIN_GROUP_NAME
11+
VITE_AUTH_APP_APP_VIEWER_GROUP_NAME=ENV_UNITY_UI_AUTH_APP_APP_VIEWER_GROUP_NAME
12+
13+
# ADS
14+
VITE_ADS_URL=ENV_UNITY_UI_ADS_URL
15+
16+
# SPS
17+
VITE_SPS_WPST_ENDPOINT=ENV_UNITY_UI_SPS_WPST_ENDPOINT

.env/.env.development

Lines changed: 0 additions & 16 deletions
This file was deleted.

.env/.env.integration

Lines changed: 0 additions & 16 deletions
This file was deleted.

.env/.env.production

Whitespace-only changes.

.env/.env.sips-test

Lines changed: 0 additions & 16 deletions
This file was deleted.

.env/.env.test

Lines changed: 0 additions & 16 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ yarn-debug.log*
1010
yarn-error.log*
1111
pnpm-debug.log*
1212
lerna-debug.log*
13+
.env
14+
!.env/.env
1315

1416
node_modules
1517
dist

Dockerfile

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
FROM node:lts-hydrogen as builder
66

7-
# Add git
87
RUN apt-get update
98

109
# Create app directory
@@ -17,23 +16,25 @@ COPY . .
1716
RUN npm clean-install
1817

1918
# Build distribution
20-
RUN npm run build-integration
21-
19+
RUN npm run build
2220

2321
############################################################
2422
############################################################
2523
# Build Image Stage
2624

2725
FROM ubuntu:18.04
28-
LABEL version="0.0.1"
26+
LABEL version="0.1.0"
27+
28+
ENV UNITY_WWW_ROOT=/var/www/unity-ui
29+
ENV ENTRYPOINT_FOLDER=/entrypoint.d
2930

3031
RUN apt-get update \
3132
&& apt-get install -y apache2 \
3233
&& rm -rf /var/lib/apt/lists/* \
3334
&& apt-get clean
3435

3536
# Create app directory and copy distribution code from builder stage
36-
WORKDIR /var/www/unity-ui
37+
WORKDIR ${UNITY_WWW_ROOT}
3738
COPY --from=builder /usr/src/app/dist ./
3839

3940
# Configure apache2
@@ -42,7 +43,14 @@ RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
4243
RUN a2dissite 000-default.conf
4344
RUN a2ensite unity-ui.conf
4445

46+
# Copy and set up files needed for container startup
47+
COPY ./entrypoint.d/* ${ENTRYPOINT_FOLDER}/
48+
RUN chmod 777 -R ${ENTRYPOINT_FOLDER}/* && chmod +x -R ${ENTRYPOINT_FOLDER}/*
49+
4550
EXPOSE 80
4651

47-
# Default command to run container
48-
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND", "-k", "start"]
52+
# Default process to run on container startup
53+
ENTRYPOINT ["/entrypoint.d/docker-entrypoint.sh"]
54+
55+
# Default options to pass to apache when starting container in docker-entrypoint.sh
56+
CMD ["-k", "start"]

Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export IMAGE_NAME = unity-ui
2+
export CONTAINER_NAME = unity-ui
3+
export RUN_OPTIONS =
4+
5+
build-no-cache: RUN_OPTIONS = "--no-cache"
6+
build-no-cache: build
7+
8+
build:
9+
docker buildx build --progress=plain $(RUN_OPTIONS) -t ${IMAGE_NAME} -f Dockerfile .
10+
11+
destroy-container:
12+
docker container rm ${CONTAINER_NAME}
13+
14+
destroy-image:
15+
docker image rm ${IMAGE_NAME}
16+
17+
kill:
18+
docker kill ${CONTAINER_NAME}
19+
20+
run:
21+
docker run --env-file=./.env/.env.docker.local -t -i --rm -p 8080:80 --name ${CONTAINER_NAME} ${IMAGE_NAME}
22+
23+
start:
24+
docker start ${CONTAINER_NAME}
25+
26+
stop:
27+
docker stop ${CONTAINER_NAME}
28+
29+
# ----------------------------------------------------------------------------
30+
# Self-Documented Makefile
31+
# ref: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
32+
# ----------------------------------------------------------------------------
33+
help: ## (DEFAULT) This help information
34+
@echo ====================================================================
35+
@grep -E '^## .*$$' \
36+
$(MAKEFILE_LIST) \
37+
| awk 'BEGIN { FS="## " }; {printf "\033[33m%-24s\033[0m \n", $$2}'
38+
@echo
39+
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' \
40+
$(MAKEFILE_LIST) \
41+
| awk 'BEGIN { FS=":.*?## " }; {printf "\033[36m%-24s\033[0m %s\n", $$1, $$2}' \
42+
# | sort
43+
.PHONY: help
44+
.DEFAULT_GOAL := help

entrypoint.d/docker-entrypoint.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
#set -x; : "$0" "$@" # Use for debugging
3+
4+
# Inject Environment Variables for React App
5+
source ${ENTRYPOINT_FOLDER}/env.sh
6+
7+
rm -f /var/run/apache2/apache2.pid
8+
9+
# Run the main container process (from the Dockerfile CMD)
10+
exec /usr/sbin/apachectl -D FOREGROUND "$@"

entrypoint.d/env.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
for i in $(env | grep ENV_UNITY_UI_)
3+
do
4+
key=$(echo $i | cut -d '=' -f 1)
5+
value=$(echo $i | cut -d '=' -f 2-)
6+
echo $key=$value
7+
# sed All files
8+
# find $UNITY_WWW_ROOT -type f -exec sed -i "s|${key}|${value}|g" '{}' +
9+
10+
# sed .js only
11+
find $UNITY_WWW_ROOT -type f \( -name '*.js' \) -exec sed -i "s|${key}|${value}|g" '{}' +
12+
done

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"name": "unity-ui",
33
"private": true,
4-
"version": "0.4.0",
4+
"version": "0.5.0",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build-integration": "tsc && vite build --mode integration --base=/dashboard/",
9-
"build-test": "tsc && vite build --mode test --base=/dashboard/",
10-
"build-sips-test": "tsc && vite build --mode sips-test --base=/unity-sips-test/dashboard/",
11-
"build-prod": "tsc && vite build --mode production",
8+
"build": "tsc && vite build",
129
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1310
"preview": "vite preview"
1411
},

src/Config.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ const Config = {
2424

2525
}
2626

27+
if( import.meta.env.DEV ) {
28+
// Output Configuration on every call to help with debugging only in DEV mode
29+
console.log(Config)
30+
}
31+
2732
export default Config;

0 commit comments

Comments
 (0)