GatsbyJSでgithub user pagesにデプロイするまでの手順

github.com

GatsbyJSgithub user pagesにデプロイするまでの手順のメモ。

gatsbyのバージョンは、1.9.149。
gh-pagesのバージョンは、1.1.0。

install

cliをシステムにインストール。

❯ npm i -g gatsby-cli
❯ gatsby -h
Usage: ../../../.nodebrew/current/bin/gatsby <command> [options]

コマンド:
  develop                   Start development server. Watches files, rebuilds, and hot reloads if
                            something changes
  build                     Build a Gatsby project.
  serve                     Serve previously built Gatsby site.
  new [rootPath] [starter]  Create new Gatsby project.

オプション:
  -h, --help     ヘルプを表示                                                                     [真偽]
  --verbose      Turn on verbose output                                       [真偽] [デフォルト: false]
  -v, --version  バージョンを表示                                                                 [真偽]

gatsby new

gatsby newコマンドで必要なファイル群を生成する。

❯ gatsby new username.github.io

デフォルトだとgatsby-starter-defaultがstarterとして使われる。他のstarterを使用したい場合はオプションで指定する。

❯ gatsby new username.github.io [starter]

www.gatsbyjs.org

gatsby develop

開発用のサーバはgatsby developで起動する。yarn run developでもgatsby developと同じように開発用のサーバが起動する。変更はlivereloadで反映される。

cd username.github.io
❯ gatsby develop

deploy

www.gatsbyjs.org

github pagesにデプロイするためにgh-pagesを追加する。

❯ yarn add --dev gh-pages

package.jsonのscriptsにdeployを追加。user pageなので、masterブランチにpushする。

"deploy": "gatsby build && gh-pages -d public -b master"

あとはdeployを実行するとgithub pagesにデプロイされる。

❯ yarn run deploy

> username.github.io@1.0.0 deploy /path/to/username.github.io
> gatsby build && gh-pages -d public -b master

success delete html files from previous builds — 0.024 s
success open and validate gatsby-config.js — 0.007 s
success copy gatsby files — 0.055 s
success onPreBootstrap — 0.015 s
success source and transform nodes — 0.028 s
success building schema — 0.158 s
success createLayouts — 0.040 s
success createPages — 0.003 s
success createPagesStatefully — 0.026 s
success onPreExtractQueries — 0.001 s
success update schema — 0.102 s
success extract queries from components — 0.014 s
success run graphql queries — 0.017 s
success write out page data — 0.011 s
success write out redirect data — 0.002 s
success onPostBootstrap — 0.002 s

info bootstrap finished - 2.114 s

success Building CSS — 4.312 s
success Building production JavaScript bundles — 9.291 s
success Building static HTML for pages — 3.285 s
info Done building in 19.009 sec
Published