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
Allow loading of values from .env file repo root #391
Comments
Spoke with @jongio and we are going change this slightly to say "a .env file next to |
@ellismg @rajeshkamal5050 I think that supporting something like this would alleviate some of the headaches we're seeing around azd + environment variables (notably things like passing API keys down into Bicep). If we can load the .env in the root then a workflow like this:
could end up looking more like this with the simplified init flow:
|
@savannahostrowski The behavior you're describing would be resolved with #2739 Do we want also want to add a "global .env" as suggested by the issue? Given how remote environments have been implemented, I think the |
We can also take the stance that Another thing to note: resharing env files for multiple purposes can lead to env var management complexity. |
I was looking for more discussions on the subject of environment variables and stumbled across this issue so I'm looking forward to seeing what changes you might make in this area. I went through a few iterations of how to deal with env vars in my azd template, and although I'm pretty happy with the end solution I know that it doesn't address some areas (like secure varaibles e.g. from a key vault) so I think I will revisit it at some point. My template includes a Next.js app and the main challenge I wanted to solve in my template was keeping the management of environment variables at development time exactly as per Next.js's docs, but have them flow through into azd and the IAC scripts as seamlessly as possible i.e. not having to define the same environment variable in several different places. I also needed to expose the output from the IAC scripts in the azd env files (under the I was able to use azd hooks and some custom scripts to achieve what I wanted and I'm happy with that approach. The remote environments feature was released pretty much as I was completing development of my template so I need to dig into that, but reading through the comments on this ticket it sounds like the expectation for an azd template would be that the default location for managing environment variables would be inside the My issue with that approach would be that you would have to change the "normal" development flow of your app to work with azd, but it feels like it should be the other way around - you should continue to develop your app as you normally would and azd should work on top of that. For example, Next.js expects your Am I right in my understanding that the recommendation from azd will become to manage environment variables for your apps/services inside the |
Current date, azd's env file and the app env file are indeed separate. azd's env file holds Azure infrastructure config or output & potential input, and the app env file could contain settings for your app. This kind of design would allow separation of concerns, where your Azure environments can be managed independently than the different variations of your app. Such usage could include:
In the current system, suppose that if you wanted to point to Azure resources in your app, you could:
I do think that there can be a more streamlined experience for hosting simple static web apps. An interesting idea here is: what if azd allowed you to have an option in azure.yaml to do that we did previously, but in an automated fashion: infra:
env-export:
- dev: env.development
- staging: .env.staging
- production: .env.production The schema is slightly more verbose if we wanted to support prefix(es) so that azd doesn't end up overwriting your app's .env file unintentionally. |
@CMeeg Thanks for the feedback.
I love that you called this out. This is something we should have top of mind when we are designing improvements here. We do strive to map to the developer's local dev paradigm where possible and play nice with other tools and conventions in their ecosystem. @weikanglim I do sort of like your idea here. I wish we could just honour what the developer has in their project root and use that where needed. Copying things between .envs is sort of confusing for me personally and feels like azd clutter on top of what the developer has already configured. |
Thanks for clarifying @weikanglim and for the feedback @savannahostrowski. It sounds like maybe I did misinterpret the intended direction that this issue is going in. I was just concerned that I would have to change how I'm developing my app to fit with azd, but it doesn't sound like that will be the case. It sounds like how I've been treating the env files and trying to keep that separation of concerns in my template does gel with the current state of play then so that is good to know. The flow I've ended up with is:
When running in a pipeline I include a task that can load environment variables specific to the target environment from either GitHub environment variables or an AZDO variable group (depending on where you're running your pipeline) into a local env file so that the above flow can then run in the pipeline exactly as it would locally. The main upside of this approach is that I can continue to manage my environment variables as I normally would, and they become automatically "available" to use in the infrastructure through the azd preprovision hook, and the output from azd is then also automatically "available" to my app through the azd postprovision hook. I still have to make code changes to use the environment variables of course, but at least I only have to manage them in one place. The main downside I think is that a json file is probably not as "correct" as using a Bicep parameters file - this is something I am going to explore. If I understand your ideas correctly the potential additions to azd could effectively replace step 5 above, so merge/replace a local env file with the contents of an azd env file (or certain keys based on known prefix(es)). This could be useful instead of me having to write my own scripts to do that. The use cases around connecting my local app to different environment's resources in Azure I'm less sure of. Personally I am uneasy about having app settings from other environments like staging or production in my development environment, but I can see why this could be useful if you have provisioned your own dev environment in Azure and you want to run against those provisioned Azure resources locally rather than having to azd deploy. Especially, like you say, to avoid emulators or in some cases use Azure resources that don't have emulators. I think what I was hoping for when I first saw this issue was that azd would at some point support the flow of environment variables that I have now in my template, but through built-in commands or behaviour. The amount of variance in how env files work in each application/service/framework that azd wants to support must make this really hard to make a general solution for though so to be honest I'm really happy that the hooks provided by azd gives me the flexibility to implement what I have in the first place! |
@savannahostrowski @weikanglim Not a must-fix for 1.5.0. Adding it to Ge bucket. Please pull it in when appropriate. |
Some projects are going to want to put env vars that are shared in all envs and shipped in the repo, even as a placeholder, like this:
Right now we don't allow this because the user has to first create an env, then open the .env file, then copy and paste the values there.
I would like our code that loads env vars from the current env to also check and load a .env file from the root of the repo, if it exists.
Values in env/.env values would override values in root/.env, so load root/.env first and then env/.env
If we have
.env
file in root of repoAnd this in the env/.env file
Then SOMEVALUE would be set to BAR
If we only have, root/.env:
Then SOMEVALUE would be set to FOO.
The text was updated successfully, but these errors were encountered: