如何使用github actions 自动部署Hexo博客

一、创建github仓库

创建两个github仓库,一个共有仓库和一个私有仓库。

  • 私有仓库用来存储Hexo项目源代码
    用master分支来存放项目源代码
  • 公有仓库用来存储编译之后的静态页面
    用gh-pages分支来存储静态页面

当私有仓库的master分支有内容push进来时,GitHub Actions 自动编译并且部署到公有仓库的gh-pages分支。

创建GitHub Token

创建一个有repo和workflow权限的GitHb Token

创建repository secret

步骤:私有仓库-> settings->Srcrets->New repository secrets。

新创建的secrets key在Actions配置文件要用到,因此变量名字要保持一致。

二、添加Actions配置文件

1.在你的hexo项目根目录下创建.github文件夹。
2.在.github文件夹下继续创建workflows文件夹。
2.在workflows文件夹下创建hexo-deploy.yml文件。

name: deploying Hexo project to GitHub pages
on:
  push:
    branches:
      - master # master 分支有 push 行为时就触发这个 action

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Build and Deploy
        uses: theme-keep/hexo-deploy-github-pages-action@master # 使用专门部署 Hexo 到 GitHub pages 的 action
        env:
          PERSONAL_TOKEN: ${{ secrets.HEXO_DEPLOY }} # secret 名
          PUBLISH_REPOSITORY: XPoet/keep-blog # 公共仓库,格式:GitHub 用户名/仓库名
          BRANCH: gh-pages # 分支,填 gh-pages 就行
          PUBLISH_DIR: ./public # 部署 public 目录下的文件

或者不使用theme-keep/hexo-deploy-github-pages-action@master
也是可以实现的

name: Deploy Hexo Project to GitHub Pages
on:
  push:
    branches:
      - master  # 当 master 分支有 push 行为时触发这个 action

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'  # 指定 Node.js 版本

      - name: Install Dependencies # 安装项目依赖
        run: npm install  

      - name: Install hexo-cli   # 全局安装hexo-cli 
        run: |
          npm install -g hexo-cli 

      - name: Build Hexo # 生成 Hexo 页面
        run: |
          hexo clean
          hexo generate  

      - name: Deploy to GitHub Pages
        env: 
          HEXO_DEPLOY: ${{ secrets.HEXO_DEPLOY }}

        run: |
          cd public  # 进入生成的 Hexo 页面目录
          git init  # 初始化新的 Git 仓库
          git config --global user.name "${{ github.actor }}"  # 动态获取 GitHub 用户名
          git config --global user.email "${{ github.actor }}@users.noreply.github.com"  # 使用 GitHub 提供的 noreply 邮箱
          git config --global --add safe.directory $GITHUB_WORKSPACE  # 添加当前工作目录为安全目录
          git remote add origin https://${{ secrets.HEXO_DEPLOY }}@github.com/iword-one/iword-one.github.io.git  # 添加公有仓库的远程地址
          git checkout --orphan gh-pages  # 切换到 gh-pages 分支
          git add .
          git commit --allow-empty -m "Deploy Hexo pages"
          git push -u --force origin gh-pages  # 强制推送到公有仓库的 gh-pages 分支


三、触发自动部署流程

$ hexo init my-blog
$ cd my-blog
$ npm install
$ git init
$ git remote add origin 私有仓库的地址
$ hexo g
$ hexo s

git add .
git commit -m "deploy hexo source"
git push -u origin master

GitHub Actions 检测到 master 分支有内容 push 进来,会自动执行 action 配置文件的命令,将 Hexo 项目编译成静态页面,然后部署到公共仓库的 gh-pages 分支。
在私有仓库的Actions可以查看配置的action详细信息

posted @ 2024-10-20 15:05  林汉州win  阅读(7)  评论(0编辑  收藏  举报