Skip to content
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

Merged
merged 39 commits into from
May 30, 2024

Conversation

billhimmelsbach
Copy link
Contributor

@billhimmelsbach billhimmelsbach commented Apr 29, 2024

Allows env variables to be injected in sbl-frontend at runtime by using import-meta-env and a few other tweaks to nginx, which allows for a bunch of fun stuff:

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

  • add .env to .dockerignore so that .env changes don't trigger image rebuilds
  • add .env.example for use by import-meta-env to define required env variables and to make it easy for new users to copy it and create their own .env files
  • modify .github/workflows/test.yml to add env variables to github workflows
  • modify .gitignore to ignore all .env files except the new .env.example
  • install new packages
    • import-meta-env for injecting env vars, flexibility between .env and env variables being passed on the command line, allowing configmaps
      • includes: @import-meta-env/cli, @import-meta-env/unplugin, dotenv
    • dotenv-cli for automating env var injection for yarn docker-run command
  • modify Dockerfile for env var injection
    • add import-meta-env
    • copy nginx config to allow for env var injection
    • change CMD to ENTRYPOINT for env var injection and switching to non-root user
  • update env-guide with instructions on adding new env vars
  • update index.html with a placeholder for injecting env vars for prod builds / cache busting
  • add nginx-entrypoint.sh for env var injection and switching to non-root user
  • allow for env vars in nginx.conf to modify the CSP to add localhost when serving from docker locally
  • modify package.json
    • add new packages (see above)
    • add a few sbl-frontend only ways of running docker (in the future, we'll more often than not use sbl-project, see: feat(docker): add sbl-frontend / profile to docker compose sbl-project#211)
    • modify docker-run to work now
      • removed the && open http://localhost:8080 to docker-run because it caused weird race conditions and made it hard to kill the running container
      • changed the port to 8085 to avoid using the same port as other containers
    • add docker-build-and-run to combine build and run commands
  • modify axiosService.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.
  • modify common.ts to include MAIL_BASE_URL
  • lint oidc.ts suggested removing as string now, since these env vars are already typed string
  • all API calls in /requests now use an axios instance for their API calls instead of relying on the proxy
  • modify vite.config.ts
    • remove proxy
    • add import-meta-env
    • remove envPrefix since import-meta-env handles that now

Future improvements

How to test

  1. If you're missing a variable in your .env file (see .env.example for complete list) do you see an error appear?

Screenshot 2024-05-29 at 7 00 36 AM

  1. Are the github actions tests on this PR that now run on env vars passing?
Screenshot 2024-05-29 at 6 56 49 AM
  1. Can you run yarn docker-build and yarn docker-run and see the website appear on http://localhost:8085/?
  • You may need to refresh the page after everything spins up, also see a future PR: #589
  • We'll mostly be using sbl-project to spin up the sbl-frontend container, but this is nice to have for standalone testing
Screenshot 2024-05-29 at 7 31 42 AM

Related PRs

@billhimmelsbach billhimmelsbach marked this pull request as ready for review May 29, 2024 14:36
@billhimmelsbach billhimmelsbach changed the title wip: feat(env): load env vars at runtime with import-meta-env feat(env): load env vars at runtime with import-meta-env May 29, 2024
meissadia
meissadia previously approved these changes May 29, 2024
Copy link
Collaborator

@meissadia meissadia left a 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!

@shindigira
Copy link
Contributor

Not critical but I think we can include https://import-meta-env.org/guide/tools/typescript.html to package.json with a script instead of relying on the manually created vite-env.d.ts

@billhimmelsbach
Copy link
Contributor Author

billhimmelsbach commented May 29, 2024

Not critical but I think we can include https://import-meta-env.org/guide/tools/typescript.html to package.json with a script instead of relying on the manually created vite-env.d.ts

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.

Copy link
Contributor

@shindigira shindigira left a 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!

.github/workflows/test.yml Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@shindigira shindigira left a 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 variables
  • yarn start conditionally created the vite-env.d.ts

@billhimmelsbach billhimmelsbach merged commit 21ba292 into main May 30, 2024
4 checks passed
billhimmelsbach added a commit to cfpb/sbl-project that referenced this pull request May 30, 2024
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
billhimmelsbach added a commit to cfpb/regtech-deployments that referenced this pull request May 30, 2024
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants