Skip to content

Instantly share code, notes, and snippets.

@chrislaughlin
Created March 31, 2018 16:20
Show Gist options
  • Save chrislaughlin/6fa93d86886b8f5de37d07d6f961ee9c to your computer and use it in GitHub Desktop.
Save chrislaughlin/6fa93d86886b8f5de37d07d6f961ee9c to your computer and use it in GitHub Desktop.
Using Travis CI with Gatsby

First set out your build scripts In your package.json scripts and install the gh-pages npm module

"build": "gatsby build",
"test": "echo \"no test specified\" && exit 0",
"preDeploy": "gatsby build --prefix-paths",
"deploy": "npm run preDeploy && gh-pages -d public"

Connect Travis CI to your repo and in the repo settings add an enviroment var call GITHUB_TOKEN with the value of your github token from: Setting > developer settings > Personal access tokens here create a token for Travis.

Then add the following file to your repo:

.travis.yml

language: node_js
node_js:
  - "stable"
cache:
  directories:
  - node_modules
deploy:
  provider: pages
  skip-cleanup: true
  github-token: $GITHUB_TOKEN
  local_dir: public
  keep-history: true
  on:
    branch: master
before_script:
  - "npm i -g gatsby"
  - "npm i"
script:
  - "npm run test"
after_success:
  - "npm run deploy"
@rodet
Copy link

rodet commented Jun 28, 2021

Thanks for that snippet, this is a great way to start! I have a few comments on the caching:

  • Since 2019, npm is cached on Travis by default, so you can remove the node_modules entry.
  • According to that documentation, it is useful to cache the Gatby directories .cache and public. I've tested this myself and this seems to bring faster builds reliably.

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