使用GitHub Actions自动编译部署hexo博客

2022-11-08,,,,

前言

使用hexo博客也挺久的,最开始是本地hexo clean && hexo g,最后hexo d推送到服务器。后来是本地hexo clean && hexo g,最后将生成文件推送到GitHub,GitHub actions 推送到服务器。后来本地文件丢失,GitHub 仅仅存着编译好public文件夹内容,该丢失就丢失了。有GitHub actions何不直接把编译、部署都交给他,还能备份博客,本地也不用安装一堆环境,使用这个方法也有段时间了,记录下。为了方便建站和编写,还是本地建议安装hexo。


一、初始配置

ssh密钥配置

ps: 只有且使用一个密钥就不要看了

github配置

建立私有仓库fungit.org ,不要初始化仓库。

本地生成两份ssh密钥,一份用来本地push到GitHub,一份GitHub actions用来推送到服务器

ssh-keygen -t rsa -b 4096 -f /c/Users/Administrator/Documents/ssh/fungit_blog_github
ssh-keygen -t rsa -b 4096 -f /c/Users/Administrator/Documents/ssh/github_to_server

在GitHub私有仓库fungit.org添加 Depoly key,把刚刚生成的fungit_blog_github.pub添加进去,勾选write权限,用来推送本地文件。

添加一个Secret,把刚刚生成的github_to_server添加进去(注意是私钥)名为GITHUB_TO_SERVER_KEY,用来GitHub actions推送到服务器。

服务器配置

新建git用户并配置密码

useradd -m -s /bin/bash git
passwd git

将认证公钥加入git用户认证文件

ssh-copy-id -i /c/Users/Administrator/Documents/ssh/github_to_servera.pub git@ip -p 22222

-i: 指定文件
-p: ssh连接端口

你也可以手动在/home/git 新建.ssh/authorized_keys 文件,把github_to_servera.pub添加到authorized_keys。.ssh文件夹默认权限为700,authorized_keys文件默认权限是600。

本地配置

安装hexo最新版本,最好安装git-scm, git bash挺好用的。

本地配置多个ssh-key,需要手动配置使用指定的ssh密钥。使用git bash,新建config文件

vim ~/.ssh/config
#添加以下内容
Host fungit.blog
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\fungit_blog_github
PreferredAuthentications publickey
IdentitiesOnly yes

Host: 区分默认的github.com,仓库链接使用fungit.blog 替换 github.com推送时就会匹配IdentityFile对应的密钥文件。

配置hexo站点

hexo init fungit.org
cd fungit.org
rm -rf .git
git init

连接私有仓库fungit.org

git remote -add origin git@fungit.blog:kroyoo/fungit.org.git

注意git@github.com使用上面config配置的git@fungit.blog替换。

测试是否可以正常通信

$ ssh -T git@fungit.org
Hi fungit.org! You've successfully authenticated, but GitHub does not provide shell access.

至此,ssh密钥配置完成,当然,如果本地只有使用一个默认密钥id_rsa,不用那么麻烦,直接新建站点连接仓库就行了。


二、自动化部署

本地配置

如果你是跟我一样不喜欢本地安装依赖,只需手动把依赖添加到package.json

我的package.json文件参考:

{
....
...
"dependencies": {
"hexo": "^5.0.0",
"hexo-deployer-git": "^2.1.0",
"hexo-generator-archive": "^1.0.0",
"hexo-generator-category": "^1.0.0",
"hexo-generator-index": "^2.0.0",
"hexo-generator-tag": "^1.0.0",
"hexo-renderer-ejs": "^1.0.0",
"hexo-renderer-jade": "^0.5.0",
"hexo-renderer-marked": "^3.0.0",
"hexo-renderer-stylus": "^2.0.0",
"hexo-server": "^2.0.0",
"hexo-wordcount": "^6.0.1"
}
}

GitHub Actions配置

不多说,先看yaml文件:

name: Auto Deploy
on:
watch:
types: [started]
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id
steps:
- name: Checkout source
uses: actions/checkout@v2
with:
ref: main
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '12'
- name: Setup Hexo
run: |
git config --global user.email "deploy@fungit.org"
git config --global user.name "deploy"
npm install hexo-cli -g --no-audit
npm install --no-audit - name: hexo clean and hexo g
run: |
hexo clean
hexo g - name: Deploy to Server
uses: easingthemes/ssh-deploy@v2.1.5
env:
SSH_PRIVATE_KEY: ${{ secrets.GITHUB_TO_SERVER_KEY }}
ARGS: "-rltgoDzvO --delete"
SOURCE: "public/"
REMOTE_HOST: ${{ secrets.SERVER_IP }}
REMOTE_PORT: ${{ secrets.SERVER_PORT }}
REMOTE_USER: ${{ secrets.SERVER_USER }}
TARGET: ${{ secrets.REMOTE_TARGET }} - name: Deploy gh-pages
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
rm -rf ~/.ssh
mkdir -p ~/.ssh/
echo 'fungit.org' > public/CNAME
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
hexo deploy

为了方便,使用了easingthemes/ssh-deploy@v2.1.5推送工具,详细可以去了解下。随便看看peaceiris/actions-gh-pages@v3还有最后我推送到gh-pages分支的写法。

SOURCE: 需要推送的目录,hexo生成在public。
SSH_PRIVATE_KEY: ssh私钥、私钥。前面配置的GITHUB_TO_SERVER_KEY。
TARGET: 服务器目录如/home/wwwroot/fungit.org

ps: 所需依赖最好放在package.json里面,这样Vervel部署也方便。 部署到ph-pages分支和连接vercel是为了以后服务器炸了、迁移方便切过去(懒,不想修)

Vercel:


GitHub Actions:

作者: Yeksha

連結: https://fungit.org/2021/github-acionts-automatically-compile-deploy/

來源: Fungit

著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

使用GitHub Actions自动编译部署hexo博客的相关教程结束。

《使用GitHub Actions自动编译部署hexo博客.doc》

下载本文的Word格式文档,以方便收藏与打印。