The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
chore: inject enviroment at the ./front deployment phase (#2174) (#2179)
Ruslana5fe256d7e0f
diff --git a/front/package.json b/front/package.jsonindex 21cd338be82..8bd4ec95999 100644--- a/front/package.json+++ b/front/package.json@@ -66,7 +66,8 @@},"scripts": {"start": "PORT=3001 craco start --max-warnings=0",- "build": "craco build",+ "build:inject-runtime-env": "./scripts/inject-runtime-env.sh",+ "build": "craco build && yarn build:inject-runtime-env","test": "craco test","coverage": "craco test --coverage .","lint": "eslint src --max-warnings=0",diff --git a/front/public/env-config.js b/front/public/env-config.jsnew file mode 100644index 00000000000..20254cd2541--- /dev/null+++ b/front/public/env-config.js@@ -0,0 +1,3 @@+window._env_ = {+ // This file should stay empty. It will be overwritten by the build process.+}diff --git a/front/public/index.html b/front/public/index.htmlindex 9578c1ecae4..f2d2be21bab 100644--- a/front/public/index.html+++ b/front/public/index.html@@ -36,6 +36,7 @@/><title>Twenty</title>+ <script src="%PUBLIC_URL%/env-config.js"></script></head><body><noscript>You need to enable JavaScript to run this app.</noscript>diff --git a/front/scripts/inject-runtime-env.sh b/front/scripts/inject-runtime-env.shnew file mode 100755index 00000000000..d4281004b71--- /dev/null+++ b/front/scripts/inject-runtime-env.sh@@ -0,0 +1,11 @@+echo "Generating env-config.js file from runtime environment variables..."++BASE_FILENAME="build/env-config.js"+mkdir -p build+rm -rf "./$BASE_FILENAME"++{+ echo "window._env_ = {"+ echo " REACT_APP_SERVER_BASE_URL: \"$REACT_APP_SERVER_BASE_URL\","+ echo "}"+} > "./$BASE_FILENAME"\ No newline at end of filediff --git a/front/src/config/index.ts b/front/src/config/index.tsnew file mode 100644index 00000000000..48d0908f2d7--- /dev/null+++ b/front/src/config/index.ts@@ -0,0 +1,10 @@+declare global {+ interface Window {+ _env_?: Record<string, string>;+ }+}++export const REACT_APP_SERVER_BASE_URL =+ window._env_?.REACT_APP_SERVER_BASE_URL ||+ process.env.REACT_APP_SERVER_BASE_URL ||+ 'http://localhost:3000';diff --git a/front/src/modules/apollo/hooks/useApolloFactory.ts b/front/src/modules/apollo/hooks/useApolloFactory.tsindex 9f996cf60a9..e2d66eb0e7d 100644--- a/front/src/modules/apollo/hooks/useApolloFactory.ts+++ b/front/src/modules/apollo/hooks/useApolloFactory.ts@@ -6,6 +6,7 @@ import { useRecoilState } from 'recoil';import { tokenPairState } from '@/auth/states/tokenPairState';import { isDebugModeState } from '@/client-config/states/isDebugModeState';import { AppPath } from '@/types/AppPath';+import { REACT_APP_SERVER_BASE_URL } from '~/config';import { ActivityTarget } from '~/generated/graphql';import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';import { useUpdateEffect } from '~/hooks/useUpdateEffect';@@ -23,7 +24,7 @@ export const useApolloFactory = () => {const apolloClient = useMemo(() => {apolloRef.current = new ApolloFactory({- uri: `${process.env.REACT_APP_SERVER_BASE_URL}/graphql`,+ uri: `${REACT_APP_SERVER_BASE_URL}/graphql`,cache: new InMemoryCache({typePolicies: {Activity: {diff --git a/front/src/modules/auth/hooks/useAuth.ts b/front/src/modules/auth/hooks/useAuth.tsindex c41282fe755..071bdd0c29e 100644--- a/front/src/modules/auth/hooks/useAuth.ts+++ b/front/src/modules/auth/hooks/useAuth.ts@@ -6,6 +6,7 @@ import {useRecoilState,} from 'recoil';+import { REACT_APP_SERVER_BASE_URL } from '~/config';import {useChallengeMutation,useCheckUserExistsLazyQuery,@@ -136,8 +137,7 @@ export const useAuth = () => {const handleGoogleLogin = useCallback((workspaceInviteHash?: string) => {const authServerUrl =- process.env.REACT_APP_SERVER_AUTH_URL ??- process.env.REACT_APP_SERVER_BASE_URL + '/auth';+ REACT_APP_SERVER_BASE_URL ?? REACT_APP_SERVER_BASE_URL + '/auth';window.location.href =`${authServerUrl}/google/${workspaceInviteHash ? '?inviteHash=' + workspaceInviteHash : ''diff --git a/front/src/modules/metadata/components/ApolloMetadataClientProvider.tsx b/front/src/modules/metadata/components/ApolloMetadataClientProvider.tsxindex 67371da9962..b3eb9db9be6 100644--- a/front/src/modules/metadata/components/ApolloMetadataClientProvider.tsx+++ b/front/src/modules/metadata/components/ApolloMetadataClientProvider.tsx@@ -4,6 +4,7 @@ import { ApolloClient, InMemoryCache } from '@apollo/client';import { useRecoilState } from 'recoil';import { tokenPairState } from '@/auth/states/tokenPairState';+import { REACT_APP_SERVER_BASE_URL } from '~/config';import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext';@@ -17,7 +18,7 @@ export const ApolloMetadataClientProvider = ({const apolloMetadataClient = useMemo(() => {if (tokenPair?.accessToken.token) {return new ApolloClient({- uri: `${process.env.REACT_APP_SERVER_BASE_URL}/metadata`,+ uri: `${REACT_APP_SERVER_BASE_URL}/metadata`,cache: new InMemoryCache(),headers: {Authorization: `Bearer ${tokenPair.accessToken.token}`,diff --git a/front/src/modules/users/utils/getProfilePictureAbsoluteURI.ts b/front/src/modules/users/utils/getProfilePictureAbsoluteURI.tsindex 77350d114be..ccd502d187f 100644--- a/front/src/modules/users/utils/getProfilePictureAbsoluteURI.ts+++ b/front/src/modules/users/utils/getProfilePictureAbsoluteURI.ts@@ -1,3 +1,5 @@+import { REACT_APP_SERVER_BASE_URL } from '~/config';+export const getImageAbsoluteURIOrBase64 = (imageUrl?: string | null) => {if (!imageUrl) {return null;@@ -12,8 +14,7 @@ export const getImageAbsoluteURIOrBase64 = (imageUrl?: string | null) => {}const serverFilesUrl =- process.env.REACT_APP_SERVER_FILES_URL ??- process.env.REACT_APP_SERVER_BASE_URL + '/files';+ REACT_APP_SERVER_BASE_URL ?? REACT_APP_SERVER_BASE_URL + '/files';return `${serverFilesUrl}/${imageUrl}`;};diff --git a/infra/prod/front/Dockerfile b/infra/prod/front/Dockerfileindex 88c156bc647..2b6c3e79880 100644--- a/infra/prod/front/Dockerfile+++ b/infra/prod/front/Dockerfile@@ -1,6 +1,5 @@FROM node:18.16.0-alpine as build-ARG REACT_APP_SERVER_BASE_URLARG REACT_APP_SERVER_AUTH_URLARG REACT_APP_SERVER_FILES_URL@@ -18,10 +17,13 @@ COPY ./infra/prod/front/serve.json ./buildFROM node:18.16.0-alpine as frontWORKDIR /app/front+COPY --from=build /app/front/build ./build+COPY ./front/scripts/inject-runtime-env.sh /app/front/scripts/inject-runtime-env.shRUN yarn global add serve+LABEL org.opencontainers.image.source=https://github.com/twentyhq/twentyLABEL org.opencontainers.image.description="This image provides a consistent and reproducible environment for the frontend."-CMD ["serve", "build"]+CMD ["/bin/sh", "-c", "/app/front/scripts/inject-runtime-env.sh && serve build"]diff --git a/infra/prod/server/Dockerfile b/infra/prod/server/Dockerfileindex 2e795682a72..f7fba86aa67 100644--- a/infra/prod/server/Dockerfile+++ b/infra/prod/server/Dockerfile@@ -1,4 +1,4 @@-FROM node:18.16.0-alpine as server+FROM node:18.16.0-alpine as buildWORKDIR /app/serverCOPY ./server/package.json ./@@ -11,6 +11,12 @@ RUN npx prisma generateRUN yarn build+FROM node:18.16.0-alpine as server++COPY --from=build /app/server/dist ./dist++WORKDIR /app/server+LABEL org.opencontainers.image.source=https://github.com/twentyhq/twentyLABEL org.opencontainers.image.description="This image provides a consistent and reproducible environment for the backend, ensuring it deploys faster and runs the same way regardless of the deployment environment."diff --git a/render.yaml b/render.yamlindex 4411c52df2d..e028d983b0c 100644--- a/render.yaml+++ b/render.yaml@@ -14,7 +14,7 @@ services:name: serverenv: dockerdockerfilePath: ./infra/prod/server/Dockerfile- dockerCommand: "sh -c yarn prisma:migrate && yarn database:setup && node dist/src/main"+ dockerCommand: "sh -c ./scripts/compute-database-url.sh && yarn prisma:migrate && yarn database:setup && node dist/src/main"autoDeploy: falseenvVars:- key: FRONT_BASE_URL@@ -38,8 +38,6 @@ services:name: twenty_postgrestype: pservproperty: port- - key: PG_DATABASE_URL- value: postgres://twenty:twenty@twenty-postgres:5432/default?connection_limit=1disk:name: twenty-diskmountPath: /.local-storagediff --git a/server/scripts/compute-database-url.sh b/server/scripts/compute-database-url.shnew file mode 100755index 00000000000..dad7d852156--- /dev/null+++ b/server/scripts/compute-database-url.sh@@ -0,0 +1,2 @@+#!/bin/sh+export PG_DATABASE_URL=postgres://twenty:twenty@$PG_DATABASE_HOST:5432/default\ No newline at end of filediff --git a/server/scripts/run-integration.sh b/server/scripts/run-integration.shindex fc30b598bfc..3673ab813f2 100755--- a/server/scripts/run-integration.sh+++ b/server/scripts/run-integration.sh@@ -1,5 +1,5 @@#!/usr/bin/env bash-# src/run-integration.sh+# scripts/run-integration.shDIR="$(cd "$(dirname "$0")" && pwd)"source $DIR/set-env-test.shdiff --git a/server/scripts/set-env-test.sh b/server/scripts/set-env-test.shindex 8eb4eee9d23..98e7ac7c9cb 100755--- a/server/scripts/set-env-test.sh+++ b/server/scripts/set-env-test.sh@@ -1,5 +1,5 @@#!/usr/bin/env bash-# scripts/setenv.sh+# scripts/set-env-test.sh# Get script's directorySCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"