hexo push后自动部署github pages

hexo有几种部署方式,一种是本地编译后,直接push public路径下的静态文件,一种是通过cli方式,向仓库提交source下的的markdown文件,触发action.实现自动部署

本文主要说明后一种方式.

创建github workflow

hexo的根目录的.github/workflows/pages.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
name: Pages

on:
push:
branches:
- main # default branch

jobs:
pages:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
# 看下自己本地的noe版本
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

创建github pages

参考 https://docs.github.com/en/pages/getting-started-with-github-pages/creating-a-github-pages-site

创建完成后,微调一下:

settings->Pages->Build and deployment->Branch选项中,把branch改成gh-pages

初始化本地仓库

1
2
3
4
git init .
git remote add origin ${你上面创建的仓库地址}
git remote add .
git commit -m "init project"

gitgnore

由于使用了github的自动部署功能,所以无需上传静态文件,在hexo根目录添加.gitignore文件

1
2
3
public/
.deploy*/
node_modules

提交

最后提交到main分支

1
git push -u origin main

提交完成后,在githubaction选项中可以看到正在构建,构建完成,就可以在https://${your-username}.github.io路径访问你的在线博客了

参考

https://hexo.io/docs/github-pages

Author

finger

Posted on

2023-09-20

Updated on

2025-06-11

Licensed under

Comments