Skip to content

Commit

Permalink
feat(nginx4spa): configurable PORT (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bouquillon authored and douglasduteil committed Dec 9, 2019
1 parent 9cc9aad commit 476fe97
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion nginx4spa/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Test nginx4spa:
- docker run
--detach
--env VERSION=x.y.z
--env PORT=90
--name nginx4spa_test
--publish 8888:80
--publish 8888:90
--rm
--volume ${CI_PROJECT_DIR}/nginx4spa/test:/usr/share/nginx/html
${CI_REGISTRY_IMAGE}/nginx4spa:${CI_COMMIT_SHA}
Expand Down
6 changes: 4 additions & 2 deletions nginx4spa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
> Nginx image for single page application
As default, in every files in the "/usr/share/nginx/html" directory, the [`envsub.sh`](./envsub.sh) script replaces `%%KEY%%` by `VALUE` where `export KEY=VALUE` in the global env var.

So :

```sh
$ echo "VERSION=%%VERSION%%" > /www/version.txt
$ docker run \
--env VERSION=x.y.z \
--env PORT=4444 \
--name nginx4spa_test \
--publish 8888:80 \
--publish 8888:4444 \
--rm \
--volume /www:/usr/share/nginx/html \
registry.gitlab.factory.social.gouv.fr/socialgouv/docker/nginx4spa:<version>
$ curl localhost:8080/version.txt
$ curl localhost:8888/version.txt
VERSION=x.y.z
```
6 changes: 6 additions & 0 deletions nginx4spa/envsub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ DELIMITER=${DELIMITER:="%%"}
# Save env variable in file
printenv > /tmp/env-vars

# Add default nginx port
echo "PORT=${PORT:="80"}" >> /tmp/env-vars

while IFS='=' read -r KEY VALUE
do
# In every files in the dir, replace the environment variables value
find ${WWW_DIRECTORY} -type f -exec \
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" {} \;
# replace in nginx.conf too
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" /etc/nginx/nginx.conf

done < /tmp/env-vars

rm /tmp/env-vars
2 changes: 1 addition & 1 deletion nginx4spa/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ http {
sendfile on;

server {
listen 80;
listen %%PORT%%;
root /usr/share/nginx/html;
index index.html;
server_name_in_redirect on;
Expand Down

0 comments on commit 476fe97

Please sign in to comment.