Skip to content

Instantly share code, notes, and snippets.

@mleszcz
Forked from nickbclifford/auto-deploying.md
Created June 4, 2018 14:15
Show Gist options
  • Select an option

  • Save mleszcz/fdaf4fe8c44c3cf9d4cfd9b4b1be9fdb to your computer and use it in GitHub Desktop.

Select an option

Save mleszcz/fdaf4fe8c44c3cf9d4cfd9b4b1be9fdb to your computer and use it in GitHub Desktop.
How to automatically deploy code to a server using Travis CI

Auto-Deploying via Travis CI

Because Travis CI can automatically execute scripts after successfully (or unsuccessfully!) executing tests, it is an obvious choice for a deployment tool. In order to deploy to a Git repository on a remote server, the process generally is as follows:

  • Set up SSH keys
  • Add the server's copy of the repository as a Git remote
  • Push to the remote
  • SSH into the server and execute any installation/compilation/miscellaneous commands

Before even touching .travis.yml...

SSH keypair

Make sure to generate an SSH keypair for passwordless login! This can be done on from the commandline via ssh-keygen -t rsa (provided OpenSSH is installed, which it normally is for Linux and other emulation layers like Git Bash) or through GUI tools like PuTTYgen. When prompted for a passphrase, make sure to leave it blank so that Travis can automatically login. Make note of where the keys are generated.

Next you'll need to encrypt the private key via the Travis CLI. Note that a Linux/OSX environment is necessary for this function to work properly, so some sort of emulation layer is necessary for Windows. Either way, install the Travis CLI. Once you've done that, run travis encrypt-file id_rsa (your filename may differ). I would recommend running this command inside your repository and using the --add flag to automatically add the correct commands to .travis.yml. Check this id_rsa.enc file into Git, but do not add the unencrypted id_rsa file.

Remote Git repository

Assuming you already have your Git repository cloned onto the remote server, the next step is to configure it to allow pushes. Run git config --local receive.denyCurrentBranch updateInstead to allow Git to accept pushes to a remote with a clean working tree.

Git recommends using a separate git user for remote interactions. However, your repositories might be under a separate user (apps, for example), so you'll need to add both those users to a group (deploy, for example). Once you've done that, make sure that the user and the group can access and modify the repository folder's contents (chown apps:deploy -R my-repo; chmod g+rw -R my-repo). You are now ready to start configuring Travis!

Configuring Travis

TODO

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