I explain how I automated the deployment of an open source project with Travis CI.

I automated the deployment of an open source project I’m involved in (Litewrite) and would like to share how it works.

We can’t use a webhook directly because our tests should run first. Also our hosting solution (5apps) is not a server we own and we can’t pull the latest changes from Github.

Instead 5apps provides a Git origin where we need to push to.

Before, I used to push manually from my local machine, because I need to have an SSH key the server trusts.

The new solution is to let Travis CI push the changes for us.

Travis CI works great for automating things like tests and is even free to use for open source projects.

The configuration for Travis CI is really simple.

We need Travis CI to run our tests first and call a custom deploy script for the master branch if the tests are successful:

language: node_js # Node runs `npm install` and `npm test` automatically
node_js: node # Uses the latest version (via nvm)
sudo: false # Allows Travis to run this project in container
deploy:
  provider: script
  script: scripts/deploy.sh
  on:
    branch: master

To deploy we need an SSH key that’s added to 5apps, but the key shouldn’t be public.

We can hide the key by putting it in an environment variable on Travis CI. You just need to make sure to preserve the new lines while adding the key.

Now, in our deploy script we can retrieve the key and use it to push via Git:

eval "$(ssh-agent -s)" # Start the ssh agent
echo "$DEPLOY_KEY" > deploy_key.pem
chmod 600 deploy_key.pem # This key should have push access
ssh-add deploy_key.pem
git remote add deploy $REPO_URI
git push deploy master

With this set up, we don’t need to deploy manually ever again.

All we need to do, is to publish our latest changes on the master branch.