HEXO自动化部署到Github博客
发表于:2024-07-12 |

HEXO自动化部署到Github博客

配置SSH

1
2
3
git config --global user.name "bengenseo"
git config --global user.email "[email protected]"
ssh-keygen -t rsa -C "[email protected]"

空值 三次回车

1
ssh -T [email protected]

输入 yes

Warning: Permanently added ‘github.com’ (ED25519) to the list of known hosts.
Hi bengenseo! You’ve successfully authenticated, but GitHub does not provide shell access.
警告:已将“github.com”(ED25519)永久添加到已知主机列表中。
嗨 bengenseo!您已成功通过身份验证,但 GitHub 不提供 shell 访问权限。

1
ssh-keygen -t rsa -b 4096 -C "[email protected]"

插件

1
2
3
4
5
6
npm install gulp
npm install gulpfile
npm install gulp-clean-css
npm install gulp-uglify
npm install gulp-concat
npm install hexo-cli -g

.github下创建workflows文件夹

auto.yml

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
name: 自动部署
# 当有改动推送到master分支时,启动Action
on:
push:
branches:
- main
#2020年10月后github新建仓库默认分支改为main,注意更改
release:
types:
- published

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v2
with:
ref: main

- name: 安装 Node
uses: actions/setup-node@v1
with:
node-version: "20.x"

- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 缓存 Hexo
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: node_modules
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: 安装依赖
if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
run: |
npm install gulp-cli -g #全局安装gulp
npm install --save

- name: 生成静态文件
run: |
hexo clean
hexo generate
gulp

- name: 部署到Github
uses: JamesIves/github-pages-deploy-action@v4
with:
token: ${{ secrets.ACTIONS_DEPLOY_KEY }}
repository-name: bengenseo/bengenseo.github.io
branch: main
folder: public
commit-message: "${{ github.event.head_commit.message }} Updated By Github Actions"

创建gulpfile.js

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
const { src, dest, series } = require('gulp');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
const fs = require('fs');


// Minify CSS files
function minifyCSS() {
if (fs.existsSync('source/css') && fs.readdirSync('source/css').length > 0) {
return src('source/css/*.css')
.pipe(cleanCSS())
.pipe(dest('public/css'));
} else {
return new Promise((resolve) => {
console.log('No CSS files to minify.');
resolve();
});
}
}

// Minify JS files
function minifyJS() {
if (fs.existsSync('source/js') && fs.readdirSync('source/js').length > 0) {
return src('source/js/*.js')
.pipe(uglify())
.pipe(dest('public/js'));
} else {
return new Promise((resolve) => {
console.log('No JS files to minify.');
resolve();
});
}
}

// Default task
exports.default = series(minifyCSS, minifyJS);

创建CNAME

1
blog.sjooo.sbs

themes/主题/创建.gitkeep

.gitkeep

1
2
mkdir -p themes/*
touch themes/*/.gitkeep

JSON搜索文件

其它插件

1
2
npm install hexo-generator-json-content --save
npm install hexo-generator-feed --save

根目录/_config.yml

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
#JSON搜索文件
jsonContent:
meta: false
pages: false
posts:
title: true
date: true
path: true
text: true
raw: false
content: false
slug: false
updated: false
comments: false
link: false
permalink: false
excerpt: false
categories: false
tags: true
#RSS订阅
feed:
type: atom
path: atom.xml
limit:
hub:
content:
content_limit:
content_limit_delim: ' '
order_by: -date

链接配置

插件

1
npm install hexo-abbrlink --save

_config.yml

1
2
3
4
abbrlink:
alg: crc32 # 算法,可选 crc16 和 crc32
rep: hex # 生成的链接形式,可选 dec 和 hex
permalink: posts/:abbrlink.html

创建项目-空文件夹中运行

1
2
hexo init
npm install hexo-deployer-git --save
1
hexo d -g

运行程序

1
2
3
hexo cl
hexo g
hexo s

第一次

1
2
3
4
5
6
7
8
9
hexo cl
hexo g
git init
git remote rm origin
git remote add origin [email protected]:bengenseo/bengenseo.github.io.git
git checkout -b main
git add .
git commit -m "github action update"
git push origin main

出错

1
2
3
4
git pull origin main
git add .
git commit -m "Merge remote changes"
git push origin main

第二次

1
2
3
4
5
6
git remote rm origin
git remote add origin [email protected]:bengenseo/bengenseo.github.io.git
git checkout -b main
git add *
git commit -m "github action update"
git push origin main

强制推送

1
2
3
4
5
6
7
8
hexo cl
hexo g
git remote rm origin
git remote add origin [email protected]:bengenseo/bengenseo.github.io.git
git checkout -b main
git add *
git commit -m "github action update"
git push origin main --force
上一篇:
Pycharm连接Docker
下一篇:
自动化部署博客