From 1a254b0bece5ecd6c1607d882503186d85e102e4 Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Wed, 16 Aug 2023 19:07:16 +0300 Subject: [PATCH 1/2] Fix ENV variables are not being set inside docker closes https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/414 --- .env | 2 +- Dockerfile | 27 +++++++++++++++++++------- env.sh | 7 +++++++ generate-env-browser.js | 43 ----------------------------------------- package.json | 2 +- 5 files changed, 29 insertions(+), 52 deletions(-) create mode 100644 env.sh delete mode 100755 generate-env-browser.js diff --git a/.env b/.env index 999fd609..7e5d14ab 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ REACT_APP_API_URL=http://localhost:4200 PORT=8080 -VRT_VERSION=4.20.0 \ No newline at end of file +VRT_VERSION=5.0.0 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3c212cde..150b53fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,13 @@ FROM node:18-alpine3.18 AS builder WORKDIR /app # Copy all files from the repo to /app -COPY . . +# COPY . . +COPY /public ./public +COPY /src ./src +COPY index.html . +COPY package*.json . +COPY tsconfig.json . +COPY vite.config.ts . # Install app dependencies RUN npm ci --verbose @@ -14,9 +20,6 @@ RUN npm ci --verbose # Build the ui RUN npm run build -# Create environment variable js file -RUN node generate-env-browser.js - ### STAGE 2: Run ### # This image is around 5 megabytes FROM nginx:1.25-alpine3.17-slim @@ -26,7 +29,17 @@ COPY /nginx /etc/nginx/conf.d EXPOSE 8080 EXPOSE 443 -COPY --from=builder /app/build /usr/share/nginx/html -COPY --from=builder /app/env-config.js /usr/share/nginx/html/ +WORKDIR /usr/share/nginx/html + +# Copy built source +COPY --from=builder /app/build . + +# Copy override ENV script with default values +COPY ./env.sh . +COPY .env . + +# Make our shell script executable +RUN chmod +x env.sh -# Nginx server will now start automatically. +# Start Nginx server with override ENV variables script +CMD ["/bin/sh", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""] \ No newline at end of file diff --git a/env.sh b/env.sh new file mode 100644 index 00000000..f74c76de --- /dev/null +++ b/env.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# (c) https://github.com/kunokdev/cra-runtime-environment-variables/blob/master/env.sh +# line endings must be \n, not \r\n ! + +echo "window._env_ = {" > ./env-config.js +awk -F '=' '{ print $1 ": \"" (ENVIRON[$1] ? ENVIRON[$1] : $2) "\"," }' ./.env >> ./env-config.js +echo "}" >> ./env-config.js diff --git a/generate-env-browser.js b/generate-env-browser.js deleted file mode 100755 index 663dfc00..00000000 --- a/generate-env-browser.js +++ /dev/null @@ -1,43 +0,0 @@ -import fs from "node:fs"; -import process from "node:process"; - -const JS_FILE = "env-config.js"; -const ENV_FILE = ".env"; - -// Remove existing config file -if (fs.existsSync(JS_FILE)) { - fs.unlinkSync(JS_FILE); -} -// Create new file -fs.writeFileSync(JS_FILE, "", "utf-8"); - -// Add assignment -fs.appendFileSync(JS_FILE, "window._env_ = {\n"); - -// Read each line in .env file -// Each line represents key=value pairs -const envFile = fs.readFileSync(ENV_FILE, "utf-8"); -const lines = envFile.split("\n"); - -lines.forEach((line) => { - // Ignore comments, lines starting with hash - if (line.startsWith("#")) { - return; - } - // Split env variables by character `=` - if (line.includes("=")) { - const [varname, varvalue] = line.split("="); - - // Read value of current variable if exists as Environment variable - let value = process.env[varname] || varvalue; - // Otherwise use value from .env file - if (typeof value === "undefined") { - value = varvalue; - } - - // Append configuration property to JS file - fs.appendFileSync(JS_FILE, ` ${varname}: "${value}",\n`); - } -}); - -fs.appendFileSync(JS_FILE, "};"); diff --git a/package.json b/package.json index 74ccac54..cf5b829b 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "uuid": "^9.0.0" }, "scripts": { - "start": "node generate-env-browser.js && cp env-config.js ./public/ && vite", + "start": "chmod +x ./env.sh && sh ./env.sh && cp env-config.js ./public/ && vite", "test": "jest", "format": "prettier --write integration_tests src *.js *.ts *.md", "build": "vite build", From 3bafd4fb32a77bde2cae1d23a1d94c6c4b944cba Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Wed, 16 Aug 2023 19:11:53 +0300 Subject: [PATCH 2/2] Update Dockerfile --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 150b53fc..d995c5d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,6 @@ FROM node:18-alpine3.18 AS builder WORKDIR /app # Copy all files from the repo to /app -# COPY . . COPY /public ./public COPY /src ./src COPY index.html .