Hexo&Github Action持续部署

目的

之前一直是用的是mac和windows 两个设备,有时候想写文章发布也不是很方便,于是就想弄个自动部署,除此之外还等输入hexo new,hexo s,hexo g,hexo d等繁琐命令

写文章

  • 在你博客本地目录下的source文件夹内的_posts文件夹下尽情书写博客吧
  • 写完博客通过关联远程Hexo私有源码库进行push,github action检测到分支变动会进行重新编译并重新部署public下博客内容到你的公有库
  • 当你换一台机器进行博客书写,只需要关联远程源码库进行pull下,就可以马上进行内容书写了

    准备仓库

    私有仓库: blog
    这里是存放 Hexo 博客源码的

公有仓库: 用户名.github.io
这里是用来 public 静态页面的,最好是空的,当然是你现在的hexo博客也可以。

获取 GitHub 令牌

登录你的 GitHub 账号,点击右上角的头像,点击「Settings」进入设置。
img.png
点击菜单栏中的「Developer settings」进入开发者设置。
img.png
点击菜单栏中的「Personal access tokens」进入令牌设置。
img.png
点击「Generate new token」新建一个令牌。
img.png
勾选全部的权限,名称随意。
并点击「Generate token」完成生成。
img.png
记得保存好这个令牌,它不会再次出现。只能重新生成个新的。

新建仓库

使用 GitHub 新建一个存放 Hexo 文件的仓库,要选私有仓库
不要勾选任何的初始化仓库选项!
在 Hexo 根目录中删除 .git 文件夹(隐藏文件夹),删除主题目录下的 .git 文件夹。
然后在 Hexo 根目录下使用 cmd 或终端运行以下命令:

1
2
3
4
5
git init # 新建 Git 仓库
git add -A # 暂存所有文件
git commit -m "Create" # 提交更新
git remote add origin https://github.com/用户名/新建的私有仓库名.git # 新增远程链接
git push -u origin master # 推送至远程仓库

新建action 脚本

3、附上我的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
name: Deploy # 部署

on: # 触发条件
push:
branches:
- main # 推送到 main 分支

release:
types:
- published # 推送新版本号

workflow_dispatch: # 手动触发

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout # Checkout 仓库
uses: actions/checkout@v2
with:
ref: main

- name: Setup Node # 安装 Node.js
uses: actions/setup-node@v1
with:
node-version: "12.x"

- name: Install Hexo # 安装 Hexo
run: |
npm install hexo-cli -g

- name: Cache Modules # 缓存 Node 插件
uses: actions/cache@v1
id: cache-modules
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

- name: Install Dependencies # 如果没有缓存或 插件有更新,则安装插件
if: steps.cache-modules.outputs.cache-hit != 'true'
run: | # 如果仓库里没有 package-lock.json,上传一下,npm ci 必须要有 package-lock.json
git config --global url."https://".insteadOf ssh://git@
npm ci
- name: Generate # 生成
run: |
hexo clean
hexo generate
- name: Deploy # 部署
run: |
git config --global user.name "用户名"
git config --global user.email "邮箱"
git config --global init.defaultBranch main
export TZ='Asia/Shanghai'
cd public/
git init
git add -A
git commit -m "Create by workflows"
git remote add origin https://【token】@github.com/【用户名】/【仓库名】
git push origin main -f

参考网址:
https://anzhiy.cn/posts/asdx.html
https://zsyyblog.com/8598dd5c.html


Hexo&Github Action持续部署
http://example.com/2023/02/01/hexo + github action/
作者
柳遇安
发布于
2023年2月1日
许可协议