User login

Pass variables to a Rake task and use them

in

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

Searched words: 
Don't know how to build task 'example' rake pass a variable rake get full description rake call another task

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.