-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(env): load env vars at runtime with import-meta-env #428
Conversation
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.
Awesome, really appreciate the documentation, and it works!
Not critical but I think we can include https://import-meta-env.org/guide/tools/typescript.html to |
I ran this tool to generate that file @shindigira, so I'd be down to generate that file regularly / include it as a package.json script? EDIT: I did rename it vite-env.d.ts, since that's what the vite docs recommended. |
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.
Two small changes. Really awesome!
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.
Tested and verified:
- missing environment variables will cause a terminal error
- ensured all environment variables are used in local development
yarn start
will prompt to install a typescript auto-generating package for the env variablesyarn start
conditionally created thevite-env.d.ts
Let's add `sbl-frontend` to `sbl-project` 🥳 Closes #210 ## Changes - add `sbl-frontend` to `docker-compose.yml` (depends on cfpb/sbl-frontend#428) - add a `frontend` profile for easy startup (works with #213) - add a `frontend.local.env` with non-secret vars for local dev ## How to test this PR 1. Wait for cfpb/sbl-frontend#428 to get merged 2. Run `git checkout main && git pull" in the `sbl-frontend` repo to ensure you have the latest changes 3. Run `docker-compose --profile "*" up"` 4. Wait for things to spin up 5. Does navigating to http://localhost:8898/ result in seeing the home page? ## TODO - [x] merge cfpb/sbl-frontend#428 - [x] profiles: #212 which is closed by #213
Let's use a configmap for the `sbl-frontend` repo, since we can [use env vars at runtime](cfpb/sbl-frontend#428) now 🥳 Closes #14 ## Current behavior - env vars need to be baked into images in order for `sbl-frontend` to work ## Expected behavior - container env vars are defined by a configmap, templatized in the helm values.yaml, and then loaded at runtime by `import-meta-env` ## Additions - same changes as previous PR #7 for user-fi, but also includes `envFrom` - adds `configmap.yaml` - eval template in `deployment.yaml` - add configmap and `envFrom` to `values.yaml` ## Testing 1. Test in conjunction with the accompanying enterprise PR, see the testing instructions there ## TODOs - [x] depends on [this SBL Frontend PR to be merged first](cfpb/sbl-frontend#428)
Allows env variables to be injected in
sbl-frontend
at runtime by usingimport-meta-env
and a few other tweaks to nginx, which allows for a bunch of fun stuff:yarn docker-build-and-run
to run just the frontend with env vars injected.env
file like normal)sbl-frontend
tosbl-project
docker-compose.yaml
(feat(docker): add sbl-frontend / profile to docker compose sbl-project#211)There's a lot more that can be done here (see Future Improvements section), but this should be a good start. 🤞
This PR changes quite a few things, so I listed why I made each change in the section below (starting from the top of files changed list and working my way down) and included a bunch of code comments which I hope is helpful. 🤞
Changes
.env
to.dockerignore
so that.env
changes don't trigger image rebuilds.env.example
for use byimport-meta-env
to define required env variables and to make it easy for new users to copy it and create their own.env
files.github/workflows/test.yml
to add env variables to github workflows.gitignore
to ignore all .env files except the new.env.example
import-meta-env
for injecting env vars, flexibility between.env
and env variables being passed on the command line, allowing configmaps@import-meta-env/cli
,@import-meta-env/unplugin
,dotenv
dotenv-cli
for automating env var injection foryarn docker-run
commandimport-meta-env
CMD
toENTRYPOINT
for env var injection and switching to non-root usernginx.conf
to modify the CSP to addlocalhost
when serving from docker locallysbl-frontend
only ways of running docker (in the future, we'll more often than not usesbl-project
, see: feat(docker): add sbl-frontend / profile to docker compose sbl-project#211)docker-run
to work now&& open http://localhost:8080
todocker-run
because it caused weird race conditions and made it hard to kill the running container8085
to avoid using the same port as other containersdocker-build-and-run
to combine build and run commandsaxiosService.ts
to change from a proxy based approach for base urls to per API axios instance approach. I talked with @shindigira about this: trying to replicate this kind of behavior in nginx causes some load balancer / upload issues, and causes some weirdness. This is a commonly used approach around these issues.common.ts
to includeMAIL_BASE_URL
oidc.ts
suggested removingas string
now, since these env vars are already typed string/requests
now use an axios instance for their API calls instead of relying on the proxyvite.config.ts
envPrefix
since import-meta-env handles that nowFuture improvements
How to test
.env
file (see .env.example for complete list) do you see an error appear?yarn docker-build
andyarn docker-run
and see the website appear on http://localhost:8085/?sbl-project
to spin up thesbl-frontend
container, but this is nice to have for standalone testingRelated PRs