Continuous deployment with #Mojolicious and \@github
After hearing Curtis Poe giving the talk “Agile Companies Go P.O.P.‎” at the YAPC I thought I should finally get the word out about my extension to Mojolicious, which allow continuous deployment using a github post-receive hook.
So… Mojolicious has this marvelous web server called hypnotoad. What makes this server so cool, is that it can do hot deployment1{#fnref:fn-hot_deployment .footnote}.This is indeed excellent feature for continuous deployment. In my projects I have also pure copywriters which wants to make changes to the templates, but has no idea how to ssh-into-ec2-then-restart-the-server-with-some-sudo-command. Even though we’re a small group, I wanted to give the opportunity to my partners in crime to restart the server by themselves instead of waiting for me. So therefor I decided to write Toadfarm and Toadfarm::Plugin::Reload.
The “Reload” plugin allow this workflow:
- The developer / copywriter edit files locally.
- Then they use github Mac to push the changes back to a special branch2{#fnref:fn-special_branch .footnote}.
- Github then POST a document to Toadfarm, using a post-receive hook.
- Toadfarm verifies that the request is from github.
- Toadfarm then reloads with the new source code if it is valid.
There might be a delay between pushing the code and a complete restart, but if everything goes OK, the new code should be up and running within 30 seconds.
What kind of configuration is required to make this work? Not much really:
- You need to set up the post-receive hook.
- Make sure the remote “origin” in production point to github.
- Configure Toadfarm.
Below you can see a sample config, which is essentially copy/paste from my server.
{
secret => 'some-super-secret-session-hash', # Mojolicious config
hypnotoad => { # Hypnotoad config
pid_file => '/var/run/toadfarm/pid', # |
listen => [ 'http://*:8080' ], # |
},
apps => [ # Toadfarm config
'/home/webapps/batware/script/batware' => { # |
Host => 'home.thorsen.pm', # |
},
],
plugins => [
Reload => { # Toadfarm::Plugin::Reload config
path => '/something-secret-5', # |
repositories => [{ # |
name => 'jhthorsen.github.com', # |
branch => 'master', # |
path => '/home/webapps/batware', # |
}],
},
],
}
CAVEAT:
- Hypnotoad (by it self) will not detect syntax errors in the templates on reload.
Want to try it out? It’s simple to install with cpanminus:
cpanm -n --sudo Toadfarm
See also examples on how to set it up.
Want to discuss or ask questions about Toadfarm? Comment below or contact batman @ irc.perl.org. (I’m usually in the #mojo channel)
::: footnotes