Setting environment variables for test and development

I’m thinking of adding the dotenv gem (GitHub - bkeepers/dotenv: A Ruby gem to load environment variables from `.env`.) to my plugin for testing and development purposes. This is so that I can easily set my environment variables per environment.

I was wondering if I’m missing something and there’s a better way of achieving this?

What are you trying to load? You can set


for all SiteSettings.

Would that help?

Unfortunately not. I’m working on a private plugin and need to set an environment variable for sensitive data so that I’m not checking it into source control.

It’s fine setting that environment variable on the production server, but I want to be able to load this into the test environment when running tests for example. I could specify it inline, but ideally I’d want a more scalable solution.

The reason I posted is because adding that gem feels wrong, I would have assumed that Discourse would already have a solution for this problem.

1 Like

Then I think you can set envs like


For tests, I think you might set GlobalSetting.my_plugin_setting in the spec, but that’s mostly a wild guess.

Thanks, I will take a look at GlobalSetting

1 Like

You can set ENV['yourkey'] = 'yourval' in your plugin and that works. I used it just today to test my code.

Also, you can install gems specific to modes. I haven’t tried it myself yet but here’s an example. discourse-assign/Gemfile at master · discourse/discourse-assign · GitHub

1 Like