Hugoでブログ作成
所属しているコミュニティのLTで紹介されて、ずっと使ってみたかった静的サイトジェネレータHugoでブログサイトを立ち上げた。デプロイは昨年LINE LIFFの勉強でアカウントを取っていたNetlifyで行った。その手順をまとめておく。
参考にしたサイトはつぎのとおり。
1.PowerShellのインストール
上記のQuick Startの中で、Windows PowerShellではなくPowerShellを使うように指示されているので、まずPowerShellをインストールする。それにはWingetを使う。コマンドプロンプトでWingetを起動する。
Microsoft Windows [Version 10.0.19044.2604]
(c) Microsoft Corporation. All rights reserved.
C:\Users\fujis>winget
v1.4.10173 の Windows パッケージ マネージャー
Copyright (c) Microsoft Corporation. All rights reserved.
WinGet コマンド ライン ユーティリティを使用すると、コマンド ラインからアプリケーションやその他のパッケージをインストールできます。
使用法: winget [<コマンド>] [<オプション>]
使用できるコマンドは次のとおりです:
install 指定されたパッケージをインストール
show パッケージに関する情報を表示します
source パッケージのソースの管理
search アプリの基本情報を見つけて表示
list インストール済みパッケージを表示する
upgrade 利用可能なアップグレードの表示と実行
uninstall 指定されたパッケージをアンインストール
hash インストーラー ファイルをハッシュするヘルパー
validate マニフェスト ファイルを検証
settings 設定を開くか、管理者設定を設定する
features 試験的な機能の状態を表示
export インストールされているパッケージのリストをエクスポート
import ファイル中のすべてのパッケージをインストール
特定のコマンドの詳細については、そのコマンドにヘルプ引数を渡します。 [-?]
次のオプションを使用できます。
-v,--version ツールのバージョンを表示
--info ツールの一般情報を表示
-?,--help 選択したコマンドに関するヘルプを表示
--wait 終了する前に任意のキーを押すプロンプトをユーザーに表示します
--verbose,--verbose-logs WinGet の詳細ログを有効にする
--disable-interactivity 対話型プロンプトを無効にします
その他のヘルプについては、次を参照してください: https://aka.ms/winget-command-help
PowerShellのバージョンを確認する。
C:\Users\fujis>winget search Microsoft.PowerShell
'msstore' ソースでは、使用する前に次の契約を表示する必要があります。
Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction
ソースが正常に機能するには、現在のマシンの 2 文字の地理的リージョンをバックエンド サービスに送信する必要があります (例: "US")。
すべてのソース契約条件に同意しますか?
[Y] はい [N] いいえ: y
名前 ID バージョン ソース
----------------------------------------------------------
PowerShell Microsoft.PowerShell 7.3.3.0 winget
PowerShell Microsoft.PowerShell.Preview 7.4.1.0 winget
Microsoft.PowerShellをインストールする。
C:\Users\fujis>winget install --id Microsoft.Powershell --source winget
見つかりました PowerShell [Microsoft.PowerShell] バージョン 7.3.3.0
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
ダウンロード中 https://github.com/PowerShell/PowerShell/releases/download/v7.3.3/PowerShell-7.3.3-win-x64.msi
██████████████████████████████ 101 MB / 101 MB
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました
2.Hugoのインストール
PowerShell 7.3.3
PS C:\Users\fujis> cd documents
PS C:\Users\fujis\Documents> hugo new site hugo_blog
Congratulations! Your new Hugo site is created in C:\Users\fujis\Documents\hugo_blog.
Just a few more steps and you are ready to go:
1. Download a theme into the same-named folder.
Choose a theme from https://themes.gohugo.io/ or
create your own with the "hugo new theme <THEMENAME>" command.
2. Perhaps you want to add some content. You can add single files
with "hugo new <SECTIONNAME>\<FILENAME>.<FORMAT>".
3. Start the built-in live server via "hugo server".
Visit https://gohugo.io/ for quickstart guide and full documentation.
PS C:\Users\fujis\Documents> cd hugo_blog
PS C:\Users\fujis\Documents\hugo_blog> git init
Initialized empty Git repository in C:/Users/fujis/Documents/hugo_blog/.git/
PS C:\Users\fujis\Documents\hugo_blog> git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke themes/ananke
Cloning into 'C:/Users/fujis/Documents/hugo_blog/themes/ananke'...
remote: Enumerating objects: 2654, done.
remote: Counting objects: 100% (83/83), done.
remote: Compressing objects: 100% (48/48), done.
remote: Total 2654 (delta 35), reused 66 (delta 28), pack-reused 2571
Receiving objects: 100% (2654/2654), 4.51 MiB | 2.06 MiB/s, done.
Resolving deltas: 100% (1468/1468), done.
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory
PS C:\Users\fujis\Documents\hugo_blog> echo "theme = 'ananke'" >> config.toml
PS C:\Users\fujis\Documents\hugo_blog> hugo server
Start building sites …
hugo v0.110.0-e32a493b7826d02763c3b79623952e625402b168+extended windows/amd64 BuildDate=2023-01-17T12:16:09Z VendorInfo=gohugoio
| EN
-------------------+-----
Pages | 7
Paginator pages | 0
Non-page files | 0
Static files | 1
Processed images | 0
Aliases | 0
Sitemaps | 1
Cleaned | 0
Built in 113 ms
Watching for changes in C:\Users\fujis\Documents\hugo_blog\{archetypes,assets,content,data,layouts,static,themes}
Watching for config changes in C:\Users\fujis\Documents\hugo_blog\config.toml, C:\Users\fujis\Documents\hugo_blog\themes\ananke\config.yaml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
一旦立ち上がったローカルサーバを Ctrl+C を停止し、最初の投稿をする。
PS C:\Users\fujis\Documents\hugo_blog> hugo new posts/my-first-post.md
Content "C:\\Users\\fujis\\Documents\\hugo_blog\\content\\posts\\my-first-post.md" created
PS C:\Users\fujis\Documents\hugo_blog> hugo server --buildDrafts
Start building sites …
hugo v0.110.0-e32a493b7826d02763c3b79623952e625402b168+extended windows/amd64 BuildDate=2023-01-17T12:16:09Z VendorInfo=gohugoio
| EN
-------------------+-----
Pages | 10
Paginator pages | 0
Non-page files | 0
Static files | 1
Processed images | 0
Aliases | 1
Sitemaps | 1
Cleaned | 0
Built in 86 ms
Watching for changes in C:\Users\fujis\Documents\hugo_blog\{archetypes,assets,content,data,layouts,static,themes}
Watching for config changes in C:\Users\fujis\Documents\hugo_blog\config.toml, C:\Users\fujis\Documents\hugo_blog\themes\ananke\config.yaml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
再度ローカルサーバが立ち上がるのでChromeブラウザで http://localhost:1313 にアクセスすると作成したブログを確認できる。
3.Githubにリモートリポジトリを作る
コマンドhugo new site hugo_blogを実行する(hugo_blogフォルダができる)前にリモートリポジトリを作っておかなければ以下のようなエラーが起きる。
PS C:\Users\fujis\Documents\hugo_blog> git add .
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in archetypes/default.md.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in config.toml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in content/posts/my-first-post.md.
The file will have its original line endings in your working directory
PS C:\Users\fujis\Documents\hugo_blog> git commit -m "first commit"
[main (root-commit) 413094e] first commit
8 files changed, 29 insertions(+)
create mode 100644 .gitmodules
create mode 100644 .hugo_build.lock
create mode 100644 archetypes/default.md
create mode 100644 config.toml
create mode 100644 content/posts/my-first-post.md
create mode 100644 resources/_gen/assets/css/ananke/css/main.css_83735de7ca999e9c17f3419b41b93fdb.content
create mode 100644 resources/_gen/assets/css/ananke/css/main.css_83735de7ca999e9c17f3419b41b93fdb.json
create mode 160000 themes/ananke
PS C:\Users\fujis\Documents\hugo_blog> git branch -M main
PS C:\Users\fujis\Documents\hugo_blog> git remote add origin https://github.com/k-fujishiro/hugo_blog.git
PS C:\Users\fujis\Documents\hugo_blog> git push -u origin main
To https://github.com/k-fujishiro/hugo_blog.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/k-fujishiro/hugo_blog.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
hintにしたがってgit pullを実行。
PS C:\Users\fujis\Documents\hugo_blog> git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 617 bytes | 36.00 KiB/s, done.
From https://github.com/k-fujishiro/hugo_blog
* [new branch] main -> origin/main
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> main
git pullができていないようなので下記のコマンドでもう一度実施する。
PS C:\Users\fujis\Documents\hugo_blog> git pull https://github.com/k-fujishiro/hugo_blog.git main
From https://github.com/k-fujishiro/hugo_blog
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
PS C:\Users\fujis\Documents\hugo_blog> git pull https://github.com/k-fujishiro/hugo_blog.git main
From https://github.com/k-fujishiro/hugo_blog
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
git pushがrejectされたときの対応方法を参考にしてfetchとrebaseを実行。
PS C:\Users\fujis\Documents\hugo_blog> git fetch
PS C:\Users\fujis\Documents\hugo_blog> git rebase origin/main
Successfully rebased and updated refs/heads/main.
あらためてaddからpushまでを実行。
PS C:\Users\fujis\Documents\hugo_blog> git add .
PS C:\Users\fujis\Documents\hugo_blog> git commit -m "second commit"
[main 09e4cb5] second commit
1 file changed, 2 deletions(-)
PS C:\Users\fujis\Documents\hugo_blog> git branch -M main
PS C:\Users\fujis\Documents\hugo_blog> git remote add origin https://github.com/k-fujishiro/hugo_blog.git
error: remote origin already exists.
PS C:\Users\fujis\Documents\hugo_blog> git push -u origin main
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (24/24), 15.67 KiB | 5.22 MiB/s, done.
Total 24 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/k-fujishiro/hugo_blog.git
01c391f..09e4cb5 main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
今度は成功した。pubulish(発行:デプロイではない)をするためにhugoコマンドを実行。
PS C:\Users\fujis\Documents\hugo_blog> hugo
Start building sites …
hugo v0.110.0-e32a493b7826d02763c3b79623952e625402b168+extended windows/amd64 BuildDate=2023-01-17T12:16:09Z VendorInfo=gohugoio
| JA
-------------------+-----
Pages | 10
Paginator pages | 0
Non-page files | 0
Static files | 1
Processed images | 0
Aliases | 1
Sitemaps | 1
Cleaned | 0
Total in 184 ms
Github にpushする。
PS C:\Users\fujis\Documents\hugo_blog> git add .
...
...
PS C:\Users\fujis\Documents\hugo_blog> git commit -m "third commit"
...
...
PS C:\Users\fujis\Documents\hugo_blog> git branch -M main
PS C:\Users\fujis\Documents\hugo_blog> git push -u origin main
Enumerating objects: 34, done.
Counting objects: 100% (34/34), done.
Delta compression using up to 8 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (30/30), 314.83 KiB | 18.52 MiB/s, done.
Total 30 (delta 10), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (10/10), completed with 2 local objects.
To https://github.com/k-fujishiro/hugo_blog.git
09e4cb5..285c1fc main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
これでデプロイの準備ができた。
4.Netlifyでデプロイ
基本的にはHugo公式サイトのこちらにしたがって進めた。