November 3, 2011

Deploying hubot with capistrano and forever

Just recently github released their hubot into the wild. Hubot really is a nice piece of software. There’s even a really easy way to deploy it to Heroku. But wait… what if you want to deploy to your own server?

Here’s one way using capistrano & forever. But first: a complete capistrano example:

set :application, "hubot"

set :scm, :git
set :ssh_options, { forward_agent: true }
set :branch, "master"

set :deploy_to, "/home/hubot"
set :deploy_via, :remote_cache
set :repository,  "[email protected]"

set :user, "hubot"
set :use_sudo, false

default_environment['PATH'] = "$PATH:#{deploy_to}/.node_modules/.bin"

role :web, "your-servers-ip"
role :app, "your-servers-ip"
role :db,  "your-servers-ip", primary: true

namespace :deploy do
  task :start do
    log_file = "#{shared_path}/log/hubot.log"
    run "if [ -e #{log_file} ]; then rm #{log_file}; fi"
    run ". ~/.env && \
      cd #{release_path} && \
      forever start --pidfile #{shared_path}/pids/hubot.pid -l #{shared_path}/log/hubot.log -c coffee bin/hubot -a xmpp"
  end
  task :stop do
    run "cd #{release_path} && \
      forever stop bin/hubot"
  end
  task :restart do
    stop
    start
  end
end

Now, the interesting parts:

  • set :deploy_to, "/home/hubot"

    I’m assuming you created a new unix user for your hubot. While this is not necessary it keeps things cleanly separated (one user per app/ project)

  • default_environment['PATH'] = "$PATH:#{deploy_to}/.node_modules/.bin"

    Unless you installed all node dependencies globally, we have to update the $PATH here to locate all binaries.

    Hubot has plenty npm dependencies, and I’m assuming you installed them locally. Since these dependencies only have to be installed once, and not every time you deploy your hubot, I like to place them in a hidden node_modules folder inside your users home directory.

    Please note that you could also place them in the shared_path. Either way, you will have to run npm install and move the packages by hand, once.

  • run "if [ -e #{log_file} ]; then rm #{log_file}; fi"

    When using forever, you’ll have to remove old log-files before you can start a new instance. First we check to see if the logfile exists (if [ -e #{log_file} ];), and if so, we remove it (then rm #{log_file}; fi)

  • . ~/.env

    Adding settings to repositories is bad practice, so I place them in a .env file and load them before spinning up hubot. This is equivalent to source ~/.env

  • forever start --pidfile #{shared_path}/pids/hubot.pid -l #{shared_path}/log/hubot.log -c coffee bin/hubot -a xmpp

    This tells forever to start hubot using the coffee command. This is important, since hubot is written in coffeescript. We’re also setting a pid- and logfile here for tracing & debugging purpose. Last but not least we’re starting hubot with the xmpp adapter active.

Some things to watch out for:

  • make sure to install coffeescript and forever binaries beforehand!
  • try to run hubot with your final configuration over SSH just to make sure it’s correct! If it doesn’t work, it needs tweaking!

If you’re having trouble getting your hubot up and running I recommend visiting the hubot wiki - the configuration options are being documented there.

That’s it!

cap deploy && Rock on!

© Raphael Randschau 2010 - 2022 | Impressum