GitHub博客的搭建

2023年的博客从hexo切换到hugo

迁移过程

搭建hugo新环境

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

构建github ciworkflows

 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 }}

2017年的博客建设

本地搭建hexo环境

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

  • 参考 hexo 在本地安装,我用的是debian系统。 在中国境内经常会遇到墙的问题,建议使用 淘宝的cnpm
1
$ npm install -g cnpm --registry=https://registry.npm.taobao.org

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

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。
http://*.mickeyzzc.tech/*

使用Docker构建Blog静态文件

  • 利用DOCKERFILE构建一个本地使用的hexo镜像.
 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
1
2
3
docker run --rm -p 4000:4000 \
        -v $HOME/migit/miBlog:/home/hexo/source\
        -it mickeyzzc/node-hexo

最后打包合并到GIT库管理

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