前提要求

创建两个远程仓库

这里采用 github 平台

image-20230623004808613

source-repo 作为项目的总源码仓库,而另外一个作为部署后的工作仓库

申请 token

Settings/Developer Settings/Personal accses tokens/Tokens(classic)设置下新建token

image-20230623005630047

image-20230623005900876

配置文件

.github/workflows下新建文件autodeploy.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: 自动部署

on:
push:
branches:
- main
release:
types:
- published

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v3
with:
ref: main
- name: 安装 Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g
- name: 缓存 Hexo
uses: actions/cache@v3
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}
- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --save
# npm install gulp-cli -g
- name: 生成静态文件
run: |
hexo clean
hexo generate
# gulp
#hexo bangumi -u
#hexo cinema -u
- name: 部署
run: |
cd ./public
git init
git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
git config --global user.email '${{ secrets.GITHUBEMAIL }}'
git add .
git commit -m "U updated via Github Actions."
git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/hexo-blog.git" master:main # 后面的仓库地址改成自己的工作仓库地址
# git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" master:main
# git push --force --quiet "https://${{ secrets.GITEEUSERNAME }}:${{ secrets.GITEETOKEN }}@gitee.com/${{ secrets.GITEEUSERNAME }}/${{ secrets.GITEEUSERNAME }}.git" master:master
# - name: 构建 Gitee Pages
# uses: yanglbme/gitee-pages-action@master
# with:
# gitee-username: ${{ secrets.GITEEUSERNAME }}
# gitee-password: ${{ secrets.GITEE_PASSWORD }}
# gitee-repo: ${{ secrets.GITEEUSERNAME }}/${{ secrets.GITEEUSERNAME }}

第一步、添加环境变量

image-20230623010045260

在这里我只是部署到github上,如有其他平台可以根据autodeploy.yml文件里的部署步骤下的run命令自行添加,里面各自的$环境变量$也得在上面图中填写

部分变量说明:

GITHUBUSERNAME:github 用户名

GITHUBEMAIL:github 邮箱

GITHUBTOKEN:上面创建的 token

第二步、将 hexo 代码上传到远程仓库

上传等一会儿就可以点开Actions页面看项目的部署情况

image-20230623010531992

image-20230623010605536

image-20230623010621264

等部署完成之后打开工作仓库hexo-blog就可以看到有一个commit

image-20230623010734669

第三步、利用 vercel 部署项目

这里选择用vercel来对项目进行部署,如有其他选择可以自行选择。

注册vercel账号 - Add New Project

image-20230623010915680

直接导入刚才的hexo-blog仓库

image-20230623011009424