Mac下使用Hugo+Github Pages搭建我的博客(超詳細)

Hugo

Hugo 是由 Go 語言實現的靜態網站生成器。簡單、易用、高效、易擴展、快速部署。 ~~~~git

創建此博客受到jdhao的啓發.
根據本文章的步驟,你能夠訪問最終搭建完成的個人我的博客github

安裝

brew install hugo  

# 檢查安裝成功  
hugo version  

Hugo Static Site Generator v0.58.3/extended darwin/amd64 BuildDate: unknown

生成 site 目錄

hugo new site blog  
cd blog  
git init  

# 目錄結構  
tree blog

config.toml 是配置文件,在裏面能夠定義博客地址、構建配置、標題、導航欄等等。
themes 是主題目錄,能夠去 themes.gohugo.io 下載喜歡的主題。
content 是博客文章的目錄。npm

安裝主題

themes.gohugo.io 選擇喜歡的主題,下載到 themes 目錄中,而後在 config.toml 中配置 theme = "even" 便可。 segmentfault

even主題介紹
hugo-theme-even: hexo-theme-even的移植版本.api

下載

能夠直接 clone 到 themes 目錄下,優勢是若是對主題有調整需求能夠同時提交到 git 控制中。bash

git clone https://github.com/olOwOlo/hugo-theme-even themes/even

也能夠添加到 git 的 submodule 中,優勢是後面講到用 travis 自動部署時比較方便。若是須要對主題作更改,最好 fork 主題再作改動。服務器

git submodule add https://github.com/olOwOlo/hugo-theme-even.git themes/even

從新 build

若是須要調整更改主題,須要在 themes/even 目錄下從新 buildmarkdown

cd themes/even && npm i && npm start

第一篇文章

hugo new post/my-first-post.md

文章頂部能夠設置一些 meta 信息,例如:hexo

---  
title: "My First Post"  
date: 2017-12-14T11:18:15+08:00  
weight: 70  
markup: mmark  
draft: false  
keywords: ["hugo"]  
description: "第一篇文章"  
tags: ["hugo", "pages"]  
categories: ["pages"]  
author: ""  
---  
這裏是文章內容

配置項目

覆蓋config.toml

拷貝themes/even/exampleSite下的config.toml覆蓋根目錄下的config.tomldom

config.toml一些設置項目

logoTitle = "你的首頁標題名"  

# 歸檔、標籤、分類每頁顯示的文章數目,建議修改成一個較大的值  
archivePaginate = 5  

# 是否在歸檔頁顯示文章的總數  
showArchiveCount = true  

# 是否使用mathjax(數學公式)  
mathjax = true           # see https://www.mathjax.org/   
mathjaxEnableSingleDollar = true   # 是否使用 $...$ 便可進行inline latex渲染  
mathjaxEnableAutoNumber = false   # 是否使用公式自動編號  

# 連接到markdown原始文件(僅當容許hugo生成markdown文件時有效)  
linkToMarkDown = false    # Only effective when hugo will output .md files.  

# 啓用公共CDN,需自行定義  
[params.publicCDN]        # load these files from public cdn          

# 社交連接                
[params.social]

預覽

hugo server -D  
#...  
#Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)

hugo 怎麼插入本地 image

在site 的目錄下的 static 下就是存儲的靜態文件,咱們建立 media 目錄存放圖片等媒體文件,引用的話,直接「/media/xxx.png」 。

GitHub 配置

在github上新建一個項目,項目的名稱必須是(你的用戶名.github.io)

設置Git的user name和email

git config --global user.name "imymirror"  
git config --global user.email "2633610394@qq.com"

生成密鑰

ssh-keygen -t rsa -f ~/.ssh/id_rsa_blog  -C "2633610394@qq.com"

將ssh公鑰內容拷貝到Github

將ssh公鑰內容拷貝到Github->Setting->Deploy keys

cat ~/.ssh/id_rsa_blog.pub  

Your identification has been saved in id_rsa_blog.  
Your public key has been saved in id_rsa_blog.pub.

生成 Github Access Token

至少要有 public_repo 的權限。

配置 Travis1

Travis CI 註冊關聯 Github 的帳號,而後同步帳戶並激活 blog repo。

接着進入 blog 的設置頁面,選擇自動部署觸發條件,並把剛剛生成的 GitHub Access Token 添加到環境變量裏。

在 blog repo 中添加 .travis.yml

sudo: false  
language: go  
git:  
    depth: 1  
install: go get -v github.com/gohugoio/hugo  
script:  
    ‐ hugo  
deploy:  
    provider: pages  
    skip_cleanup: true  
    github_token: $GITHUB_TOKEN  
    on:  
        branch: master  
    local_dir: public  
    repo: <username>/<username>.github.io  
    fqdn: <custom-domain-if-needed>  
    target_branch: master  
    email: <github-email>  
    name: <github-username>

部分參數解釋:

  • 默認狀況下,travis 會自動下載 git submodules
  • github_token: $GITHUB_TOKEN 要和 travis 設置的環境變量名一致
  • fqdn: 若是須要設置自定義域名,能夠在這裏設置,travis 會自動生成 CNAME 文件提交,同時要設置 config.toml 中的相應的 baseURL

最後,能夠手動去 travis 觸發一次 build 檢查效果。若是設置了提交觸發 build,以後每次 blog repo 有提交都會自動 build,再也不須要關心 travis 狀態。

怎麼找到本身的Github ID

Where can I find the GitHub ID in my account?

https://api.github.com/users/your_github_user_name

部署到Github我的頁面2

在Github建立一個倉庫,例如名字叫blog,能夠是私有的,這個倉庫用來存放網站內容和源文件.

再建立一個名稱爲<username>.github.io的倉庫,username爲GitHub用戶名,這個倉庫用於存放最終發佈的網站內容

進入本地網站目錄

cd <YOUR PROJECT>

關聯遠程blog倉庫

git remote add origin git@github.com:zhigang26/blog.git

確保本地網站正常,hugo server運行後在本地打開localhost:1313檢查網站效果和內容,注意hugo server這個命令不會構建草稿,因此若是有草稿須要發佈,將文章中的draft設置爲false

關閉本地Hugo服務器Ctrl+C,而後刪除本地網站目錄下的public文件夾

rm -rf ./public

建立public子模塊,注意下面是一行命令,不是兩行

git submodule add -b master git@github.com:<USERNAME>/<USERNAME>.github.io.git public  

git submodule add -b master git@github.com:zhigang26/zhigang26.github.io.git public

而後就能夠執行hugo命令,此命令會自動將網站靜態內容生成到public文件夾,而後提交到遠程blog倉庫

hugo -t even  

cd public  
git status  
git add .  
git commit -m "first commit"  
git push -u origin master

自動部署腳本

將以上步驟添加到自動執行腳本中deploy.sh,腳本commit提交信息會使用執行時的時間,將腳本放到網站項目路徑下,寫完博客後,雙擊運行便可自動部署發佈

#!/bin/bash  

echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"  

# Build the project.  
hugo -t even # if using a theme, replace with `hugo -t <YOURTHEME>`  

# Go To Public folder  
cd public  
# Add changes to git.  
git add .  

# Commit changes.  
msg="rebuilding site `date`"  
if [ $# -eq 1 ]  
  then msg="$1"  
fi  
git commit -m "$msg"  

# Push source and build repos.  
git push origin master  

# Come Back up to the Project Root  
cd ..

Markdown文件之間如何交叉引用

好比在my-first-post.md引用使用Hugo搭建我的博客.md,引用方式能夠嘗試:
image

Nodes


  1. 使用 Hugo 搭建博客
  2. Hugo部署到Github
相關文章
相關標籤/搜索