caprover
caprover copied to clipboard
[Question] Share Env variables between apps?
Hello! Is it possible to share environment variables between apps. I have searched the docs and past issues and I think the answer is no, there is no way to do this, but I wanted to ask just to be sure.
My use case for this would be when deploying an app with multiple captain-definition
files in it. It would be nice to be able to have the same environment for both apps since the codebase is the same and most of the environment variables would be needed for both (db url, auth keys, config variables, etc.). Currently, I think the only way to have the same environment variables would be to manually make sure they are the same. I was hoping there would be a 'nicer' way of doing this, where there could be one source of truth for the environment rather than multiple.
Thanks!
This would be outside the scope of CapRover.
You would need some sort of helper utility to listen to or update your environment variables and then trigger a deployment with them, per app. There are other ways this could be done, such as pulling variables dynamically from a different source, such as a Redis cache but I wouldn't recommend that. Your solution will be application and workflow specific.
Thanks for the quick response @maietta!
This would be outside the scope of CapRover.
Got it, that is what I thought.
I realize CapRover is very conscious about keeping features limited to what is only necessary for core functionality. But would it be possible to consider adding this as a feature? I would imagine it isn't uncommon when having a web/worker setup to want the same variables across each app?
I am not extremely fluent with the CapRover codebase but I did look through some of it, have reviewed all of the docs, and played around with my own VPS with CapRover enough that I had an idea for how this could be implemented in a way I would hope is not very intricate.
Environment variables for each app are stored in config-captain.json
. What if there also was the concept of an 'App Group'. An App Group would have a name and environment variables. If an app was a part of a group it would include that as a new field in its config. As far as docker goes, there would be a new step that would check if an app is in a group and if it is, would add in the group's environment variables before adding in the app's environment variables. To edit these in the Web UI, there could be a section for group variables followed by a section for app variables.
Again, I realize CapRover works so well because it avoids adding unnecessary features, but this seems pretty basic to me while also (I believe hopefully) not being too tricky to implement! Either way, I figured I would put it out there for discussion.
Thanks again!
I appreciate the feedback. Although I have not previously encountered the need to share environment variables between applications, it does seem reasonable to do so. It would be interesting to have a feature that allows linking an application to another application after deployment or during the deployment process. I am not sure of the best way to implement this, but it would be great to hear from others as I believe it could be a valuable feature request.
I would find this useful, for a similar use-case as explained by @dunkmann00, where I'm deploying the same codebase to multiple (well, two) different apps on CapRover, and altering the behaviour of these different "clones" merely through the environment variables. I wasn't sure whether this practice was frowned upon, or whether a better approach should be adopted, but it works for me...
Except that it's not ideal to have duplicated environment variables between the multiples apps. In my case, the behaviour might be controlled differently only via one or maybe two env vars, while the rest should be common, and it's a pain (and error-prone) to have to keep two independent sets of env vars aligned.
I also have a couple of env vars which I use across multiple apps, e.g. to control which database app to access, and when I change one I usually want to change them all (I presently do a search/replace in the config file).
So, rather than being able to link one app to another (for env var purposes) I would envisage having a number of named "env var groups" (defined completely independent of any app), which you can enable/apply if desired in any of your apps... this would allow you to apply env vars from one or more of those groups to an app, e.g. maybe a core set of env vars, and then you could even override certain env vars from a group by setting a different value in the config for the app itself.
That said, I understand completely if the goal of CapRover is to provide a basic set of features that do most things required of all users in an uncomplicated way, rather than a whole bunch of bespoke features that might only be of use to a limited number of users (and confusing/distracting for other users).
It would be great to have this, in rancher for example, you can create a group of apps and in this group define the environment variables - it looks good with the tags in caprover which was recently introduced. I believe that based on these tags defining the environment variables would be the easiest way to achieve this objective.