Skip to content

Commit

Permalink
refactor: Add granularity to NODE_ENV and REACT_APP_ENV (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Feb 1, 2023
1 parent d3287f2 commit a88cb71
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ jobs:
REACT_APP_SHAREDB_URL: wss://sharedb.${{ env.FULL_DOMAIN }}
# needed because there's no API to change google's allowed OAuth URLs
REACT_APP_GOOGLE_OAUTH_OVERRIDE: https://api.editor.planx.dev
REACT_APP_ENV: pizza

build_storybook:
name: Build Storybook
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/push-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
REACT_APP_SHAREDB_URL: wss://sharedb.editor.planx.dev
REACT_APP_AIRBRAKE_PROJECT_ID: ${{ secrets.AIRBRAKE_PROJECT_ID }}
REACT_APP_AIRBRAKE_PROJECT_KEY: ${{ secrets.AIRBRAKE_PROJECT_KEY }}
REACT_APP_ENV: staging
- name: Upload Build Artifact
uses: actions/upload-artifact@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/push-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
REACT_APP_SHAREDB_URL: wss://sharedb.editor.planx.uk
REACT_APP_AIRBRAKE_PROJECT_ID: ${{ secrets.AIRBRAKE_PROJECT_ID }}
REACT_APP_AIRBRAKE_PROJECT_KEY: ${{ secrets.AIRBRAKE_PROJECT_KEY }}
REACT_APP_ENV: production
- name: Upload Build Artifact
uses: actions/upload-artifact@v2
with:
Expand Down
7 changes: 3 additions & 4 deletions api.planx.uk/airbrake.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Notifier } from "@airbrake/node";
import { isLiveEnv } from "./helpers";

const airbrake =
process.env.NODE_ENV === "production" &&
isLiveEnv() &&
process.env.AIRBRAKE_PROJECT_ID &&
process.env.AIRBRAKE_PROJECT_KEY
? new Notifier({
projectId: Number(process.env.AIRBRAKE_PROJECT_ID),
projectKey: process.env.AIRBRAKE_PROJECT_KEY,
environment: process.env.API_URL_EXT!.endsWith("planx.uk")
? "production"
: "staging",
environment: process.env.NODE_ENV!
})
: undefined;

Expand Down
3 changes: 3 additions & 0 deletions api.planx.uk/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ const makeUniqueFlow = (flowData: Flow["data"], replaceValue: string): Flow["dat
return flowData;
};

const isLiveEnv = () => (["production", "staging", "pizza"].includes(process.env.NODE_ENV || ""));

export {
getFlowData,
getMostRecentPublishedFlow,
Expand All @@ -197,4 +199,5 @@ export {
getChildren,
makeUniqueFlow,
insertFlow,
isLiveEnv,
};
3 changes: 2 additions & 1 deletion api.planx.uk/s3/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import S3 from "aws-sdk/clients/s3";
import { isLiveEnv } from "../helpers";

export function s3Factory() {
return new S3({
Expand All @@ -11,7 +12,7 @@ export function s3Factory() {
}

function useMinio() {
if (process.env.NODE_ENV === "production") {
if (isLiveEnv()) {
// Points to AWS
return {};
} else {
Expand Down
5 changes: 3 additions & 2 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { useOrdnanceSurveyProxy } from "./proxy/ordnanceSurvey";
import { usePayProxy } from "./proxy/pay";
import { downloadFeedbackCSV } from "./admin/feedback/downloadFeedbackCSV";
import { sanitiseApplicationData } from "./webhooks/sanitiseApplicationData";
import { isLiveEnv } from "./helpers";

const router = express.Router();

Expand Down Expand Up @@ -90,7 +91,7 @@ const handleSuccess = (req: Request, res: Response) => {
const { returnTo = process.env.EDITOR_URL_EXT } = req.session!;

const domain = (() => {
if (process.env.NODE_ENV === "production") {
if (isLiveEnv()) {
if (returnTo?.includes("editor.planx.")) {
// user is logging in to staging from editor.planx.dev
// or production from editor.planx.uk
Expand Down Expand Up @@ -119,7 +120,7 @@ const handleSuccess = (req: Request, res: Response) => {
httpOnly: false,
};

if (process.env.NODE_ENV === "production") {
if (isLiveEnv()) {
cookie.secure = true;
cookie.sameSite = "none";
}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.pizza.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
virtual.host: api.${ROOT_DOMAIN}
virtual.port: ${API_PORT}
virtual.tls: ${TLS_EMAIL}
environment:
NODE_ENV: "pizza"

sharedb:
labels:
Expand Down
4 changes: 3 additions & 1 deletion editor.planx.uk/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ REACT_APP_FEEDBACK_FISH_ID=65f02de00b90d1

REACT_APP_API_URL=http://localhost:7002
REACT_APP_HASURA_URL=http://localhost:7000/v1/graphql
REACT_APP_SHAREDB_URL=ws://localhost:7003
REACT_APP_SHAREDB_URL=ws://localhost:7003

REACT_APP_ENV=development
1 change: 1 addition & 0 deletions editor.planx.uk/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_ENV=test
1 change: 1 addition & 0 deletions editor.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"classnames": "^2.3.1",
"core-js": "^3.24.1",
"date-fns": "^2.29.1",
"dotenv": "^16.0.3",
"fast-xml-parser": "^4.0.11",
"formik": "^2.2.9",
"graphql": "^16.5.0",
Expand Down
12 changes: 10 additions & 2 deletions editor.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion editor.planx.uk/src/airbrake.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Notifier } from "@airbrake/browser";
import { isLiveEnv } from "utils";

export const reportError = getErrorLogger().notify;

Expand All @@ -13,7 +14,7 @@ function log(...args: any[]) {
// forward all JS errors to airbrake.io
function getErrorLogger(): ErrorLogger {
const hasConfig =
process.env.NODE_ENV === "production" &&
isLiveEnv() &&
process.env.REACT_APP_AIRBRAKE_PROJECT_ID &&
process.env.REACT_APP_AIRBRAKE_PROJECT_KEY;

Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/lib/featureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const hasFeatureFlag = (featureFlag: featureFlag) =>
has: hasFeatureFlag,
};

if (process.env.NODE_ENV !== "test") {
if (process.env.REACT_APP_ENV !== "test") {
// log current flag status on page load
console.debug(
activeFeatureFlags.size > 0
Expand Down
3 changes: 3 additions & 0 deletions editor.planx.uk/src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import "@testing-library/jest-dom";
import "jest-localstorage-mock";
import "jest-axe/extend-expect";

import dotenv from "dotenv";
import { mockFade } from "testUtils";

dotenv.config({ path: "./.env.test" });

beforeAll(() => mockFade);
3 changes: 3 additions & 0 deletions editor.planx.uk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ export const fetchCurrentTeam = (): Team | undefined => {
});
return data?.teams[0];
};

export const isLiveEnv = () =>
["production", "staging", "pizza"].includes(process.env.NODE_ENV || "");
3 changes: 1 addition & 2 deletions infrastructure/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as postgres from "@pulumi/postgresql";
import * as mime from "mime";
import * as tldjs from "tldjs";
import * as url from "url";
import * as random from "@pulumi/random";

import { generateTeamSecrets } from "./utils/generateTeamSecrets";
import { createHasuraService } from "./services/hasura";
Expand Down Expand Up @@ -304,7 +303,7 @@ export = async () => {
memory: 1024 /*MB*/,
portMappings: [apiListenerHttps],
environment: [
{ name: "NODE_ENV", value: "production" },
{ name: "NODE_ENV", value: env },
{ name: "EDITOR_URL_EXT", value: `https://${DOMAIN}` },
{ name: "AWS_S3_REGION", value: apiBucket.region },
{ name: "AWS_ACCESS_KEY", value: apiUserAccessKey.id },
Expand Down

0 comments on commit a88cb71

Please sign in to comment.