原理是把 Hexo
的源文件和 Actions
放到 blog-source
分支, 生成的静态文件放到主分支. 这样 Actions
里面监听到 blog-source
分支的推送就构建环境生成静态文件, 然后推送到主分支, 这样就可以了.
好处有几个:
- 源代码不用带着跑, 担心丢失, 一直在分支里面
- 不用手动生成静态文件, 部署, 专注于写作
- 不需要用第三方自动构建工具了
Github Actions 配置
在源文件分支新建 .github/workflows/ci.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
| name: 使用 Actions 自动部署 Hexo 博客
on: push: branches: - blog-source
env: GIT_NAME: xxx GIT_EMAIL: xxx@users.noreply.github.com BRANCH_SOURCE: blog-source
jobs: build: name: 构建与部署博客 runs-on: ubuntu-latest steps: - name: 切换到源文件分支 uses: actions/checkout@v2 with: ref: ${{ env.BRANCH_SOURCE }}
- name: 配置 Git 环境 run: | git config --global user.name "$GIT_NAME" git config --global user.email "$GIT_EMAIL"
- name: 设置 NodeJS uses: actions/setup-node@v1 with: node-version: "10.x"
- name: 安装 Hexo 及其相关插件 run: | export TZ='Asia/Shanghai' npm install hexo-cli -g npm install hexo-generator-feed --save
- name: 生成静态文件 run: hexo g
- name: 提交静态文件 run: | cd ./public git init git add --all . git commit -m "Actions CI Auto Builder"
- name: 推送静态文件 uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }} force: true directory: public
|
GITHUB_TOKEN
, 在 https://github.com/settings/tokens ,设置名字为 GITHUB_TOKEN
, 然后勾选 repo, admin:repo_hook, workflow
等选项,最后点击 Generate token
即可。
参考链接