Search code examples
ruby-on-railsrubycredentialscircleci

Why is circle CI failing to find rails credentials but spec tests works locally?


On circle CI I get the following error

Failure/Error: hmac_secret = Rails.application.credentials[Rails.env.to_sym][:some_key]
NoMethodError:
undefined method `[]' for nil:NilClass

Solution

  • Rails credentials are encrypted in a credentials.yml.enc file; this file is safe to add to source control. Rails will also generate a master.key file, which is not added to your source control.

    The CI environment either needs a copy of the master.key in the repository when it runs its suite. If that's not possible, you can set a RAILS_MASTER_KEY env variable with the correct value.

    It is also possible that your credentials file doesn't have a field for whatever your Rails.env.to_sym evaluates to. If the file looks like this:

    development:
      secret_key: 123
    production:
      secret_key: 234
    

    using Rails.application.credentials[Rails.env.to_sym][:secret_key] in a test env won't give you a hit.