Skip to content

Instantly share code, notes, and snippets.

@kitingChris
Created March 2, 2022 19:39
Show Gist options
  • Save kitingChris/8aa257380092f1c80cc1620c34362b6b to your computer and use it in GitHub Desktop.
Save kitingChris/8aa257380092f1c80cc1620c34362b6b to your computer and use it in GitHub Desktop.
docker-entrypoint.sh for ReactApps with pm2 and environment variable substitution on startup
#!/usr/bin/env sh
env | grep "REACT_APP_" | while IFS= read -r line; do
var=${line%%=*}
val=$(printf '%s\n' "${line#*=}" | sed -e 's/[\/&]/\\&/g')
find . -type f -regex '.*\.\(html\|js\|js\.map\|css\|css\.map\|json\|svg\)' \
-exec sed -i "s/{{$var}}/$val/g" {} +
done
command -v pm2 > /dev/null && status="$?" || status="$?"
if [ "${status}" = 127 ]; then
yarn global add pm2
fi
PM2_OPTIONS="serve . --no-daemon --spa"
if [ -n "${REACT_APP_BASICAUTH_USERNAME}" ]; then
PM2_OPTIONS="${PM2_OPTIONS} --basic-auth-username ${REACT_APP_BASICAUTH_USERNAME}"
fi
if [ -n "${REACT_APP_BASICAUTH_PASSWORD}" ]; then
PM2_OPTIONS="${PM2_OPTIONS} --basic-auth-password ${REACT_APP_BASICAUTH_PASSWORD}"
fi
# shellcheck disable=SC2086
pm2 ${PM2_OPTIONS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment