Continuous Deployment with Travis-CI and Github pages

Continuous Deployment with Travis-CI simplifies the process of automatically deploying applications after successful tests. By integrating Travis-CI with your project on GitHub, you can ensure that every commit in the master branch triggers an automatic production deployment once the tests pass.

What is Travis-CI?

Travis-CI is a cloud-based service that automates testing and deployment for applications. This paid service offers a free plan for open-source projects, making it an excellent choice for developers. While there are many alternatives, Travis-CI is widely used in the open-source community, making it a great example for our discussion.

Setting Up Travis-CI

Configuration File

Setting up Travis-CI requires a straightforward configuration file. Here’s a minimal example for running a Node.js project:

YAML
language: node_js

That’s it! Depending on the language and files in your project, Travis-CI automatically selects the default test command. For Node.js, it typically runs npm test if a package.json file is present. If a Makefile is included, it executes make test.

A Comprehensive Configuration

Here’s a more detailed example of a .travis.yml file:

YAML
language: node_js

node_js:
  - '5'
  - '4'

matrix:
  fast_finish: true

cache:
  directories:
    - node_modules

before_install:
  - npm prune

before_script:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start

script: npm run test-with-coverage

after_success: 'npm run coverage'

deploy:
  skip_cleanup: true
  provider: script
  script: ./scripts/deploy.sh
  on:
    branch: master
    node: '5'

env:
  global:
    - NODE_ENV=production

Real-World Example

Let’s consider a simple JavaScript project that will be deployed on GitHub Pages. To do this, you need a GitHub token that must be encrypted to keep it secure.

Generating an Encrypted GitHub Token on Travis-CI

The easiest way to add a token is via the GitHub interface.

Encrypting the Token

You have two options to encrypt your token:

  1. Using the Node Package travis-encrypt
    Install the package globally:
Bash
   npm i -g travis-encrypt
   travis-encrypt --add --repo {YOU/YOUR_REPO} GITHUB_TOKEN={YOUR_TOKEN}
  1. Using the Ruby Gem travis
    Install the gem:
Bash
   sudo gem install travis
   travis encrypt --add --repo {YOU/YOUR_REPO} GITHUB_TOKEN={YOUR_TOKEN}

Both methods will automatically add the encrypted token to your .travis.yml file under the env.global section.

YAML
env:
  global:
    - NODE_ENV=production
    - secure: vqhHD....ROxGPQo=
    - GIT_DEPLOY_REPO=https://[email protected]/YOU/YOUR_REPO.git

Automation Process

Suppose you have a project to deploy on GitHub Pages. The first step is to generate your project and then push the generated folder to your gh-pages branch.

Using the Deployment Task Only When Necessary

Travis-CI provides a deployment step that runs after successful tests, allowing you to manage deployment effectively.

Here’s how to set up the deployment configuration:

YAML
deploy:
  skip_cleanup: true
  provider: script
  script: ./scripts/deploy.sh
  on:
    branch: master
    node: '5'

Creating the deploy.sh Script

To deploy to the gh-pages branch, you can use the following script:

Bash
#!/usr/bin/env bash

GIT_DEPLOY_REPO=${GIT_DEPLOY_REPO:-$(node -e 'process.stdout.write(require("./package.json").repository)')}

cd dist && \
$(npm bin)/rimraf .git
git init && \
git config user.name "Travis CI" && \
git config user.email "[email protected]" && \
git add . && \
git commit -m "Deploy to GitHub Pages" && \
git push --force "${GIT_DEPLOY_REPO}" master:gh-pages

Conclusion

Once you push a commit to the master branch, Travis-CI will run your tests, generate your project if the tests pass, and deploy it automatically. This method ensures your project is always up-to-date without manual intervention.

And if you ever want to commit without triggering a Travis-CI build, simply include [ci skip] in your commit message. This is especially handy when making minor changes, like updating documentation.

With this setup, you can confidently deploy your application, knowing that Travis-CI will handle the rest, allowing you to focus on development!