Capistrano Twitter task, take 2 2
In my original post, I explained how I used the twitter4r gem to post when we deployed an app with capistrano. Chris Matthieu posted a comment about how to do it without the twitter4r gem. I actually like this better, especially after we started having trouble with the twitter4t gem. So, here’s his version that I adapted slightly:
desc 'posts to twitter that an application has been deployed to a web server'
task :send_tweet do
require 'open-uri'
require 'net/http'
url = URI.parse('http://twitter.com/statuses/update.xml')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'your_username' + ":" + 'your_password', ''
req.set_form_data({'status' => "Deployed #{application} to #{rails_env}"})
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
endThanks, Chris!
Capistrano Twitter task 1
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}")
endThe 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
