-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for student project environment variables #632
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
…omments
Can students use the If this works, then we can avoid making a change to Preflight. |
…QL setup output
docker/clone-and-preflight.ts
Outdated
if (preflightEnvironmentVariables) { | ||
const environmentVariablesKeys: string[] = JSON.parse( | ||
preflightEnvironmentVariables, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the environment variables are found, JSON.parse
converts the string representation of an array into an array.
'["NEXTAUTH_URL", "APP_SECRET_KEY"]'
to ["NEXTAUTH_URL", "APP_SECRET_KEY"]
docker/clone-and-preflight.ts
Outdated
const preflightEnvironmentVariables = postgresProcess.stdout.match( | ||
/PREFLIGHT_ENVIRONMENT_VARIABLES:\n(\[.*?\])/, | ||
)?.[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alpine-postgresql-setup-and-start
script prints PREFLIGHT_ENVIRONMENT_VARIABLES:
followed by a string
representation of an array. Use .match()
to locate this line and extract the array from the next line.
docker/clone-and-preflight.ts
Outdated
for (const key of environmentVariablesKeys) { | ||
process.env[key] = `fake_${key.toLowerCase()}`; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get a value from the array and create an environment variable with a fake value based on its name. If the array is empty, no additional environment variable will be set
docker/clone-and-preflight.ts
Outdated
stderr: 'inherit', | ||
})`bash ./scripts/alpine-postgresql-setup-and-start.sh`; | ||
|
||
const preflightEnvironmentVariables = postgresProcess.stdout.match( | ||
/PREFLIGHT_ENVIRONMENT_VARIABLES:\n(\[.*?\])/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match minimum 1 character that is not ]
inside of the array, so that we don't do anything for empty arrays
/PREFLIGHT_ENVIRONMENT_VARIABLES:\n(\[.*?\])/, | |
/PREFLIGHT_ENVIRONMENT_VARIABLES:\n(\[[^\]]+\])/, |
docker/clone-and-preflight.ts
Outdated
); | ||
|
||
for (const key of environmentVariablesKeys) { | ||
process.env[key] = `fake_${key.toLowerCase()}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
maybe 'fake_' isn't expressive enough for a student debugging why they're app isn't working (eg. if their app actually needs the real env var value)
so probably best to make the prefix or value more expressive
-
you probably can get rid of the
key.toLowerCase()
suffix too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the fake
prefix, and changed it to MISSING_ENV_
, so when students debug, they would see MISSING_ENV_CLOUDINARY_API_KEY
instead of fake_cloudinary_api_key
5a07f4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, MISSING_ENV_
is not much more helpful than fake_
- I guess we should be more expressive and make sure students can easily find the source of this (probably should mention Preflight and maybe UpLeveled)
probably also with some kind of mention of "fake" or "dummy" or "placeholder"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed now to UPLEVELED_PREFLIGHT_PLACEHOLDER
, I think students will understand that this comes from Upleveled and is for Preflight as a placeholder.
7dcffd4
Co-authored-by: Karl Horky <[email protected]>
…PREFLIGHT_PLACEHOLDER'
stdout: ['inherit', 'pipe'], | ||
stderr: ['inherit', 'pipe'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to ['inherit', 'pipe']
for programmatic + terminal output. Print directly in the terminal with inherit
and capture the process output with pipe
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
const preflightEnvironmentVariables = postgresProcess.stdout.match( | ||
/PREFLIGHT_ENVIRONMENT_VARIABLES:\n(\[[^\]]+\])/, | ||
)?.[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alpine-postgresql-setup-and-start
script prints PREFLIGHT_ENVIRONMENT_VARIABLES:
followed by a string representation of an array. Use .match()
to locate this line and extract the array from the next line.
docker/clone-and-preflight.ts
Outdated
if (preflightEnvironmentVariables) { | ||
for (const key of JSON.parse(preflightEnvironmentVariables) as string[]) { | ||
process.env[key] = 'UPLEVELED_PREFLIGHT_PLACEHOLDER'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the script uses additional environment variables, this gets a value from the array and create an environment variable with a UPLEVELED_PREFLIGHT_PLACEHOLDER
value. If the array is empty, no additional environment variable will be set
pgctl start
sindresorhus/execa#1191Prerequisite for
Preflight is failing in Alex Ecommerce Store Project due to missing
env
variables. This project is the first to include additional environment variables beyond the database ones we teach in the lectures. Since we don't check the.env.example
file, Preflight was relying on only the default environment variables, causing it to fail during the dotenv-safe check.This PR updates how Preflight handles additional environment variables. The
alpine-postgresql-setup-and-start.sh
script now prints the required variables. Preflight captures this output usingstdout: pipe
, extracts the exact line, and creates environment variables with fake values. With this PR Preflight runs without failing due to missing environment variables.Preflight output after a successful test