Rails 6 app not reading ENV on Heroku during tests

Viewed 542

I have a Rails 6 app deployed to Heroku and working fine. I've just started adding an API to the app, and this is also working fine locally, with all tests passing locally.

When I deploy to Heroku, the app responds correctly to curl.

However, my tests (running via Heroku CI) are failing with TypeError: no implicit conversion of nil into String during authorisation of the JWT token. I've identified the root cause, which is that the DEVISE_JWT_SECRET_KEY environment variable isn't being loaded for my tests.

This variable is defined as a Heroku environment variable, and works fine when I test the actual app using curl, as I mentioned. Running

heroku config:get DEVISE_JWT_SECRET_KEY -a [APP_NAME]

returns the correct key.

When I add a line to my tests to check for the variable, the raise is executed when the tests run, showing that the variable is missing.

raise if ENV['DEVISE_JWT_SECRET_KEY'].nil?

Can anyone suggest why environment variables aren't loading when I run my tests on Heroku?

1 Answers

Note that unlike review apps, Heroku CI does not inherit any config vars from the parent app.

To set confidential or volatile environment variables (such as access tokens) that you shouldn’t include in app.json, you can instead add them to your pipeline’s Heroku CI settings in the Heroku Dashboard.

https://devcenter.heroku.com/articles/heroku-ci

So it would be wrong to expect your CI to use the config vars you set through the CLI and that could potentially lead to some really serious problems if your CI for example used real third party credentials by mistake.

Related