KAROUSHI -Japanese Engineer Blog-

コボルドからドラゴンへ -Kobold to Dragon-

システムエンジニアのブログです。サイト名は「雑魚キャラからボスキャラへレベルアップしたい!」という思いを込めて命名しました。自分はやっとリザードマンになったくらいです。

WindowsでHexo+GitHub Pagesでデプロイまで実行できなかった話

ブログ向けの静的ジェネレータを探していたらHexoがよさそうだったのでまずはインストールして使ってみることにしました。
Hexoはnode.js製なのでnpmが必要なのでそこからのWindows版インストール手順をまとめてみました。
が、結論から言うとHexoでサイト作成はうまくいきましたが、GitHub Pagesへデプロイはできませんでした。
Windows環境でうまく言った方アドバイスをいただけると助かります。

目次

構築環境

OS: Windows10
node: v8.11.3

node.jsのダウンロードとインストール

node.jsの公式サイトからWindows64bit版のnode.jsをダウンロードしてください。
https://nodejs.org/ja/

ダウンロードしたexeをクリックしてインストールをします。インストールが完了したらコマンドプロンプトを起動して以下コマンドを実行してください。

> node -v
v8.11.3

>npm -v
5.6.0

hexoをインストール

npmでhexoをインストールします。

> npm install -g hexo

途中で以下のようなWarnが出ますが特に問題ない模様。

npm WARN deprecated titlecase@1.1.2: no longer maintained
・・・省略・・・
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\hexo\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

インストールが完了したら以下コマンドを実行してください。

> hexo -v
hexo-cli: 1.1.0
os: Windows_NT 10.0.17134 win32 x64
http_parser: 2.8.0
node: 8.11.3
v8: 6.2.414.54
uv: 1.19.1
zlib: 1.2.11
ares: 1.10.1-DEV
modules: 57
nghttp2: 1.32.0
napi: 3
openssl: 1.0.2o
icu: 60.1
unicode: 10.0
cldr: 32.0
tz: 2017c

サンプルブログを作ってみる

以下コマンドを実行してmyblogというブログのひな型を作成します。
ちなみに実行したディレクトリにmyblogディレクトリを作成するので、実行前に任意の場所にcdで移動しておいてください。

>hexo init myblog
INFO  Cloning hexo-starter to ~\Desktop\サイト\hexo\myblog
Cloning into 'myblog'...
remote: Counting objects: 65, done.
remote: Total 65 (delta 0), reused 0 (delta 0), pack-reused 65
Unpacking objects: 100% (65/65), done.
Checking connectivity... done.
Submodule 'themes/landscape' (https://github.com/hexojs/hexo-theme-landscape.git) registered for path 'themes/landscape'
Cloning into '/myblog/themes/landscape'...
Submodule path 'themes/landscape': checked out '73a23c51f8487cfcd7c6deec96ccc7543960d350'
[32mINFO [39m Install dependencies
npm WARN deprecated titlecase@1.1.2: no longer maintained

> nunjucks@3.1.3 postinstall myblog\node_modules\nunjucks
> node postinstall-build.js src

npm WARN rollback Rolling back node-pre-gyp@0.10.0 failed (this is probably harmless): EPERM: operation not permitted, scandir 'myblog\node_modules\fsevents\node_modules'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 400 packages in 18.869s
INFO  Start blogging with Hexo!

すると以下のような構成のディレクトリが作成されます。

myblog
├node_modules
├scaffolds
├source
├themes
├.gitgnore
├_config.yml
├package.json
└package-lock.json

サーバを起動する。

>hexo server
INFO  Start processing
INFO  Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

ブラウザを開いて以下にアクセスをしてみてください。
http://localhost:4000/

このような画面が表示されればOKです。

Github pagesの設定

Githubにログインし、タブメニュの[Settings]を開き"GitHub Pages"の以下を設定して[Save]を押す。
Source: master branch

すると"Your site is ready to be published at "の後ろに公開されるページのURLが記載されているのでコピーしておきます。

パッケージのインストール

Hexoはバージョン3からデプロイ機能が本体から切り離されたので以下パッケージをインストールします。

npm install hexo-deployer-git --save

configにgithubを設定

hexoの_config.ymlを以下のように編集します。
repoは自分のGitリポジトリによって適宜修正すること。

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.com
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:

・・・省略・・・

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: 

↓

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://【アカウント名】.github.io/【リポジトリ名】/
root: 【リポジトリ名】
permalink: :year/:month/:day/:title/
permalink_defaults:

・・・省略・・・

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: github
  repo: 【Git リポジトリURL】
  branch: master

github pagesにdeploy

コマンドプロンプトを開いて、myblogのディレクトリまで移動し以下コマンドを実行してdeployします。

hexo deploy -g

※ココでエラーになる。

トラブルシューティング

deployコマンド実行時に/dev/ttyにデバイスとアドレスが見つからない

【手順】
以下をコマンドを実行する
>hexo deploy -g

【ログ】
標準出力で以下が表示される。

Fatal: HttpRequestException encountered.
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: Fatal: HttpRequestException encountered.
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument

    at ChildProcess.<anonymous> (myblog\node_modules\hexo-util\lib\spawn.js:37:17)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at ChildProcess.cp.emit (hexo\myblog\node_modules\cross-spawn\lib\enoent.js:40:29)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)

【原因】
Windows環境のため/dev/ttyなんてディレクトリは存在しない。

【対処方法】
repoに"http"ではなく"ssh"接続のURLを設定したが解消されず。

ホストキーの認証に失敗

【手順】
以下をコマンドを実行する

>hexo deploy -g

【ログ】
標準出力で以下が表示される。

nothing to commit, working directory clean
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

    at ChildProcess.<anonymous> (myblog\node_modules\hexo-util\lib\spawn.js:37:17)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at ChildProcess.cp.emit (myblog\node_modules\cross-spawn\lib\enoent.js:40:29)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:557:12)

【原因】
Gitに登録されている秘密鍵の設定がおかしいため認証に失敗している。

【対処方法】
不明。
Git Bashの場合、Windowsユーザの配下にある.sshディレクトリに公開鍵と秘密鍵を設定してみましたが、改善されず。