For Cloud Foundry to automatically invoke a Rake task while a Ruby or Ruby on Rails app is deployed, you must do the following:
commandattribute in the application manifest.
Use the following example to invoke a Rake database migration task at application startup:
Create a file with the Rake task name and the extension
.rake, and store it in the
lib/tasks directory of your application.
Add the following code to your rake file:
namespace :cf do desc "Only run on the first application instance" task :on_first_instance do instance_index = JSON.parse(ENV["VCAP_APPLICATION"])["instance_index"] rescue nil exit(0) unless instance_index == 0 end end
This Rake task limits an idempotent command to the first instance of a deployed application.
Add the task to the
manifest.yml file with the
command attribute, referencing the idempotent command
rake db:migrate chained with a start command.
applications: - name: my-rails-app command: bundle exec rake cf:on_first_instance db:migrate && rails s -p $PORT