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

AI摘要:

参考:

🔗利用Github Action实现Github到Gitee的持续同步 - SSgeek - 博客园 (cnblogs.com)

🔗使用GitHub Actions自动编译部署hexo博客 - Fungit - 博客园 (cnblogs.com)

配置SSH,使action可以推送代码

首先在本地生成一个ssh密钥对

1
ssh-keygen -t rsa -f ~/Documents/ssh-key/id_rsa(对应路径即可)

找到id_rsa.pub文件,在GithubSSH公钥中添加上面生成的密钥对的公钥,或settings—>Deploy key(仅对当前仓库有效)

找到id_rsa文件,在github打开对应action的仓库settings—>secrets,新建一个仓库secret,名为HEXO_DEPLOY_KEY,值为上面生成的密钥对的私钥

配置action

在仓库新建/.github/workflows/pages.yml

配置ssh位置 /home/runner/.ssh/

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
name: Deploy hexo site to Pages # 脚本 workflow 名称

on:
push:
branches: [main] # 当监测 main,master 的 push
paths: # 监测所有 source 目录下的文件变动,所有 yml,json 后缀文件的变动。
- '*.json'
- '**.yml'
- '**/source/**'
- '**/scripts/**'

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Default to bash
defaults:
run:
shell: bash

jobs:
build: # 任务名称
timeout-minutes: 30 # 设置 30 分钟超时
runs-on: ubuntu-latest # 指定最新 ubuntu 系统
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
# Examples: 20, 18.19, >=16.20.2, lts/Iron, lts/Hydrogen, *, latest, current, node
# Ref: https://github.com/actions/setup-node#supported-version-syntax
node-version: "20"
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Install Hexo-cli # 安装 Hexo
run: |
npm install -g hexo-cli --save
echo "install hexo successful"
- name: Build Blog # 编译创建静态博客文件
run: |
hexo clean
hexo generate
echo "build blog successful"
- name: Set ssh Permission
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
rm -rf /home/runner/.ssh
mkdir -p /home/runner/.ssh/
echo "$ACTION_DEPLOY_KEY" > /home/runner/.ssh/id_rsa
chmod 700 /home/runner/.ssh
chmod 600 /home/runner/.ssh/id_rsa
ssh-keyscan github.com >> /home/runner/.ssh/known_hosts
- name: Deploy to Github # 设置 git 信息并推送静态博客文件
run: |
git config --global user.email "deploy@qq.com"
git config --global user.name "deploy"
hexo deploy
- run: echo "Deploy Successful!"

经测试重新生成了一个初始博客文章hello-world.md,待解决


使用GitHub Actions自动编译部署hexo博客
https://blog.cngo.rr.nu/posts/428a.html
作者
cngo
发布于
2024年7月14日
许可协议