如何使用 GitHub Action 自动部署 Hexo 博客


Step 1

把 Hexo 代码放到 GitHub 的仓库里

Step 2

.github/workflows 中创建一个 deploy.yml 文件

Step 3

在刚刚创建的文件中输入以下代码

# @author: xyz8848
# @see https://xyz8848.com/index.php/archives/11/
name: Hexo Blog Delopy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest 

    steps:
    # @see https://github.com/actions/checkout
    - name: Checkout Repository master branch
      uses: actions/checkout@master

    # @see https://github.com/actions/setup-node
    - name: Setup Node.js
      uses: actions/setup-node@master
      with:
        node-version: "14"

    - name: Setup Hexo
      run: |
        npm install hexo-cli -g

    - name: Deploy Hexo
      run: |
        hexo clean
        hexo generate
        hexo deploy

    - name: Setup Git Infomation
      run: |
        git config --global user.name '<你的GitHub用户名>'
        git config --global user.email '<你的GitHub邮箱>'

    - name: Deploy to GitHub Pages
      uses: crazy-max/ghaction-github-pages@v2
      with:
        # 部署到 gh-pages 分支
        target_branch: gh-pages
        # 部署目录为 Hexo 的默认输出目录
        build_dir: public
        # 自定义域名(CNAME)(可选)
        fqdn: 
        # 强制提交
        keep_history: true
      env:
        # @see https://docs.github.com/cn/actions/reference/authentication-in-a-workflow#about-the-github_token-secret
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Step 4

更改 deploy.yml 中的两个字段:

  • <你的GitHub用户名>
  • <你的GitHub邮箱>

Author: xyz8848
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source xyz8848 !
  TOC