Hexo博客搭建相关参考

使用Github Actions自动部署Hexo博客

https://xulianjun.github.io/2024/09/08/Hexo%20+%20GitHub%20Action,零成本自动化部署个人博客/
https://xulianjun.github.io/2024/09/08/Hexo%20+%20GitHub%20Action,零成本自动化部署个人博客/

加载动画

https://blog.anheyu.com/posts/52d8.html

访问post文件夹下的页面出现499响应

使用:宝塔php网站(静态),cloudflare代理,cloudfalre的ssl

尚不清楚为什么会产生这样的问题,可能是对post路径有拦截等限制。
解决办法:
1.将hexo的_config.yml文件中的permalink: :layout/:year:month:day:hour:minute:second.html修改为permalink: articles/:year:month:day:hour:minute:second.html
(即本来文章在post文件夹下面,现在都在articles下面了。)
2.设置pretty_urls:
trailing_index: true
trailing_html: true

自动从github上拉取更新到服务器上

本地推送hexo博客的源码到github仓库A,并使用github workflow自动部署到另一个github仓库B中

注意!!:A仓库存储hexo博客的源码,B仓库存储hexo编译后的静态文件。

在本地的hexo源码根目录下,创建.github/workflows目录,在该目录下创建一个名为deploy.yml的文件,内容如下(注意替换<>中的内容):

name: Deploy Hexo to GitHub Pages

on:
push:
branches:
- master # 当推送到 master 分支时触发(该分支是A仓库的分支)

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 30 # 设置 30 分钟超时

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
submodules: false # 禁用子模块检查

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'v22.15.0'

- name: Cache node_modules # 缓存 node_modules,提高编译速度
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-

- name: Install Dependencies
run: |
npm install
echo "init node successful"

- name: Install Hexo Git Deployer
run: |
npm install hexo-deployer-git --save
npm install -g hexo-cli --save
echo "install hexo successful"
npm install hexo-renderer-pug hexo-renderer-stylus --save
npm install hexo-wordcount --save
npm i @barry-flynn/hexo-github-calendar
npm install hexo-butterfly-envelope --save
npm install [email protected] --save
npm install [email protected] --save
npm install pwa-asset-generator

- name: Clean and Generate Static Files
run: |
hexo clean
hexo generate
hexo swpp
echo "build blog successful"

- name: Configure Git
run: |
git config --global user.name '<B仓库的github用户名>'
git config --global user.email '<B仓库的github用户名>@users.noreply.github.com'

- name: Deploy to GitHub Pages
run: |
cd public/
git init
git add -A
git commit -m "Create by workflows"
git branch -M main # B仓库的分支名为main
git remote add origin https://<B仓库的github classic token>@github.com/<B仓库的github用户名>/<B仓库的名字>.git
git push origin HEAD:main -f # B仓库的分支名为main

将hexo源代码推送到A仓库后,github workflow会自动编译并将hexo的public文件(也就是静态博客文件)推送到B仓库中。

服务器自动拉取B仓库的静态文件到指定目录

注意!!:B仓库存储hexo编译后的静态文件。

在宝塔的服务器上,安装git,并配置github的ssh密钥,以方便服务器直接从github中拉取私有仓库内容。

配置服务器中的webhook

在宝塔服务器的软件商店中安装宝塔WebHook应用,然后编辑运行脚本为以下内容(注意替换<>中的内容):

#!/bin/bash

# 输出更新时间
echo "网站更新时间为:"
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"

# project_name为网站项目
cd /www/wwwroot/<网站存储目录> || exit

# 检查是否为git仓库
if [ ! -d ".git" ]; then
echo "初始化新的git仓库"
git init
git remote add origin [email protected]:<B仓库的github用户名>/<B仓库的名字>.git
# 设置默认分支并拉取
git branch -M main # B仓库的分支名为main
git pull origin main --force # B仓库的分支名为main
else
echo "更新现有仓库"
# 获取远程最新状态
git fetch origin
# 强制将本地分支重置为远程分支的状态
git reset --hard origin/main # B仓库的分支名为main
# 删除本地所有未被Git跟踪的文件和目录
git clean -fd
fi

配置github的webhook

在B仓库的Settings->Webhooks->Add webhook中,配置如下内容:

  1. Payload URL设置为:
    在宝塔面板的宝塔webhook应用中,点击查看密钥,会给出一个类似于https://<服务器ip或为宝塔面板设置的域名>:<服务器上宝塔面板的端口>/hook?access_key=<密钥>&param=<参数>的url,填入github上的Payload URL即可。

    这个url中的参数不用管,因为在运行脚本中没有使用。

  2. Content type设置为:

application/json

  1. Secret设置为:
    在宝塔面板的宝塔webhook应用中,点击查看密钥,会给出密钥,填入github上的Secret即可。

  2. SSL verification设置为:

Enable SSL verification

注意开启ssl需要宝塔面板配置域名,并配置ssl证书,且ssl证书不能使用cloudflare的免费ssl证书(可以在宝塔面板中申请一个Let’s Encrypt的免费证书),否则会提示无法验证证书品牌:We couldn't deliver this payload: tls: failed to verify certificate: x509: certificate signed by unknown authority

  1. Which events would you like to trigger this webhook?设置为:

Just the push event.

  1. Active设置为:

Active
7. 点击Add webhook即可。

这样,当B仓库的master分支有更新时,github会自动向Payload URL发送一个POST请求,宝塔webhook应用会接收到这个请求,并执行运行脚本,将B仓库的静态文件拉取到服务器的指定目录中。

自建rustdesk中继服务器

https://www.dujin.org/22701.html
https://linux.do/t/topic/179196