搭建个人博客
Github Actions
GitHub Actions
帮助您自动完成软件开发周期内的任务。 GitHub Actions
是事件驱动的,意味着您可以在指定事件发生后运行一系列命令。 例如,每次有人为仓库创建拉取请求时,您都可以自动运行命令来执行软件测试脚本。
关于GitHub Actions
的介绍请移步官方文档
准备工作
使用 Github Actions
将生成的 Hexo
博客文件 push
到 Github Pages
仓库或者其他平台的仓库时,需要进行身份认证。
博主使用的是SSH KEY
:
- 生成
ssh key
windows
用户可以使用 Git Bash
,linux
和mac
用户可以使用终端生成。
ssh-keygen -t rsa -C “your_email@example.com”
- 将
~/.ssh/id_rsa.pub
生成的公钥添加到 GitHub
的 SSH keys

- 将
~/.ssh/id_rsa
生成的私钥添加到 Hexo
仓库的 Secrets
,密钥名为 GH_ACTION_DEPLOY_KEY

创建 Workflow
workflow
: CI/CD 的工作流
首先我们在Hexo
主目录创建一个 .github/workflows
文件夹,并在该文件夹下创建一个 blog.yml
文件
1
| name: hexo-deploy-actions
|
也可以指定分支
1 2 3 4
| on: push: branches: - master
|
- 创建
job
,使用 ubuntu
为运行环境
job
: 任务,比如构建,测试和部署。每个 workflow
由多个 job
组成.
一个 CI/CD 的工作流有许多 jobs
组成,比如最典型的 job
是 lint
,test
,build
或者 deploy
。
1 2 3 4
| jobs: build: runs-on: ubuntu-latest
|
创建 Step
step
: 每个 job
由多个 step
组成
现在我们已经创建了一个push
到主分支执行的Workflow
,并指定运行在Ubuntu
环境下。
1 2 3 4 5 6 7 8 9
| jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Master uses: actions/checkout@master with: submodules: true
|
使用 setup-node
在当前环境中安装 nodejs 12.x
1 2 3 4
| - name: Setup Node.js 12.x uses: actions/setup-node@master with: node-version: "12.x"
|
安装完成nodejs
环境后,我们再去安装hexo
和以及其他npm
依赖
1 2 3 4
| - name: Setup Hexo Dependencies run: | npm install hexo-cli@4.2.0 -g npm install
|
此时我们需要私钥复制到系统环境中,并把github
添加到known_hosts
1 2 3 4 5 6 7 8
| - name: Setup Deploy Private Key env: GH_ACTION_DEPLOY_KEY: ${{ secrets.GH_ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$GH_ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts
|
设置全局的 Git
用户信息
1 2 3 4
| - name: Setup Git Infomation run: | git config --global user.name 'hvnobug' git config --global user.email '972080809@qq.com'
|
生成 Hexo
博客
1 2 3 4
| - name: Generate Hexo run: | hexo clean hexo generate
|
Push
到 GitHub Pages
1 2 3 4 5 6 7 8
| - name: Deploy Hexo To Github run: | cd public rm -rf .git echo blog.hvnobug.com >> CNAME git init git add -A && git commit -m "Site updated: `date '+%Y-%m-%d %H:%M:%S'`" git push -f git@github.com:hvnobug/hvnobug.github.io.git master
|
到这里我们的 GitHub Actions
已经配置完毕,此时我们只需要push
到master
分支就会触发workflow
.
完整的 blog.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
| name: hexo-deploy-actions on: push: branches: - master jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Master uses: actions/checkout@master with: submodules: true - name: Setup Node.js 12.x uses: actions/setup-node@master with: node-version: "12.x" - name: Setup Hexo Dependencies run: | npm install hexo-cli@4.2.0 -g npm install - name: Setup Deploy Private Key env: GH_ACTION_DEPLOY_KEY: ${{ secrets.GH_ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$GH_ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts - name: Setup Git Infomation run: | git config --global user.name 'hvnobug' git config --global user.email '972080809@qq.com' - name: Generate Hexo run: | hexo clean hexo generate
- name: Deploy Hexo To Github run: | cd public rm -rf .git echo blog.hvnobug.com >> CNAME git init git add -A && git commit -m "Site updated: `date '+%Y-%m-%d %H:%M:%S'`" git push -f git@github.com:hvnobug/hvnobug.github.io.git master
|