使用Hugo+Git部署站点到GitHub上

1.锅内机器换源,这里提供一个一键换源脚本。

bash <(curl -sSL https://linuxmirrors.cn/main.sh)

2.更新软件源

sudo apt update

3.安装git。

sudo apt install git -y

4.从github上安装hugo(这里装的是扩展版)(以最新版本为准,这里仅提供参考)

wget https://github.com/gohugoio/hugo/releases/download/v0.125.4/hugo_extended_0.125.4_linux-amd64.tar.gz
tar -xvzf hugo_extended_0.125.4_linux-amd64.tar.gz
sudo mv hugo /usr/local/bin/

5.创建 Hugo 项目(名字自取,这里以myblog为例)

hugo new site myblog
cd myblog
git init

6.配置git信息(上面一行填GitHub用户名,下面一行填GitHub的邮箱)

git config --global user.name "xxx"
git config --global user.email "[email protected]"


7.添加主题(以 BearBlog 为例)

git submodule add https://github.com/janraasch/hugo-bearblog.git themes/hugo-bearblog
echo 'theme = "hugo-bearblog"' >> hugo.toml

8.写文章

hugo new posts/hello.md
nano content/posts/hello.md
#然后在下面随便写点啥

9.配置nginx。

sudo apt install nginx -y

检查是否安装完成(可选):
sudo systemctl status nginx

注意请放行80和443端口。接下来创建配置文件:

sudo cp -r /root/myblog/public /var/www/myblog

sudo nano /etc/nginx/sites-available/hugoblog

内容如下:

server {
listen 80;
server_name example.com;

root /var/www/myblog;
;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

启用并重载。

sudo ln -s /etc/nginx/sites-available/hugoblog /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

10.初始化GitHub仓库并push

git remote add origin https://github.com/你的用户名/仓库名.git
git add .
git commit -m "Initial Hugo site"

注意下面我们将用ssh连接到仓库。先生成密钥:

ssh-keygen -t ed25519 -C "[email protected]"

cat ~/.ssh/id_ed25519.pub

接下来把返回的内容一字不落地复制下来。登录 GitHub:

`打开你的 GitHub 页面
`点右上角头像 → Settings
`左侧点 SSH and GPG keys
`点击 New SSH key
`填标题,粘贴你刚刚复制的内容,保存即可

下面设置远程仓库。注意仓库填你自己的。

git remote set-url origin [email protected]:username/repo.git

下面是测试连接(可选):

ssh -T [email protected]
#然后输入yes。注意别输y,主播经常手欠输y,然后就Host key verification failed.

看到Hi xxx! You’ve successfully authenticated…就好了。

然后:

git push -u origin master

11.本地克隆仓库并创建文章

本地设备以Windows为例。
git clone https://github.com/你的用户名/仓库名.git
cd 仓库目录
hugo new posts/新文章.md

注意路径为C:\Users\你的名字\仓库名,cd 到这个地方。

hugo new posts/新文章.md

编辑并保存。

12.本地上传到gh

git add .
git commit -m "更新了文章"
git push

13.远程创建脚本拉取并编译

nano /root/myblog/deploy.sh

脚本这样写:

#!/bin/bash

cd /root/myblog || exit

git pull origin master

hugo

rsync -av –delete public/ /var/www/myblog/

保存。chmod +x /root/myblog/deploy.sh

下次每次只需要/root/myblog/deploy.sh

就可以实现自动拉取并编译。

这样可以大大便利多人编辑blog的情况。至于自动化部署,以后再说。

0 0 投票数
文章评分
0 评论
内联反馈
查看所有评论
滚动至顶部
0
本文贡献者正期待您发表意见x