Open nlien opened 3 years ago
Theoretically, you are right @nlien! At the moment I can't see any pitfalls with the approach.
If you have a Build phase
for every merge/commit on your main
branch and the Deploy phase
is manual.
You want to define multiple Deploy phases
for dev, staging, production where you set your environment variables inside the CI/CD right?
If so, it's a cool approach I didn't think of :) I don't have the time at the moment to try it out, but I will next week!
You can check the code for the .js file generation, it really doesn't do anything else besides the generation.
I do not have access to any AWS CodePipelines at the moment, but I can prepare a GitLab CI example for later use-cases if that's fine for you.
Thanks for your feedback!
Yes, my thought was to have multiple "deploy phases", where each phase represents an environment.
If you have the time and effort to prepare a use-case in GitLab CI, that would be much appreciated! I'm going to test this is out in AWS Code* myself during this week or the next, hopefully.
Cool!
Hopefully later on this week I can prepare an example for GitLab CI.
@nlien You very nearly have exactly the same flow I have, but I think if you just move the runtim-env generate part to the build, and it'd makes more sense.
If you are following a build-once approach for your CI/CD pipeline where you build your release artifacts once for all environments and then promote them up, then all you really have to do is add a step in the build process to duplicate the artifact and run runtime-env-cra
for said artifact for every environment.
For me it's this simple:
npm run build
generates the static files in an output folder called, let's it dist
dist
directory for each environment, so dist-dev
, dist-qa
, and dist-prod
runtime-env-cra --config-name ./dist-${env}/runtime-env.js --env-file .env.${env}
for each env
I use a node script to do this which is why you see that template string literal syntax. I put that script usage in an package.json
script to make it a bit cleaner, and I can control what environments are passed to the node script, always corresponding to all of the .env files in the project (just realized I could just scan the project files and the script do that for me). You could probably do this using command line utils alone, but I wouldn't recommend it.
If you have the multiple deploy phases like Kristof suggested, you can control when each artifact is released/deployed however you see fit without worrying about generating configs. The builds are completely static once built.
Problems with this approach: you have to rebuild every time you want to change environment variables for any environment, but that's pretty clearly the tradeoff we're making here, and in my opinion fine because this embodies infrastructure as code practices.
I was hoping to use this tool to generate a runtime config for my SPA in a static file server environment, like S3.
I might have misunderstood, but AFAICS, there's nothing standing in the way of running this command before uploading the assets to eg. an S3 bucket
Take this pseudo code, happening on a CI/CD environment (ie. AWS CodePipeline, Travis etc.):
Build phase
.env.template
file as part of the "artifact" (e.g. adist
directory) and deploy/upload artifact to whereverDeploy phase
npm i -g runtime-env-cra
dist
directory) from wherevercd dist/
.env.template
to.env
All keys mentioned in the.env.template
file must exist as environment variables in the deployment systemruntime-env-cra
(thus creating/overwriting theruntime-env.js
file)dist
directory to S3)IMO, this workflow could be beneficial if I want to build once, deploy anywhere. My CI/CD environment could for instance build ("build phase") every commit on my
main
branch. Later I could manually deploy any given build to any given environment using the "deploy phase"The downside/cons, the way I see it, is that we are modifying the artifact (contents of the
dist
directory) prior to deploying it. But as long as the script only creates theruntime-env.js
file by reading environment variables from the system, I see no harm in it.What are your thoughts on this? See any potential pitfalls or challenges?