背景

最近在着手搭建自己的个人博客,博客的好处想必大家都知道好处有很多,下面列举了几条仅供大家参考:

  • 沉淀所学知识,搭建知识体系。(这是最重要的)
  • 千里马常有,而伯乐也需要一个平台去认识你。
  • 结识志同道合之士
  • 于智者,抛砖引玉;于惑者,授业解惑

但是在搭建的过程中,遇到很多坑,特此写这篇文章记录一下,希望可以帮助到其他童鞋。

搭建个人博客方法有很多,最简单的方法,只需要fork一个现有的博客,并将工程名修改为{github-username}.github.io,然后替换自己的博文既可。虽然也可以达到效果,但是体验不到自己动手搭建的乐趣。

下面就介绍一下Hugo & GitHub Pages搭建的过程。

简介

Hugo

Hugo是由Go语言实现的静态网站生成器。简单、易用、高效、易扩展、快速部署。

hugo的安装流程请见中文官网英文官网,此处就做不详细介绍。这里更多的想要描述一下hugo的用法,及相关文件夹的功能,以及如何配置hugo

`GitHub` hugo new site `blogname` //blogname任君喜欢

Congratulations! Your new Hugo site is created in /Users/haiguanghuang/Documents/Github/blogname.

Just a few more steps and you're 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.

➜  `blogname` cd `blogname`				//进入主页文件夹
➜  `blogname` git init						//用git管理

目录结构如下

-`blogname`
	-archetypes  
		-default.md		//这是md模板,通过`hugo new xxxx.md`就会以这个模板创建
	-config.toml		//hugo的配置文件
	-content				//放主要的博客内容
	-data       		
	-layouts     		//布局相关,优先级会比theme里面的高
	-static     		//放静态资源,比如图片啥的
	-themes					//hugo的主题,更多主题见:https://themes.gohugo.io

创建完主页后,就可以下载你喜欢的主题了,下载主题方式多样化,最终只要把对应的主题放到themes文件下即可。

下面是通过git下载,这里有个小提醒,在主题官网看到喜欢的主题,进入该主题的主页后,点击Download,会跳转到对应GitHub地址,fork一份到自己的GitHub,这样以后可以根据自己的喜好微调。

`blogname` git:(master) git subtree add --prefix=themes/leaveIt git@github.com:huanghg/LeaveIt master		//用subtree的方式关联一个主题远程分支

主题下载好了,接下来就是配置了主题了, 可以编辑config.toml , 这一步不是必要的。

baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"
theme = "leaveIt"			//这个对应themes里面的主题文件夹名
publicDir = "public"	//最终生成的文件目录

更多的config配置见Configure Hugo,初期我用的并不多,要打理好个人博客,这个config还是要花时间去了解的。

接下来就可以创建自己的博文了,默认是创建到content文件下

`blogname` git:(master) ✗ hugo new posts/welcome.md		//用hugo的模板创建博文,这个模板就是archetypes下的default.md

创建成功后还需要编辑config.toml,配置目录结构,

baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"
theme = "leaveIt"			//这个对应themes里面的主题文件夹名

[[menu.main]]
  name = "首页"
  weight = 10
  identifier = "home"
  url = "/"
[[menu.main]]
  name = "博文"
  weight = 20
  identifier = "posts"
  url = "/posts/"

最后使用hugo命令,就可以生成静态网站了,且本地部署。执行完命令后,会生成一个新的文件public,并把生成的文件放到public文件下。

➜  blogname git:(master) ✗ hugo server -t leaveIt -D

                   | EN
+------------------+----+
  Pages            |  7
  Paginator pages  |  0
  Non-page files   |  0
  Static files     |  0
  Processed images |  0
  Aliases          |  0
  Sitemaps         |  1
  Cleaned          |  0

Total in 40 ms
Watching for changes in /Users/haiguanghuang/Documents/Github/blogname/{archetypes,content,data,layouts,static,themes}
Watching for config changes in /Users/haiguanghuang/Documents/Github/blogname/config.toml
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

此时在浏览器打开,就可以看到效果啦

可以看到左上角是My New Hugo Site对应的就是config.tomltitle, 可以根据个人喜好修改。

右上角就是config.toml里面的menu,大家可以根据自己需求定制化。其中首页就是访问https://localhost:1313/,而博文是访问http://localhost:1313/posts/

截此为止,静态网站已经生成好了,接下来就是托管到GitHub

GitHub Pages

在创建主页的时候,遇到一个坑,User pages must be built from the master branch.网上其他资料都没有刻意说明,花费了我很多时间,希望可以帮助到有需要的童鞋。

网上都会建议通过{github-username}.github.io创建个人主页, 通过这种方式创建的,默认只能built master分支, 是没有办法修改GitHub Pages Source,你可以理解为master下的根目录就是通过Hugo生成的静态文件目录,但是我们默认生成的是在public目录下,我这里的做法是Hugo主页的目录另起一个远程Git仓库,而public创建一个子仓库,链接的是{github-username}.github.io,这样日后访问的时候就可以直接访问https://{github-username}.github.io即可访问了。

网上会有介绍说可以选择gh-pages分支,但是这个方式就不能通过{github-username}.github.io创建,而是另起一个名字,如blog,这样访问的时候链接就不是https://{github-username}.github.io,而是https://{github-username}.github.io/blog。这种方式只需要建一个远程Git仓库,通过添加subtree的方式链接public文件夹。

GitHub创建一个主仓库``{blogname},一个子仓库{git-username}.github.io`.

`blogname` git:(master) ✗ git remote add origin https://{git-username}.github.io/{blogname} //关联远程主仓库地址
...

➜  `blogname` git:(master) git subtree add --prefix=public git@github.com:{git-username}/{git.username}.github.io master	//将public关联远程{git-username}.github.io子仓库
...

➜  `blogname` git:(master) hugo -t leaveIt		//以leaveIt为主题,生成静态网站
...

➜  `blogname` git:(master) ✗ git commit -m "Updating site" && git push origin master //生成静态网站后,master有修改,先commit修改,并push到远程主仓库
...

➜  `blogname` git:(master) git subtree push --prefix=public git@github.com:{git-username}/{git-username}.github.io master //将public的修改push到远程子仓库,也就是User Pages

最终在浏览器访问https://{git-username}.github.io.这里面可能会有些缓存,需要些时间才能刷新最新的改动。

发布

可以发现,上面的几个命令是比较常规的,我们可以把这些命令通过脚本的方式,如publish.sh, 后续只需执行这个脚本即可

# Run hugo. Generated site will be placed in public directory (or omit -t ThemeName if you're not using a theme)
hugo -t leaveIt

# Add everything
git add -A

# Commit and push to master
git commit -m "Updating site" && git push origin master

# Push the public subtree to the master branch
git subtree push --prefix=public git@github.com:{git-username}/{git-username}.github.io master

后续有任何改动后,首先先把改动commit到master分支,然后就可以通过bash publish.sh执行这个脚本了。