4

In lib/tasks/sitemap.rake:

namespace :sitemap do

  task update: :environment do
    Rake::Task["sitemap:generate"].execute
    key = ENV['AWS_ACCESS_KEY_ID']
    secret = ENV['AWS_SECRET_ACCESS_KEY']
    bucket = ENV['S3_BUCKET']

    s3 = AWS::S3.new(access_key_id: key, secret_access_key: secret)
    s3.buckets[bucket].objects['sitemap.xml'].write(data: File.open(Rails.root.join('tmp','sitemaps','sitemap.xml')), acl: :public_read)
  end

end

In this code, if I type the strings manually the process runs fine. But as soon as I use ENV I get the following error:

rake aborted!
AWS::Errors::MissingCredentialsError: 
Missing Credentials.

How can I safely use the credentials without setting them in my committable codebase.

sergserg
  • 21,716
  • 41
  • 129
  • 182
  • Are you using Foreman or Figaro? – CDub Apr 12 '15 at 02:57
  • @CDub: Foreman. Installed using Homebrew on Yosemite. – sergserg Apr 12 '15 at 02:58
  • So it looks like if I run `foreman run rake sitemap:update` it works fine... Hm... I wonder if I have to use `foreman run` on the Heroku Scheduler for it to run proper. – sergserg Apr 12 '15 at 03:05
  • 1
    Yes, for Foreman, you'll need to use `foreman run` - if you use Figaro, the application.yml will be part of your environment, thus running `rake` alone will work. – CDub Apr 12 '15 at 03:08

1 Answers1

2

Foreman loads the environment variables before starting up the processes defined in your Procfile.

Since we're using these values in a rake task - not an actual web process, you can run:

foreman run rake sitemap:update

This will make foreman load the values before running the rake task.

sergserg
  • 21,716
  • 41
  • 129
  • 182