创建 Personal Access Token 用于 GitHub Actions 所构建得虚拟系统可以内容推送到仓库

官方教程:https://docs.github.com/cn/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

勾选repo和workflow

将创建好的 Personal Access Token 添加到仓库的 Secrets 中,并设置名称ACCESS_TOKEN

粘贴刚刚生成的token

本地配置

到.github\workflows目录创建一个hexo.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
name: Blog CI/CD

# 触发条件:在 push 到 master 分支后触发
on:
push:
branches:
- master
env:
TZ: Asia/Shanghai

jobs:
blog-cicd:
name: Hexo blog build & deploy
runs-on: ubuntu-latest # 使用最新的 Ubuntu 系统作为编译部署的环境

steps:
- name: Checkout codes
uses: actions/checkout@v2

- name: Setup node
# 设置 node.js 环境
uses: actions/setup-node@v1
with:
node-version: '12.x'

- name: Cache node modules
# 设置包缓存目录,避免每次下载
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

- name: Install hexo dependencies
# 下载 hexo-cli 脚手架及相关安装包
run: |
npm install -g hexo-cli
npm install

- name: Generate files
# 编译 markdown 文件
run: |
hexo clean
hexo generate

- name: Deploy hexo blog
env:
# Github 仓库
GITHUB_REPO: github.com/waimao365/hexo2

# 将编译后的博客文件推送到指定仓库
run: |
cd ./public && git init && git add .
git config user.name "waimao365"
git config user.email "xxxxx@qq.com"
git add .
git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
git push --force --quiet "https://${{ secrets.ACCESS_TOKEN }}@$GITHUB_REPO" master:master

上传部署

git init #如果错误可以用命令删除 rm -rf .git

git add . # themes文件夹不能上传,需要在themes文件夹内删除.git和.gaihub.文件夹

git commit -m “a”

git remote add origin git@github.com:waimao365/xxx.git

git push -u origin master #如果出现错误可以强制上次 git push -f origin master

我建立了两个项目,一个设置成私密,一个设置成公开

以上都是在私密的项目操作

下面这个改成你的公开的项目就可以了

GITHUB_REPO: github.com/waimao365/hexo2 这里也要改成你的公开的项目库

git config user.name “waimao365” 这里要改成你的
git config user.email “xxxxx@qq.com“ 这里要改成你的

vercel导入公开库

为了访问速度更好一些,可以到https://vercel.com/导入公开项目库,再绑定域名,就可以了

域名需要提前解析道

一级域名A到 76.223.126.88

二级域名 CNAME cname.vercel-dns.com