From 1bfd078938823f924d7289653ce75a210de46cde Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Tue, 12 Nov 2019 22:50:32 +0100 Subject: [PATCH] feat(nginx4spa): add spa nginx image (#33) --- .gitlab-ci.yml | 2 +- README.md | 2 +- nginx4spa/.gitlab-ci.yml | 40 +++++++++++++++++++++++++++++ {spa-nginx => nginx4spa}/Dockerfile | 3 ++- nginx4spa/README.md | 3 +++ nginx4spa/entrypoint.sh | 5 ++++ nginx4spa/envsub.sh | 25 ++++++++++++++++++ {spa-nginx => nginx4spa}/nginx.conf | 8 +++--- nginx4spa/test/foo/bar/bar.js | 1 + nginx4spa/test/index.html | 1 + spa-nginx/.gitlab-ci.yml | 18 ------------- spa-nginx/entrypoint.sh | 25 ------------------ 12 files changed, 83 insertions(+), 50 deletions(-) create mode 100644 nginx4spa/.gitlab-ci.yml rename {spa-nginx => nginx4spa}/Dockerfile (67%) create mode 100644 nginx4spa/README.md create mode 100755 nginx4spa/entrypoint.sh create mode 100755 nginx4spa/envsub.sh rename {spa-nginx => nginx4spa}/nginx.conf (100%) create mode 100644 nginx4spa/test/foo/bar/bar.js create mode 100644 nginx4spa/test/index.html delete mode 100644 spa-nginx/.gitlab-ci.yml delete mode 100644 spa-nginx/entrypoint.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 849c341cb..0819f94b1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ include: - /helm/.gitlab-ci.yml - /k8s-ns-killer/.gitlab-ci.yml - /kubectl/.gitlab-ci.yml - - /spa-nginx/.gitlab-ci.yml + - /nginx4spa/.gitlab-ci.yml # diff --git a/README.md b/README.md index 1aa15448a..ba7330e7f 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $ docker pull docker.pkg.github.com/socialgouv/docker/: | **helm** | `docker pull registry.gitlab.factory.social.gouv.fr/socialgouv/docker/helm:0.9.1` | [![README](https://img.shields.io/badge/README--green.svg)](./helm/README.md) | | **k8s-ns-killer** | `docker pull registry.gitlab.factory.social.gouv.fr/socialgouv/docker/k8s-ns-killer:0.9.1` | [![README](https://img.shields.io/badge/README--green.svg)](./k8s-ns-killer/README.md) | | **kubectl** | `docker pull registry.gitlab.factory.social.gouv.fr/socialgouv/docker/kubectl:0.9.1` | [![README](https://img.shields.io/badge/README--green.svg)](./kubectl/README.md) | -| **spa-nginx** | `docker pull registry.gitlab.factory.social.gouv.fr/socialgouv/docker/spa-nginx:0.9.1` | [![README](https://img.shields.io/badge/README--green.svg)](./spa-nginx/README.md) | +| **nginx4spa** | `docker pull registry.gitlab.factory.social.gouv.fr/socialgouv/docker/nginx4spa:0.9.1` | [![README](https://img.shields.io/badge/README--green.svg)](./nginx4spa/README.md) |

diff --git a/nginx4spa/.gitlab-ci.yml b/nginx4spa/.gitlab-ci.yml new file mode 100644 index 000000000..776aaa3ce --- /dev/null +++ b/nginx4spa/.gitlab-ci.yml @@ -0,0 +1,40 @@ +Build socialgouv/docker/nginx4spa: + stage: "Build" + extends: .base_register_to_gitlab_stage + variables: + CONTEXT: nginx4spa + IMAGE_NAME: ${CI_REGISTRY_IMAGE}/nginx4spa + +Test socialgouv/docker/nginx4spa: + stage: "Test" + image: "${CI_REGISTRY_IMAGE}/nginx4spa:${CI_COMMIT_SHA}" + cache: + key: ${CI_JOB_NAME}-${CI_COMMIT_REF_SLUG} + paths: + - ${CI_PROJECT_DIR}/var/cache/apk + variables: + NGINX_PORT: 8888 + VERSION: x.y.z + WWW_DIRECTORY: ${CI_PROJECT_DIR}/nginx4spa/test + script: + - sed -i -e "s|/usr/share/nginx/html|${WWW_DIRECTORY}|g" /etc/nginx/nginx.conf + - sed -i -e "s|80|${NGINX_PORT}|g" /etc/nginx/nginx.conf + - /entrypoint.sh & + - nginx -s stop || pkill nginx || true + # + - nginx + # + - apk --update add curl + - curl -s localhost:${NGINX_PORT} + - curl -s localhost:${NGINX_PORT} | + grep "nginx4spa/test/index.html with VERSION=x.y.z" + - curl -s localhost:${NGINX_PORT}/foo/bar/bar.js + - curl -s localhost:${NGINX_PORT}/foo/bar/bar.js | + grep "// nginx4spa/test/foo/bar/bar.js with VERSION=x.y.z" + # + - kill $(ps aux | grep '[n]ginx' | awk '{print $1}') + +Publish socialgouv/docker/nginx4spa to Github Registry: + extends: .base_publish_to_github_stage + variables: + IMAGE_NAME: socialgouv/docker/nginx4spa diff --git a/spa-nginx/Dockerfile b/nginx4spa/Dockerfile similarity index 67% rename from spa-nginx/Dockerfile rename to nginx4spa/Dockerfile index 15165e504..c08f5d3c3 100644 --- a/spa-nginx/Dockerfile +++ b/nginx4spa/Dockerfile @@ -1,6 +1,7 @@ -FROM nginx:1.17 +FROM nginx:1.17-alpine COPY ./nginx.conf /etc/nginx/nginx.conf COPY ./entrypoint.sh /entrypoint.sh +COPY ./envsub.sh /envsub.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/nginx4spa/README.md b/nginx4spa/README.md new file mode 100644 index 000000000..30dfb9263 --- /dev/null +++ b/nginx4spa/README.md @@ -0,0 +1,3 @@ +# KubeCtrl + +> KubeCtrl image diff --git a/nginx4spa/entrypoint.sh b/nginx4spa/entrypoint.sh new file mode 100755 index 000000000..63e212250 --- /dev/null +++ b/nginx4spa/entrypoint.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env sh + +source /envsub.sh + +exec nginx -g 'daemon off;' diff --git a/nginx4spa/envsub.sh b/nginx4spa/envsub.sh new file mode 100755 index 000000000..589e4af7f --- /dev/null +++ b/nginx4spa/envsub.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env sh + +################################################################################ +# Replace environment values in files # +################################################################################ + +# +# As defualt, in every files in the "/usr/share/nginx/html" directory, this +# script replace `%%KEY%%` by `VALUE` where `export KEY=VALUE` in the global env var. +# + +WWW_DIRECTORY=${WWW_DIRECTORY:="/usr/share/nginx/html"} +DELIMITER=${DELIMITER:="%%"} + +# Save env variable in file +printenv > /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" {} \; +done < /tmp/env-vars + +rm /tmp/env-vars diff --git a/spa-nginx/nginx.conf b/nginx4spa/nginx.conf similarity index 100% rename from spa-nginx/nginx.conf rename to nginx4spa/nginx.conf index 30654926f..80d1e7155 100644 --- a/spa-nginx/nginx.conf +++ b/nginx4spa/nginx.conf @@ -9,13 +9,13 @@ events { http { - include /etc/nginx/mime.types; - default_type application/octet-stream; - sendfile on; + absolute_redirect off; access_log /var/log/nginx/access.log; + default_type application/octet-stream; error_log /var/log/nginx/error.log; + include /etc/nginx/mime.types; keepalive_timeout 3000; - absolute_redirect off; + sendfile on; server { listen 80; diff --git a/nginx4spa/test/foo/bar/bar.js b/nginx4spa/test/foo/bar/bar.js new file mode 100644 index 000000000..46e8e5053 --- /dev/null +++ b/nginx4spa/test/foo/bar/bar.js @@ -0,0 +1 @@ +// nginx4spa/test/foo/bar/bar.js with VERSION=%%VERSION%% diff --git a/nginx4spa/test/index.html b/nginx4spa/test/index.html new file mode 100644 index 000000000..babd58cf3 --- /dev/null +++ b/nginx4spa/test/index.html @@ -0,0 +1 @@ +nginx4spa/test/index.html with VERSION=%%VERSION%% diff --git a/spa-nginx/.gitlab-ci.yml b/spa-nginx/.gitlab-ci.yml deleted file mode 100644 index 4a4bafc25..000000000 --- a/spa-nginx/.gitlab-ci.yml +++ /dev/null @@ -1,18 +0,0 @@ -Build socialgouv/docker/spa-nginx: - stage: "Build" - extends: .base_register_to_gitlab_stage - variables: - CONTEXT: spa-nginx - IMAGE_NAME: ${CI_REGISTRY_IMAGE}/spa-nginx - -Test socialgouv/docker/spa-nginx: - stage: "Test" - image: "${CI_REGISTRY_IMAGE}/spa-nginx:${CI_COMMIT_SHA}" - environment: feature-dev - script: - - echo "todo..." - -Publish socialgouv/docker/spa-nginx to Github Registry: - extends: .base_publish_to_github_stage - variables: - IMAGE_NAME: socialgouv/docker/spa-nginx diff --git a/spa-nginx/entrypoint.sh b/spa-nginx/entrypoint.sh deleted file mode 100644 index fb37a7e2f..000000000 --- a/spa-nginx/entrypoint.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env sh - -ROOT_DIRECTORY="/usr/share/nginx/html" - -############################################################## -# Step 1 : replace default environment values in .html files # -############################################################## - -# Save env variable in file -printenv >> env-vars - -while IFS='=' read -r KEY VALUE -do - # replace default environment variables value - find $ROOT_DIRECTORY -type f -name \*.js -exec \ - sed -i -e "s|%%$KEY%%|$VALUE|g" {} + -done