Technical recipes for frequently and infrequently recurring problems
For many of our projects it will be desirable to have the latest master code deployed to a server for QA review without the need for manual intervention. Here’s how to set that up.
In this case we’ll assume a server called cd.curationexperts.com
and a cap
deploy target of cd
. That implies that one should be able to deploy code via:
bundle exec cap cd deploy
We’ll use the laevigata
github repo as an example.
$ ssh-keygen -t rsa -b 4096 -C "administrator@curationexperts.com"
Enter file in which to save the key (/home/bess/.ssh/id_rsa): ~/.ssh/laevigata_deploy_rsa
Enter passphrase (empty for no passphrase): (leave empty)
Enter same passphrase again: (leave empty)
Append the output of cat ~/.ssh/laevigata_deploy_rsa.pub
to /home/deploy/.ssh/authorized_keys
on the server to which you want
travis to deploy.
Use travis provided tools to encrypt the private key.
gem install travis
travis login
and authenticate to github.travis encrypt-file YOUR_PRIVATE_KEY --add --repo=curationexperts/laevigata
.travis.yml
file, including a command to de-crypt the file. Make sure these get saved and committed.Add a block to .travis.yml
that will run a capistrano deploy:
after_success:
- |
if [[ $TRAVIS_BRANCH == 'master' && $TRAVIS_PULL_REQUEST == 'false' ]]; then
bundle exec cap prod_upgrade deploy
fi
Add this line to config/deploy.rb
:
set :ssh_options, keys: ["deploy_id_rsa"] if File.exist?("deploy_id_rsa")
Ideally, we’d use capistrano-passenger to restart passenger after every deploy. Unfortunately, this doesn’t work consistently. In order to ensure the deploy system is running the latest code, add a block like this to config/deploy.rb
and ensure the deploy user has sudo rights to restart apache2 (if you’re using ansible-samvera
build scripts, this should already be the case.)
# Capistrano passenger restart isn't working consistently,
# so restart apache2 after a successful deploy, to ensure
# changes are picked up.
namespace :deploy do
after :finishing, :restart_apache do
on roles(:app) do
execute :sudo, :systemctl, :restart, :apache2
end
end
end
You should have a changed .travis.yml
and config/deploy.rb
. You should also have a new, encrypted, rsa key. Make a PR with these changes and when it is merged to master it should automatically deploy.