Configure Rake Tasks for Deployed Apps
Page last updated:
For PWS to automatically invoke a Rake task while a Ruby or Ruby on Rails app is deployed, you must:
- Include the Rake task in your app.
- Configure the application start command using the
commandattribute in the application manifest.
The following is an example of how 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/tasksdirectory 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.ymlfile with the
commandattribute, referencing the idempotent command
rake db:migratechained with a start command.
applications: ‐ name: my-rails-app command: bundle exec rake cf:on_first_instance db:migrate && rails s -p $PORT