用 GitHub Actions 自动化发布Hexo网站到 GitHub Pages

本文首发于个人博客 用 GitHub Actions 自动化发布Hexo网站到 GitHub Pages 欢迎来个人小站支持一下html

说实话不用每次都执行一大长串部署指令真的香啊!node

准备Hexo网站

  1. 在本地创建一个Hexo站点,能够参考官方快速开始文档
  2. 创建两个GitHub仓库,分别叫blog(私有的)和你的GitHub用户名.github.io(共有的)。前者用来存储博客源文件,后者用于挂载GitHub Pages。
  3. 将本地的博客源文件推送到blog仓库。

准备秘钥

为了方便运行GitHub Actions时登陆GitHub帐号,咱们使用SSH方式登陆。git

使用ssh-keygen生成一组公私秘钥对github

ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-actions-deploy
复制代码

Settings->SSH and GPG keys添加刚刚生成的公钥,名称随意。 在blog仓库的Settings->Secrets里添加刚刚生成的私钥,名称为 ACTION_DEPLOY_KEYshell

配置Hexo的_config.yml

添加部署配置。npm

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
- type: git
 repo: git@github.com:bulabula.git # 使用仓库的ssh地址
 branch: master
复制代码

配置 GitHub Actions

blog仓库的Actions选项卡下点击新建workflow,编写以下配置。ubuntu

name: Deploy Blog

on: [push] # 当有新push时运行

jobs:
 build: # 一项叫作build的任务

 runs-on: ubuntu-latest # 在最新版的Ubuntu系统下运行
    
 steps:
 - name: Checkout # 将仓库内master分支的内容下载到工做目录
 uses: actions/checkout@v1 # 脚原本自 https://github.com/actions/checkout
      
 - name: Use Node.js 10.x # 配置Node环境
 uses: actions/setup-node@v1 # 配置脚原本自 https://github.com/actions/setup-node
 with:
 node-version: "10.x"
    
 - name: Setup Hexo env
 env:
 ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }}
 run: | # set up private key for deploy mkdir -p ~/.ssh/ echo "$ACTION_DEPLOY_KEY" | tr -d '\r' > ~/.ssh/id_rsa # 配置秘钥 chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts # set git infomation git config --global user.name 'bulabula' # 换成你本身的邮箱和名字 git config --global user.email 'bulabula@athorx.com' # install dependencies npm i -g hexo-cli # 安装hexo npm i  - name: Deploy
 run: | # publish hexo generate && hexo deploy # 执行部署程序 复制代码

本文首发于个人博客 用 GitHub Actions 自动化发布Hexo网站到 GitHub Pages 欢迎来个人小站支持一下hexo

相关文章
相关标签/搜索