本文基于已经创建了hexo的基础,向github action自动化部署迈进。
出于长远考虑,还是应当将blog结合CI进行完善。避免存放hexo源码的电脑重装或者更换带来的不变。
下面将记录自己的部署过程。
准备工作
- 创建Token
为了确保由github action
持续部署时,其拥有足够的权限进行hexo deploy
的相关操作,需提前创建好token。登录并访问Github->头像(右上角)->Settings->Developer Settings->Persional access tokens->Tokens (classic)
,点击generate new token
,token名和过期时间自定义,必须勾选repo和workflows。

注:创建完成的access token
只会显示一次,切记先拷贝下来。忘了只能重新创建。
- 创建项目
共需创建两个项目(当然也可存放与不同分支,修改workflow的配置即可):
一个私有项目,用于存放hexo博客源码,以**blog**
命名。
一个公有项目,用于存放静态页面,以**test.github.io**
命名。
- 编写workflow的配置文件
在本地hexo博客项目路径的.github
文件新建workflows
文件夹,再在文件夹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 62 63 64 65
| name: Auto Depoly
on: push: branches: - master
release: types: - published
jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout source uses: actions/checkout@v2 with: ref: master
- name: Setup Node.js uses: actions/setup-node@v1 with: node-version: "16.16"
- name: Install hexo run: | export TZ='Asia/Shanghai' npm install hexo-cli -g
- name: Cache hexo uses: actions/cache@v1 id: cache with: path: node_modules key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}
- name: Install dependencies if: steps.cache.outputs.cache-hit != 'true' run: | npm install hexo-wordcount hexo-generator-json-content hexo-generator-feed hexo-generator-sitemap hexo-generator-baidu-sitemap --save
- name: Generate static files run: | hexo clean hexo generate
- name: Depoly to Github Pages env: GIT_NAME: cezz-rm GIT_EMAIL: ${{ vars.GITHUBS_EMAIL }} GIT_TOKEN: ${{ vars.GITHUBS_TOKEN }}
run: | export TZ='Asia/Shanghai' cd ./public git init git config --global user.name '${{ vars.GITHUBS_USERNAME }}' git config --global user.email '${{ vars.GITHUBS_EMAIL }}' git add . git commit -m "${{ github.event.head_commit.message }} $(date +"%Z %Y-%m-%d %A %H:%M:%S") Updated By Github Actions" git push --force --quiet "https://${{ vars.GITHUBS_USERNAME }}:${{ vars.GITHUBS_TOKEN }}@github.com/${{ vars.GITHUBS_USERNAME }}/${{ vars.GITHUBS_PAGENAME }}.github.io.git" master:master
|
注:配置文件中的分支需与实际情况相对应。
blog项目配置
来到仓库下的Settings->Secrets and variables->Actions
,切换到Variables
, 点击New repository variable
,新建以下四个变量:
**GITHUBS_USERNAME**
:gihub的用户名。
**GITHUBS_EMAIL**
:电子邮箱,运行失败会有邮件推送。
**GITHUBS_PAGENAME**
:test.github.io
中的test,按实际情况填写。
**GITHUBS_TOKEN**
:填写准备工作中创建的token。
test.github.io项目配置
来到仓库下的Settings->Pages
下,进行如下相关配置,有域名可绑定域名。
测试验证
来到本地存放blog
源代码的路径,创建新的文章提交。参考命令如下:
1 2 3
| git add . git commit -m "add new blog" git push origin master
|
去页面上点击该仓库的Actions
,可以看到记录了每一次的过程,点击其中一条查看如下:
