Twizzle Your Deplizzles

Steve and I have a Twitter account that we send all our commits to. It is all handled by Github and both of us find it really handy. Rain or shine, we get commit updates on our phone which is great for staying in the loop.

For a little while now, I’ve been wanting to add deploy notices to this twitter account and tonight I finally got around to it. It was pretty easy, but I’ll post the code here to save others time. I went the no dependencies route (even though I created the Twitter gem).

set :twitter_username, 'username'
set :twitter_password, 'password'

namespace :twitter do
  task :update do
    require 'open-uri'
    require 'net/http'
    
    url = URI.parse('http://twitter.com/statuses/update.xml')
    request = Net::HTTP::Post.new(url.path)
    request.basic_auth twitter_username, twitter_password
    request.set_form_data('status' => "Deployed #{current_revision[0..6]} to #{rails_env}")
    
    begin
      response = Net::HTTP.new(url.host, url.port).start do |http|
        http.open_timeout = 10
        http.request(request)
      end
      puts 'Deploy notice sent to twitter'
    rescue Timeout::Error => e
      puts "Timeout after 10s: Seems like Twitter is down."
      puts "Use \"cap twitter:update\" to update Twitter status later w/o deploying"
    end
  end
end

after "deploy", "twitter:update"

Just drop this in your deploy file and update the username and password. Notice that I even included the git revision and rails environment so you can easily see what was last deployed and where to. You end up with a nice little message like this:

Deployed 09ea23f to staging

Very handy!

0 Comments

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.