DEV Community

Dave Aitken
Dave Aitken

Posted on

Deploying to Github Pages with Travis

This is mostly for my benefit as I always have to look it up - this will push content that's built by your Travis build into the gh-pages branch of the same repo. It's the simplest way of deploying to Github Pages I've found - it handles switching branch and details like pushing content from a dist subfolder for you automatically.

  • Install the gh-pages CLI tool using NPM:

    npm i --save-dev gh-pages
    
  • Grab a Github access token so Travis can push back up to your repo's gh-pages branch:

  • Encrypt the token with Travis so you can use it in your builds (--add automatically adds the encrypted data to your travis.yml):

    travis encrypt --add -r <org/repo> GITHUB_TOKEN=<token>
    
    • If you don't have travis available on your machine, use gem install travis.
  • Finally, Travis needs to know how to use the token when pushing. As a step in your .travis.yml:

    script:
    ...
    - git remote set-url origin https://<token's username>:$GITHUB_TOKEN@github.com/<org/repo>.git
    - gh-pages -d dist
    
    • Add any other args for gh-pages as necessary - the above will put all the content in the dist folder into the root of the gh-pages branch.

...and we're done! Let me know if you found this useful, it'll make my day.

Top comments (2)

Collapse
 
epsi profile image
E.R. Nurwijayadi

I also write a guidance for SSG for use with Travis.

epsi-rns.gitlab.io/devops/2020/02/...

Travis Running SSG

Collapse
 
epsi profile image
E.R. Nurwijayadi

Is this localhost Travis?