GitHub博客的搭建

2023年的博客从hexo切换到hugo

主要是受够了hexo每次生成都要等半天,还有各种npm包的依赖管理问题,维护起来确实是个负担。而且hugo作为Go写的静态站点生成器,速度确实快多了。

迁移过程中折腾了不少,主要是front matter格式要从YAML改成TOML,主题的各种配置差异也挺大的,不过最终还是折腾过来了。

迁移过程

搭建hugo新环境

shell
1
2
3
4
5
6
7
hugo new site mickeyzzcblog
cd mickeyzzcblog
git init
git submodule add https://github.com/hugo-next/hugo-theme-next.git themes/hugo-theme-next
cp themes/hugo-theme-next/exampleSite/config.yaml .
vim config.yaml
hugo server

这个CI workflow主要就是自动拉取代码、安装hugo、生成静态文件,然后自动推送到GitHub Pages上,算是彻底解放了手动部署的过程。

构建github ciworkflows

mermaid
flowchart LR
    A[push to main] --> B[Checkout + Submodules]
    B --> C[Setup Hugo]
    C --> D[hugo build]
    D --> E[Deploy to GitHub Pages]
yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
name: deploy

on:
    push:
    workflow_dispatch:
    schedule:
        # Runs everyday at 8:00 AM
        - cron: "0 0 * * *"

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"
                  extended: true

            - name: Build Web
              run: hugo

            - name: Deploy Web
              uses: peaceiris/actions-gh-pages@v3
              with:
                  PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
                  EXTERNAL_REPOSITORY: mickeyzzc/mickeyzzc.github.io
                  PUBLISH_BRANCH: main
                  PUBLISH_DIR: ./public
                  commit_message: ${{ github.event.head_commit.message }}

说起来也挺有意思,6年时间博客工具从hexo换到hugo,从JavaScript生态到Go生态,感觉整个技术栈都换了一遍。

2017年的博客建设

那时候选hexo主要是看中Node.js的生态比较活跃,而且中文社区也挺多资源的,毕竟是第一次搞个人博客,选个熟悉的语言生态比较稳妥。

本地搭建hexo环境

详细步骤请参考官方网站,这里只提及过程中的注意点

  • 参考hexo在本地安装,我用的是debian系统。
  • 在中国境内经常会遇到墙的问题,建议使用淘宝的cnpm。当时因为网络问题,npm经常装不上,用cnpm确实省了不少事。
shell
1
$ npm install -g cnpm --registry=https://registry.npm.taobao.org

然后使用cnpm命令安装hexo-cli

shell
1
$ cnpm install -g hexo-cli
  • 个性化的自己的配置,并且在GitHub上建立xxx.github.io库。
  • hexo的本地目录source下初始化git库,并在自己的git库上管理。

配置CDN和HTTPS

  • hexo的本地目录public下创建CNAME文件,内容为你的域名。
  • 注册CloudFlare, 把域名的NS切换到CloudFlare管理。
  • CloudFlare的Crypto页中, SSL设置为Flexible。这将允许CDN到github pages之间的访问为http。
  • CloudFlare提供Page Rules功能, 可设置路由规则。通过规则中的Always use https选项,可以将用户强制跳转到https。
1
http://*.mickeyzzc.tech/*

当时选CloudFlare主要是免费,而且国内访问速度也还行,对于个人博客来说够用了。

使用Docker构建Blog静态文件

  • 利用DOCKERFILE构建一个本地使用的hexo镜像.主要是为了跨机器部署的一致性考虑,避免不同环境的Node版本差异导致的问题。
dockerfile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# MAINTAINER MickeyZZC <[email protected]>
# DOCKER-VERSION    1.13.0

FROM node:6
MAINTAINER MickeyZZC <[email protected]>
RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && npm install -g cnpm --registry=https://registry.npm.taobao.org \
    && cnpm install -g hexo-cli \
    && mkdir -p /home/hexo/public \
    && cd /home/hexo \
    && hexo init \
    && git clone https://github.com/iissnan/hexo-theme-next themes/next \
    && cnpm install hexo-deployer-git --save \
    && git clone https://git.oschina.net/MickeyZZC/MiZDoc.git mickeyblog \
    && cp -f mickeyblog/hexo_config/_config.yml _config.yml \
    && cp -f mickeyblog/hexo_config/themes_next_config.yml themes/next/_config.yml \
    && cp -f mickeyblog/hexo_config/gitconfig.cfg ./.gitconfig \
    && cp -rf mickeyblog/hexo_source/* source/ \
    && rm -rf mickeyblog source/_posts/hello-world.md \
    && chown -R node.node /home/hexo && chown -R node /usr/local/lib/node_modules/ \
    && echo "blog.mickeyzzc.tech" > /home/hexo/public/CNAME \
    && hexo generate \
    && chown -R node.node /home/hexo

ENV HOME /home/hexo
WORKDIR /home/hexo
EXPOSE 4000
USER node
CMD ["hexo","server"]
  • 构建后本地运行来调试hexo
shell
1
2
3
docker run --rm -p 4000:4000 \
        -v $HOME/migit/miBlog:/home/hexo/source\
        -it mickeyzzc/node-hexo

最后打包合并到GIT库管理

当时的工作流就是在本地写完文章,生成静态文件,然后推送到GitHub Pages库,就这么简单。

shell
1
2
3
# git add -A
# git commit -a -s -m "xxxx"
# git push