1

I need to a create a Capistrano pre-deploy step that runs a custom rake task.

in deploy.rb:

before 'deploy:starting', 'db:rollback_staging'

namespace :db do
  desc 'Rollback staging db only if PR already deployed requires rollback'
  task :rollback_staging do
    on roles(:master) do
      within current_path.to_s do
        with rails_env: 'staging' do
          execute :rake, 'release:rollback_staging'
        end
      end
    end
  end
end

The problem is that when deploying this code the rake task is not yet present on the server and therefore deploy fails with:

rake stdout: rake aborted!

Don't know how to build task 'release:rollback_staging' (See the list of available tasks with `rake --tasks`)

If there a way to check if the rake task exists from Capistrano? smth like:

with rails_env: 'staging' do
  execute :rake, 'release:rollback_staging' if rake_exists? 'release:rollback_staging'
end
Konstantin Rudy
  • 2,237
  • 25
  • 22

2 Answers2

1

I ended up just ignoring not 0 exit code from a rake task using raise_on_non_zero_exit: false:

with rails_env: 'staging' do
  execute :rake, 'release:rollback_staging', raise_on_non_zero_exit: false
end
Konstantin Rudy
  • 2,237
  • 25
  • 22
0

Would this work? Saw this pattern in https://github.com/AgileConsultingLLC/capistrano3-delayed-job

if Rake::Task.task_defined?('release:rollback_staging')
  • 2
    Welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) If this is another question, please post it as such. If it is an answer please edit it. – Tomer Shetah Dec 17 '20 at 11:12