Token的获取

  1. 登录Github
  2. 点击头像
  3. 点击setting
  4. 点击左侧的Developer Settings
  5. 点击Personal access tokens
  6. 点击Tokens
  7. 自定义名称
  8. 时间选择永不过期
  9. 点击repo和workflow权限

Github仓库建立

  1. 新建一个Github仓库,这里我取名为hexo-source-repo,选择为私有仓库

Github Action的配置

  1. [Blogroot]新建.github文件夹,注意开头是有个.的。然后在.github内新建workflows文件夹,再在workflows文件夹内新建autodeploy.yml,在[Blogroot]/.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
name: 自动部署
# 当有改动推送到master分支时,启动Action
on:
push:
branches:
- 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: "12.x"

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

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

- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
# npm install gulp-cli -g #全局安装gulp
npm install hexo-neat -g #使用了neat压缩网站
npm install --save

- name: 生成静态文件
run: |
hexo clean
hexo bangumi -u #bilibili番剧更新
hexo generate
# gulp

- name: 部署到Github
uses: JamesIves/github-pages-deploy-action@v4
with:
token: [token] #替换成自己的
repository-name: [username]/[username].github.io #替换成自己的
branch: main
folder: public
commit-message: "${{ github.event.head_commit.message }} Updated By Github Actions"
  1. 删除或者先把[Blogroot]/themes/butterfly/.git移动到非博客文件夹目录下,原因是主题文件夹下的.git文件夹的存在会导致其被识别成子项目,从而无法被上传到源码仓库。
  2. 在博客根目录[Blogroot]路径下运行指令
1
2
3
git init #初始化
git remote add origin git@github.com:[GithubUsername]/[SourceRepo].git #[SourceRepo]为存放源码的github私 有仓库
git checkout -b main # 切换到main分支,
  1. 添加屏蔽项
    因为能够使用指令进行安装的内容不包括在需要提交的源码内,所有我们需要将这些内容添加到屏蔽项,表示不上传到 github 上。这样可以显著减少需要提交的文件量和加快提交速度。打开[Blogroot]/.gitignore,输入以下内容:
1
2
3
4
5
6
7
8
9
10
11
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
.deploy_git*/
.idea
themes/butterfly/.git
# 如果不是butterfly主题,记得替换最后一行内容为你自己当前使用的主题。
  1. 之后再运行 git 提交指令,将博客源码提交到 github 上。
1
2
3
git add .
git commit -m "github action update"
git push origin main