Configuring Rake Tasks for Deployed Apps

Page last updated:

Warning: Pivotal Application Service (PAS) v2.8 is no longer supported because it has reached the End of General Support (EOGS) phase as defined by the Support Lifecycle Policy. To stay up to date with the latest software and security updates, upgrade to a supported version.

For Cloud Foundry to automatically invoke a Rake task while a Ruby or Ruby on Rails app is deployed, you must do the following:

  • Include the Rake task in your app
  • Configure the application start command using the command attribute in the application manifest

The following is an example that shows how to invoke a Rake database migration task at application startup.

  1. Create a file with the Rake task name and the extension .rake, and store it in the lib/tasks directory of your application.

  2. 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

    This Rake task limits an idempotent command to the first instance of a deployed application.

  3. Add the task to the manifest.yml file with the command attribute, referencing the idempotent command rake db:migrate chained with a start command.

    ‐ name: my-rails-app
      command: bundle exec rake cf:on_first_instance db:migrate && rails s -p $PORT