This post shows how to automate building and deployment of Hugo static websites to GitHub Pages using Travis CI. Builds are automatically triggered when pushing to the Git repository, and deployment when a build on the master branch succeeds.

Logos: Hugo, Travis, GitHub

Hugo is a static website generator written in Go, and only requires a single binary. The easiest way to run Hugo in the Travis CI container is by including the specific hugo binary with which to build the site as part of the repository (eg. in the directory /binaries). This way you don’t need to download the Hugo release on each build job, which may take several minutes. You can get the latest binary from the Hugo releases page (use the hugo_<VERSION>_Linux-64bit.tar.gz file and include only the hugo binary).

For syntax highlighting, you also need Pygments installed. In the .travis.yml file this is done by specifiying the python-pygments package as apt addon.

Configuring Travis CI

On the Travis CI homepage on your profile page you need to enable the GitHub repository you want to build.

Travis CI also needs write-access to the GitHub repository, to be able to update the gh-pages branch. For this we provide a GitHub token environment variable named GITHUB_TOKEN in this example. Environment variables can be specified on the Travis CI website in the repository settings (see the Travis environment variables docs). You can generate this token in your GitHub account settings under “Personal access tokens -> Generate new token” (ensure that the “repo” checkbox is enabled).


For a content layout like here (Hugo content in /src/, theme name ghoust, Hugo binary in /binaries/hugo), this is the complete .travis.yml:

# Install the apt prerequisites
addons:
  apt:
    packages:
      - python-pygments

# Clean and don't fail
install:
  - rm -rf public || exit 0

# Build the website
script:
  - cd src
  - ../binaries/hugo --theme=ghoust

# Deploy to GitHub pages
deploy:
  provider: pages
  skip_cleanup: true
  local_dir: public
  github_token: $GITHUB_TOKEN # Set in travis-ci.org dashboard
  on:
    branch: master

References:


Update 2017-04-26: Several people suggested to use Netlify for hosting the pages instead of GitHub Pages. Netlify has great continuous deployment integration, free SSL certificates, custom redirects, form handling and more.


Let me know if you have any feedback and/or ideas via @metachris!