Github Actions 简介

Github Actions 可以很方便实现 CI/CD 工作流,类似 Travis 的用法,来帮我们完成一些工作,比如实现自动化测试、打包、部署等操作。当我们运行 Jobs 时,它会创建一个容器 (runner),容器支持:Ubuntu、Windows 和 MacOS 等系统,在容器中我们可以安装软件,利用安装的软件帮我们处理一些数据,然后把处理好的数据推送到某个地方。

前提

1.您已经创建了hexo博客
2.您已经注册了github的账户
3.您已经创建了github项目并上传了hexo源码

创建

在项目根目录下创建.github/workflows/main.yml

1.点击此处申请 Personal access tokens (classic)

2.在Settings-secrets and variables-Actions下设置HEXOBLOG为上一步得到的Personal access tokens

1.png

3.可以把以下内容粘贴进去

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
run-name: Deploy 

on:
push:
branches:
- main

release:
types:
- published

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

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

- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: "18.x"

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

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

- name: npm Install
run: |
npm install

- name: Generate
run: |
hexo clean
hexo generate
hexo deploy

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.hexoblog }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: ./public
commit_message: ${{ github.event.head_commit.message }}

实现的功能

在hexo项目main分支有更新时,会自动更新仓库下分支gh-pages