Skip to content

Instantly share code, notes, and snippets.

@waynegraham
Created October 16, 2015 13:27
Show Gist options
  • Save waynegraham/5c6ab006862123398d07 to your computer and use it in GitHub Desktop.
Save waynegraham/5c6ab006862123398d07 to your computer and use it in GitHub Desktop.
Capistrano deployment with Travis

Setup

On a development machine, install the Travis CLI gem and login. You'll then need to encrypt a password to decrypt the private key.

$ gem instal travis
$ travis login
$ travis encrypt DEPLOY_KEY="password for encryption" --add

This writes to an encrypted password to the .travis.yml file that you can use to encrypt the private key for deployment.

With the user that is doing the deployment, you need to encrypt the private key:

$ openssl aes-256-cbc -k "password for encryption" -in ~/.ssh/id_rsa -out deploy_id_rsa_enc_travis -a

Also make sure the public key is in the authorized keys on the server:

$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Pull the deploy_id_rsa_en_travis file and put it in the rails config directory.

Now add a script to the .travis.yml file that will decrypt the key.

# .travis.yml
after_success:
  - "openssl aes-256-cbc -k $DEPLOY_KEY -in config/deploy_id_rsa_enc_travis -d -a -out config/deploy_id_rsa"
  - "bundle exec cap deploy"

Update the Capistrano configuration to use the RSA key:

# deploy.rb
set :ssh_options, keys: ["config/deploy_id_rsa"] if File.exist?("config/deploy_id_rsa")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment