Skip to content

Commit 0ba5c04

Browse files
authored
Merge pull request #2 from zmeyc/fix-escaping
Simplify script and fix escaping
2 parents 3aa8d3d + 72eb0f3 commit 0ba5c04

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

docker-entrypoint.sh

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
#!/usr/bin/env sh
2-
set -eu
1+
#!/bin/sh
2+
set -euo pipefail
33

4-
ENV_JSON=$(env | grep '^REACT_APP_*' | jq -c '. | split("\n") | map(select(. != "")) | map(split("=") | { (.[0]) : .[1] }) | reduce .[] as $item ({}; . * $item)' -R -s)
5-
ESCAPED_ENV_JSON=$(echo $ENV_JSON | sed 's/\"/\\\"/g' | sed 's/\//\\\//g' | tr -d '\n' | tr -d '[[:blank:]]')
6-
sed -i 's/%REACT_APP_ENV%/'"$ESCAPED_ENV_JSON"'/g' /var/www/index.html
4+
# Capture all environment variables starting with REACT_APP_ and make JSON string from them
5+
ENV_JSON="$(jq --compact-output --null-input 'env | with_entries(select(.key | startswith("REACT_APP_")))')"
6+
7+
# Escape sed replacement's special characters: \, &, /.
8+
# No need to escape newlines, because --compact-output already removed them.
9+
# Inside of JSON strings newlines are already escaped.
10+
ENV_JSON_ESCAPED="$(printf "%s" "${ENV_JSON}" | sed -e 's/[\&/]/\\&/g')"
11+
12+
sed -i "s/%REACT_APP_ENV%/${ENV_JSON_ESCAPED}/g" /var/www/index.html
713

814
exec "$@"

0 commit comments

Comments
 (0)