使用 Hugo + GitHub Actions自动部署博客

slug
hugo-github-actions
status
Published
tags
hugo
github
summary
type
Post
使用 GitHub Actions 将生成的静态页面发布到 Github Pages,首先要在本地生成 ssh deploy key
ssh-keygen -t rsa -b 4096 -C "your.email" -f gh-pages -N "" 在 GitHub 网站中打开项目的设置页面,将刚生成的 ssh 公钥添加到 Deploy Keys 并选择 Allow write access,然后将私钥添加到 Secrets,可以命名为 ACTIONS_DEPLOY_KEY,然后再把写好的角本放到仓库根目录下的 .github/workflow/deploy.yml 文件中,角本内容可以引用别人写好的。
name: github pages

当 hugo-branch 分支发生 push 事件时执行下面任务

 
name: github pages

# 当 hugo-branch 分支发生 push 事件时执行下面任务
on:
  push:
    branches:
      - hugo-branch

env:
  ACTIONS_ALLOW_UNSECURE_COMMANDS: true

jobs:
  build-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true

    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2.3.1
      with:
        hugo-version: '0.61.0'

    - name: Build
      run: hugo --minify

    - name: add nojekyll
      run: touch ./public/.nojekyll

    - name: Deploy
      uses: peaceiris/actions-gh-pages@v2.5.1
      env:
        ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
        PUBLISH_BRANCH: master
        PUBLISH_DIR: ./public
 

© 精灵猫 2021 - 2025