Pass variables to a Rake task and use them
One approach http://www.viget.com/extend/protip-passing-parameters-to-your-rake-tasks/
Stefan recommends a cleaner approach:
http://blog.jayfields.com/2006/10/rake-tasks-with-parameters.html
The key elements
It is easy to test if the parameter was passed in at all (checking if 'branch' were passed in) and if not, to declare a default.
if ENV.include?("branch")
branch = ENV['branch']
else
branch = 'master'
end
The branch = ENV['branch']
line is when we assign the parameter passed in to a variable.
In use
rake test vlad:uptest branch=dsfix
Seeing available commands
To get an abbreviated, one-per-line list of available commands, use rake -T
. To always get the full description of all tasks, even if it runs to multiple lines, use rake -D
. Another trick is that you can use the task name, or any portion of the task name, to filter the -T or -D lists.
ben@ubuntu:~/code/sdl/web (master)$ rake -D vlad:up
(in /home/ben/code/sdl)
rake vlad:update
Updates your application server to the latest revision.
rake vlad:update_settings
Updates the drupal settings for the selected environemnt.
rake vlad:uptest
Updates the test site to the latest code of the specified 'branch=myfeature' (defaults to master) and brings in a copy of the live database, and runs update.
Gotchas
With origin undefined in this line, it just silently failed:
system "git push #{origin} #{branch}"
Call a Rake task from a Rake task
The in
Rake::Task["namespace:taskname"].invoke
The Rake Task in all its glory
Note: This is before Stefan starts fixing it.
desc "Updates the test site to the latest code of the specified 'branch=myfeature' (defaults to master) and brings in a copy of the live database, and runs update.php.".cleanup
remote_task :uptest, :roles => :app do
if environment != "test"
puts "You can only deploy arbitrary branches on the test site."
raise "Turns out raise doesn't print a damn thing but it ends the task."
end
if ENV.include?("branch")
branch = ENV['branch']
else
branch = 'master'
end
system "git push #{origin} #{branch}"
run [ "cd #{deploy_to}",
"git fetch #{origin}",
"git checkout #{branch}",
].join(" && ")
run "drush -r #{release_path} -y sql-drop"
Rake::Task["vlad:db:sync_from_stage"].invoke
run "cd #{release_path} && drush updb && drush cache-clear all"
end
Comments
Post new comment