Capistrano Twitter task 1

Posted by Colin A. Bartlett Sun, 10 Feb 2008 15:37:00 GMT

Justin had an idea that we should post to Twitter whenever we deploy a new version of our apps to either a staging site or the production app. Turns out, this is really easy with the twitter4r gem. I used a twitter account that Justin created expressly for our internal notices and set its updates to be private. Then I created this task:

desc 'posts to twitter that something was deployed'
task :send_tweet do
  require 'rubygems'
  gem 'twitter4r'
  require 'twitter'
  require 'twitter/console'
  twitter = Twitter::Client.from_config('config/twitter.yml',rails_env)
  status = twitter.status(:post, "deployed #{application} to #{rails_env}")
end

The twitter4r library loads a simple yaml file that contains the username and password of the twitter account to use. And one line of code posts the status update with the name of the app and the environment from previously-defined variables.

That’s it! The great thing about using Twitter for this, as Justin pointed out, is that each one of us developers can consume this information how we choose: IM, Growl messages with Twitterific, SMS, etc. So it’s really just the syndication technology that we’re taking advantage of.

Update

Chris’s comment below prompted me to come up with a new version

Comments

Leave a response

  1. Avatar
    Chris Matthieu about 22 hours later:
    You could also post a tweet without the twitter gem as follows: require 'open-uri' url = URI.parse('http://twitter.com/statuses/update.xml') req = Net::HTTP::Post.new(url.path) req.basic_auth 'twitterid' + ":" + 'twitterpassword', '' req.set_form_data({'status' = "deployed #{application} to #{rails_env}"}) res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } Hope this helps! Chris Matthieu Rubyology
Comments