简体   繁体   中英

Rails credential values are nil while running tests on github actions

I am encoding and decoding JSON web tokens using Rails secret_key_base, my secret_key_base is in the credentials.yml.enc file. In one of the test, I am using this function to decode JWT,locally the tests are running fine but on github action it is failing, I found out the the value of Rails.application.crendentials.secret_key_base is nil when running the test on github action. I fixed those tests by mocking like this

    allow(Rails.application.credentials).to receive(:secret_key_base).
      and_return("secret")

Is there a way I don't have to do this on github action for other credentials. Also since the master.key was not committed I hoped that I would see this error

ActiveSupport::MessageEncryptor::InvalidMessage

while reading from the credentials file but that also didn't happen. This is a link to my project if that clears things up.

In Rails 6, you can create credentials.yml.enc file per environment.

In vscode:

EDITOR="code --wait" rails credentials:edit --environment production
EDITOR="code --wait" rails credentials:edit --environment test

it gives you production.key, production.yml.enc, test.key, test.yml.enc . You can then commit test.key to github for testing or even better, set this key in RAILS_TEST_KEY env.

Though it gets a little bit tricky to maintain both env files. You can create credenetials.yml.example file with empty envs for reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM