Github Actions 自动部署Hexo

2020-01-14  本文已影响0人  hongweifuture

本文转载请注明来源,欢迎大家关注我的新博客

Hexo的自动部署

目前的主流方式:

说明

前一阵玩docker的时候用docker搭建了Hexo环境,感觉像Hexo的环境搭建使用docker好笨重

本次使用的是Github Actions,就是因为其简单、无需VPS、公有仓库免费、私有仓库每个月2000分钟、还能体验这个新功能,我采用的是deploy的方式,所以公有仓库就看你的需求了,可以是github page、gitlab、coding、gitee、vps等等,这里以github举例。

因为是deploy方式,所以要安装插件

npm install hexo-deployer-git --save

同时在Hexo项目根目录配置文件_config.yaml中配置

deploy:
  type: git
  repo: git@github.com:Github用户名/Github用户名.github.io.git
  branch: master

环境准备

这里是存放 Hexo 博客源码的

这里是用来 public 静态页面的

所以如果你还没有创建 Hexo ,请参考 官方快速开始文档

密钥准备

为了方便运行GitHub Actions时登录GitHub账号,我们使用SSH方式登录。就是要把设备的私钥交给GitHub Actions,公钥交给GitHub,需要去Settings里去配置。

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

ssh-keygen -t rsa -C "Github 的邮箱地址"

如 ssh-keygen -t rsa -C "123123123@gmail.com"

配置GitHub Actions

GitHub Actions 有一些自己的术语。

在blog仓库的Actions选项卡下点击新建workflow,名称默认或者自定义修改,编写如下配置。

# workflow name
name: Hexo Blog CI

# master branch on push, auto run
on: 
  push:
    branches:
      - master
      
jobs:
  build: 
    runs-on: ubuntu-latest 
        
    steps:
    # check it to your workflow can access it
    # from: https://github.com/actions/checkout
    - name: Checkout Repository master branch
      uses: actions/checkout@master 
      
    # from: https://github.com/actions/setup-node  
    - name: Setup Node.js 10.x 
      uses: actions/setup-node@master
      with:
        node-version: "10.x"
    
    - name: Setup Hexo Dependencies
      run: |
        npm install hexo-cli -g
        npm install
    
    - name: Setup Deploy Private Key
      env:
        HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
      run: |
        mkdir -p ~/.ssh/
        echo "$HEXO_DEPLOY_PRIVATE_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 'Github用户名' 
        git config --global user.email 'Github注册邮箱'
    - name: Deploy Hexo 
      run: |
        hexo clean
        hexo generate 
        hexo deploy

这些命令不需注释都可以看懂吧,如果想发布到VPS,只需执行run命令即可

上一篇 下一篇

猜你喜欢

热点阅读