0

I've been working on deploying a full-stack application via Google App Engine. If I try to let App Engine build from source, Google's buildpacks obviously doesn't know what to do with my Yarn workspace dependency URLs. So for the client, I just build locally and deploy the static asset. No problem. (This is a partial workaround for Deploying yarn workspace monorepo app with internal dependencies to Google App Engine and, more obliquely, How can I deploy to Google App Engine an app that depends on a yarn workspaces without publishing the packages to a npm registry?)

This is fine for a client, but I'm running into an issue deploying the back-end: evidently, the server should listen on the App Engine environment's PORT env var. The only way that I can think of to accomplish this is to deploy the source and allow it to be be built via Google App Engine; but, as I mentioned, this is incompatible with workspace dependency URLs. The node app still runs (and can even perform egress) if I build it locally before deploying to GAE, but because it is not listening on the PORT env var the server takes no requests. I don't want to have to publish the back-end's workspace-level deps.

I'm sure someone must have experience deploying a node app which depends on both Yarn workspace-level dependencies and production-env-scope env vars. Can you give me some direction?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
John Camden
  • 592
  • 5
  • 15
  • Why can't the _built_ backend app access the environment variable? – jonrsharpe Jun 02 '21 at 07:22
  • My references to env vars are resolved during the build process (I'm building/bundling a node app with Webpack 5). I have never looked into coding an application that would still be looking for environment vars after it has been built. However, I think (perhaps wrongly) that env vars set in the App Engine app.yaml are build env vars rather than runtime env vars, so the app would need to be built by App Engine in order to access those env variables anyway. Not sure since the 8081 port workaround (below) disincentivized my looking into this more thoroughly I'd love to know if I'm wrong. – John Camden Jun 08 '21 at 14:15
  • 1
    Per https://cloud.google.com/appengine/docs/standard/nodejs/config/appref it looks like you can have both `build_env_variables` and `env_variables`. Wherever possible I'd avoid build-time configuration because it means you have to build different artifacts for different environments, rather than building one and promoting it (I've explained more at https://blog.jonrshar.pe/2020/Sep/19/spa-config.html) and you don't need to bundle server-side code with Webpack. – jonrsharpe Jun 08 '21 at 14:33
  • (By the way, I forgot that the point was to try to *not* provide a PORT env var in the app.yaml. I guess the idea is to listen on a the PORT runtime env var; but, as I mentioned, I have no idea how to do that.) – John Camden Jun 08 '21 at 14:34
  • You look in `process.env` at runtime, and use that value, see e.g. https://github.com/textbook/starter-kit/blob/24a130757072990fce6e4656a7fbe788c8397679/server/server.js#L5 (I haven't run this on GAE but it works in several other environments and I've no reason to believe it wouldn't work there too). – jonrsharpe Jun 08 '21 at 14:35
  • Ah, I see: you want no build-time config at all (e.g. no dotenv) so process.env references are still around at runtime. Totally makes sense. Thanks. Re: bundling/building with Webpack, I can't remember now why I needed to do this, but I think perhaps it was for copying security files (e.g. keys) into the build directory. In this case, I think your strategy would probably be not to supply those kinds things as part of the build. Probably a good idea... – John Camden Jun 08 '21 at 14:46
  • 1
    dotenv is a good way to simply configure the development environment, but in production those things should be coming from the actual environment. You probably have Webpack's DefinePlugin (see https://github.com/textbook/starter-kit/wiki/Client-environment-variables) baking the env vars into your actual code, that's the bit that's a problem (particularly for things that should be secret). – jonrsharpe Jun 08 '21 at 14:51
  • Oh actually, one could just copy in the security files as part of the build script, so apparently I don't know anymore why I needed Webpack for the backend... – John Camden Jun 08 '21 at 14:51

1 Answers1

0

(Edit: it would be better to adopt the paradigm described by jonrsharpe in the comments above---that is, retrieve environment variables from the actual production environment (i.e. App Engine) instead of baking them into a build.)

I just had to listen on port 8081 (that is, build locally with PORT set to 8081 and then deploy the built server). Perhaps 8080 was taken up either the default service (the client, in my case) or perhaps by nginx. I'm not quite sure, but I'm moving on for now.

John Camden
  • 592
  • 5
  • 15