Skip to content

Instantly share code, notes, and snippets.

@itolosa
Created June 18, 2019 21:22
Show Gist options
  • Save itolosa/b335f3270f7c3912abbef45ae4126b3e to your computer and use it in GitHub Desktop.
Save itolosa/b335f3270f7c3912abbef45ae4126b3e to your computer and use it in GitHub Desktop.
Execute rake tasks including arguments using capistrano (based on: https://github.com/sheharyarn/capistrano-rake/blob/master/lib/capistrano/tasks/invoke.rake)
require 'shellwords'
namespace :invoke do
# Defalut to :app roles
set :rake_roles, :app unless fetch(:rake_roles)
desc "Execute a rake task on a remote server (cap invoke:rake TASK=db:migrate)"
task :rake do
if ENV['TASK']
on roles(fetch(:rake_roles)) do
within current_path do
with rails_env: fetch(:rails_env) do
execute 'bundle', 'exec', 'rake', Shellwords.escape(ENV['TASK'])
end
end
end
else
puts "\n\nFailed! You need to specify the 'TASK' parameter!",
"Usage: cap <stage> invoke:rake TASK=your:task"
end
end
end
@itolosa
Copy link
Author

itolosa commented Jun 18, 2019

Example: cap production invoke:rake TASK=some:customtask[argument1,argument2]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment