静态博客简单,可是发布博文时稍显麻烦,通常须要下面两步:html
hexo clean hexo g -d // 至关于 hexo g + hexo d
若是考虑到同步源文件,还须要每次更改后,将源文件 push 到指定仓库:node
git push origin master
咱们能够将 Hexo 文件分为两类,一类是源文件,即下面这些文件:git
. ├── _config.yml ├── package.json ├── scaffolds ├── source | ├── _drafts | └── _posts └── themes
一类是 public 文件,即网站文件:github
public ├── 2020 ├── categories ├── tags ....
发布博文的这三个操做表明:web
我使用一个私有仓库存放 Hexo 源文件,在 deppwang/deppwang.github.io 中存放网站文件。因此每次发布或者更新博文时,须要使用 push 操做更新源文件,再执行 hexo clean
、hexo g -d
更新博客,比较麻烦。shell
因此咱们但愿能在 push 源文件后,由 CI/CD(持续集成/持续部署)工具为咱们执行 hexo clean
、hexo g -d
这两个操做。npm
CI/CD 工具前有 Travis CI,现有 GitHub Actions,这里使用 GitHub Actions。json
GitHub Actions 的工做原理:咱们提早设置好须要自动化执行的任务,GitHub Actions 监控当前仓库的某一个操做(如:push),一旦有此操做,就自动化执行这些任务。ubuntu
因此咱们但愿使用 GitHub Actions 后,只须要往源文件仓库 push 更新源文件,GitHub Actions 监控到 push 操做时,就自动化执行 hexo clean
、hexo g -d
操做,完成博文发布。缓存
Action 存放在项目根目录的 .github/workflows
下,后缀为 .yml
。一个 Action 至关于一个工做流 workflow,一个工做流能够有多个任务 job,每一个任务能够分为几步 step。任务、步骤依次执行。
每一个 Action 是一个独立脚本,因此能够做为代码仓库。
actions/setup-node
就表示 github.com/actions/setup-node
这个 仓库,表明安装 node.js。Action 为 action.yml能够经过下面这种格式来使用别人写好的 action,@借用了指针的概念:
actions/setup-node@74bc508 # 指向一个 commit actions/setup-node@v1.0 # 指向一个标签 actions/setup-node@master # 指向一个分支
关于 GitHub Actions 更多知识,请看 GitHub Actions 入门教程 - 阮一峰。
如今须要实现一个 Action,使其可以执行 hexo clean
、hexo g -d
操做。
我是使用的 sma11black 已经开发好的 Hexo Action,这个 Action 针对的是存放 Hexo 源文件和网站文件分开存放的场景。请先看 教程,如下为教程的补充。
非第一次生成 SSH Key:
ssh-keygen -t rsa -f ~/.ssh/id_rsa_x -C "yourmail@xxx.com"
将生成的 private key
做为 Hexo 源文件仓库 Settings > Secrets
的 一个名叫 DEPLOY_KEY
的 Secret
。注意:须要复制包括 -----BEGIN OPENSSH PRIVATE KEY-----
和 -----END OPENSSH PRIVATE KEY-----
的整个内容。Secret 至关于一个变量,可使私有变量不公开。
将生成的 public key
做为网站文件仓库 Settings > Deploy Keys
的 Deploy Key。Deploy Keys 中的公钥针对于当前仓库。
为何要用 SSH Key?
hexo g -d
时,用私钥 DEPLOY_KEY 加密,GitHub 用网站文件仓库的 Deploy Key 进行验证。下面是具体的 action.yml:
name: Deploy # workflow name on: [push] # 触发事件 jobs: build: # job1 id runs-on: ubuntu-latest # 运行环境为最新版 Ubuntu name: A job to deploy blog. steps: - name: Checkout # step1 获取源码 uses: actions/checkout@v1 # 使用 actions/checkout@v1 with: # 条件 submodules: true # Checkout private submodules(themes or something else). 当有子模块时切换分支? # Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.) 缓存压缩 node_modules,不用每次下载,使用时解压,能够加快工做流的执行过程,超过 7 天没有使用将删除压缩包。 - name: Cache node modules # step2 uses: actions/cache@v1 id: cache with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - name: Install Dependencies # step3 name if: steps.cache.outputs.cache-hit != 'true' # 若是变量 cache-hit 不等于 true run: npm install # 安装 node modules 相关依赖 # Deploy hexo blog website. - name: Deploy # step4 id: deploy uses: sma11black/hexo-action@v1.0.0 with: deploy_key: ${{ secrets.DEPLOY_KEY }} user_name: your github username user_email: your github useremail
可能出现如下 3 三种状况:
一、
Host key verification failed fatal: Could not read from remote repository.
出现此问题请检查 站点配置文件
,看 deploy 是否存在出 GitHub 之外的仓库,如 Coding 。
解决方式:去除 GitHub 之外的仓库。
二、
Load key "/root/ssh/id_rsa": invalid format gitagithub.com: Permission denied (publickey) fatal: Could not read from remote repository.
出现此问题请检查 SSH Key,看 Private Key 是否正确且完整的复制
三、
ERROR Local hexo not found in /github/workspace ERROR Try running: 'npm install hexo --save'
解决方式:将 uses: sma11black/hexo-action@v1.0.0
改成 uses: deppwang/hexo-aciton@v1.0.1
,@v1.0.1
在 entrypoint.sh
中 hexo g -d
前添加了命令 npm install hexo --save
关于 Action 具体如何执行,可结合运行日志理解。