Blog deploy to Github Page.md

2024-08-11 星期日 晴

准备

前置条件:
1、本地可以正常运行hexo
2、已经安装git
3、已经注册了Github账号

首先需要拿到Github的仓库域名,接着上传本地的公钥到Github

获取域名

在Github上新建一个仓库,仓库命名:eiSouthBoy.github.io,eiSouthBoy 是用户名,根据自己实际用户名修改,仓库的属性选择:Public,最后点击创建。

此时,在 eiSouthBoy.github.io 仓库页面,进入到仓库的Settings–>Pages,可以看到Github提供的Github Pages,给出了网址:Your site is live at https://eisouthboy.github.io/,可以在浏览器输入网址直接访问的。

上传公钥

将本地的博客上传到Github上,需要使用Github的写入权限,所以需要将本地用户的公钥上传到Github。

创建公钥前,需要配置git的用户名和邮箱

1
2
3
4
5
6
7
# 进入ubuntu22.04容器
docker exec -it -u cj 52e0bd6293ac /usr/bin/bash

cd /workspace/blog
git config --global user.name=eiSouthBoy
git config --global user.email=caoj97141@gmail.com

git用户名和邮箱添加后,此时可以生成公钥了

1
2
3
# 该命令有交互,默认全部回车即可
ssh-keygen -t rsa

该命令执行完毕后,提示公钥文件保存的路径

1
2
3
4
5
6
cj@52e0bd6293ac:/workspace/blog$ ll ~/.ssh/
total 28
drwx------ 2 cj cj 4096 Aug 11 03:53 ./
drwxr-x--- 1 cj cj 4096 Aug 11 07:57 ../
-rw------- 1 cj cj 2602 Aug 11 03:48 id_rsa
-rw-r--r-- 1 cj cj 569 Aug 11 03:48 id_rsa.pub

将公钥信息上传到Github,即将id_rsa.pub文件内容复制到Github的github个人图像 –> Settings –> SSH and GPG Keys –> New SSH Key,点击创建,就会创建SSH Key信息。该信息形式如下所示(为了不保留个人信息,SHA256隐藏了关键信息):

1
2
3
4
cj@52e0bd6293ac
SHA256:*******************************************
Added on Aug 11, 2024
Last used within the last week — Read/write

测试 SSH 是否能连接到 GitHub 的 SSH 服务器

1
ssh -T git@github.com

出现如下提示,即说明本地能够通过SSH连接到Github的SSH服务器

1
2
Hi eiSouthBoy! You've successfully authenticated, but GitHub does not provide shell access.

部署

通过git方式上传博客到Github,还需要下载一个插件

1
npm install hexo-deployer-git --save

在部署前,必须要修改 /workspace/blog/_config.yml 文件,修改的部分内容有:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 修改第一处
# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: https://eisouthboy.github.io

# 修改第二处
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: git@github.com:eiSouthBoy/eiSouthBoy.github.io.git
branch: main

现在可以部署到Github上了,过程如下:

1
2
3
cd /workspace/blog
hexo clean && hexo g
hexo d

记录 hexo d 命令的返回结果信息 (非首次提交)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cj@52e0bd6293ac:/workspace/blog$ hexo d
INFO Validating config
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
On branch master
nothing to commit, working tree clean
Enumerating objects: 74, done.
Counting objects: 100% (74/74), done.
Delta compression using up to 12 threads
Compressing objects: 100% (29/29), done.
Writing objects: 100% (43/43), 21.26 KiB | 3.54 MiB/s, done.
Total 43 (delta 19), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (19/19), completed with 14 local objects.
To github.com:eiSouthBoy/eiSouthBoy.github.io.git
bbb8d53..60a542a HEAD -> main
Branch 'master' set up to track remote branch 'main' from 'git@github.com:eiSouthBoy/eiSouthBoy.github.io.git'.
INFO Deploy done: git

部署成功后,可以在宿主机的浏览器输入:https://eisouthboy.github.io 访问博客。

疑问

在部署过程中,可能遇到一些报错或者疑点,希望记录下来。用于提醒自己,还可以提醒后来人。

疑点1

在执行 hexo d 命令时,有一句提醒:

1
Branch 'master' set up to track remote branch 'main' from 'git@github.com:eiSouthBoy/eiSouthBoy.github.io.git'.

本地分支 master 建立跟踪在远程分支main上。Github从2020年10月开始,创建仓库的默认分支名:main,至于原因可参考:为什么Git分支开始从“master”变为“main”了?

但是 git init 初始化一个本地仓库时,默认的分支是:master,这就是为什么本地master分支和远程main分支名称不一致的原因了。

当然,我们可以修改本地的master名称,过程如下:

1
2
3
4
5
6
7
8
cd /workspace/blog/.deploy_git

# 修改分支名
git branch -m master main

# 查看分支信息
git branch

这是本地分支名已经修改完成了,再次执行部署命令 ,验证一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cj@52e0bd6293ac:/workspace/blog$ hexo d
INFO Validating config
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
[main 6aa2596] Site updated: 2024-08-11 10:04:48
4 files changed, 28 insertions(+), 10 deletions(-)
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 3.32 KiB | 1.66 MiB/s, done.
Total 11 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To github.com:eiSouthBoy/eiSouthBoy.github.io.git
60a542a..6aa2596 HEAD -> main
Branch 'main' set up to track remote branch 'main' from 'git@github.com:eiSouthBoy/eiSouthBoy.github.io.git'.
INFO Deploy done: git


通过上面的信息,可以看到本地分支名和远程分支名一致了。