Skip to content

Instantly share code, notes, and snippets.

@alfg
Created January 19, 2019 05:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfg/06783f30e50a71190f2110b9a411d384 to your computer and use it in GitHub Desktop.
Save alfg/06783f30e50a71190f2110b9a411d384 to your computer and use it in GitHub Desktop.
Travis Git deployments to Dokku
language: bash
before_install:
- echo $super_secret_password | gpg --passphrase-fd 0 .travis/deploy.key.gpg
- eval "$(ssh-agent -s)"
- chmod 600 .travis/deploy.key
- ssh-add .travis/deploy.key
- ssh-keyscan git.host.com >> ~/.ssh/known_hosts
after_success:
- git remote add deploy dokku@git.host.com:app
- git config --global push.default simple
- git push deploy master
env:
global:
secure: zUb0YAn1E4aJfhq2tJLjUpU5lmAAmFWV0BblHz620/SiFhtXww8EKHS2LCZu6cLwg6WKkpgwfbHZf/IjN8LGihW3glJruxdMtC7sLO++576p1R0sa3Ea24sDfRlkZiy+TOo0W4pS4uuU8XldgRvR50dngp7IKt7I9CPAD9k/P86rcmU51AqiiwAVT8YlmROR8iCeeGWMZBCzaIh4i+Hxbcv0YbQbvq1tA89pqaiT3UDdTxidRn2JMHld26eVolbPBDz7zZDaHYNXXA5d4BNlOtFVKjqXt2usHt+tMryJGtfs3eGF/rNKzIogqIyt7JqaoWgeK1LhPSwDP3L5MVMrpcncOPKFU1YXeZd++kHOXIEBIkPETscQWrBymsSIn3+7Cy3pJlXbVWzhtPmRKnrojkHwsyU48ZvzCzYQhhHHiItK/lRhgZNmO5RZJlo+1OF4R+B//RW84RBY7ItQfJO9n95bg4RD1HXMpIqHlqkssINZ4Pc71Ps1UguicXvsKx/M4TwSgAA8boaHlvKeriQSHCNs0pB5+5YD/r3x+sHGFIKstJ8cVd9+mqc7u5HLeNv9UU7KIbI0pospNCQazHd4csy6imY++Rx6aS+RHx6/nNXDbsCTKx3niX9fyHZ4TOSNUkOVBD69aguAnTGCQwY5ewmyaESsV8Jf7QiZxacWZGM=

How to deploy to Dokku using SSH keys, GPG and Travis CI.

I created this micro-guide because I spent a few hours figuring how to automate my deployments to Dokku via Travis CI and hopefully makes it easier for anyone who comes across this. I will also use this as my own reference for future project deployments.

Setup

  • Create ssh keypairs ssh-keygen -t rsa -b 4096 -f deploy.key

  • Copy deploy.key.pub to .authorized_keys on server vim /home/dokku/.ssh/authorized_keys

  • Encrypt deploy.key via gpg. It will ask to enter passphrase twice. gpg -c deploy.key

  • Store your private and public keys in .travis/, but make sure to add deploy.key and deploy.key.pub to .gitignore. You only want to commit your encrypted deploy.key.gpg file to git.

  • You can test decryption via gpg echo "test" | gpg --passphrase-fd 0 deploy.key.gpg

  • Using travis CLI, create the secure key for .travis.yml. This will automatically add the env.global.secure key with a value. This is how travis knows to set the super_secret_password envrionment variable. travis encrypt super_secret_password=supersecret --add

  • See .travis.yml as a reference. The SSH setup steps are important to prepare the CI instance for pushing to git.

Notes

  • I went with GPG instead of OpenSSL, because there were some inconsistencies across various versions that ended up leading to decryption errors in Travis CI, especially if using Windows. GPG worked right out of the box.
  • Although this is used for deploying to Dokku, it can really be used to deploy anywhere that uses git/ssh deployments.
  • Travis CLI has a ton of issues running on Windows, especially for generating encrypted files. I suggest running on OSX/Linux or Docker.

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment