惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
G
Google Developers Blog
L
LangChain Blog
腾讯CDC
大猫的无限游戏
大猫的无限游戏
U
Unit 42
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
The GitHub Blog
The GitHub Blog
博客园_首页
GbyAI
GbyAI

eallion's Blog

春假清明自驾游 Ubuntu 25.10 安装和配置 秋假 彩礼 2025 博客变化 预制菜 联邦礼仪之一 重拾写博客的乐趣 少儿 TED - 时间管理大师 n8n 之同步博客到 Mastodon n8n 之备份 Mastodon 嘟文 如何备份 Mastodon Docker 部署 Mastodon NAS 折腾记 Windows 11 安装软件 博客排版 - 挤压中文标点符号 Hugo 博客集成 Mastodon 独立博客自省问卷 15 题 Chrome 插件更新:网址净化器 在 Hugo 中使用 Shiki 炒菜万能公式 uBlacklist 订阅合集 读《中文互联网正在加速崩塌》 CSS 和 JS 实现博客热力图 受灾小记 那,他吃什么?! Mastodon 同步到 Memos Hugo 外部链接跳转提示页面 联邦宇宙及 Mastodon 简介 2024 博客变化
GitHub Actions 自动部署 Hexo 脚本
Charles Chin · 2019-12-29 · via eallion's Blog

# 前言

网上虽然有很多通过 GitHub Actions 自动部署 Hexo 的教程,但都有各种各样的问题。
主要问题还是 Workflow 脚本没有写正确,比如插件部分。

# 步骤

# 1、生成密钥对(这个也不会的话,方法自行 Google)

ssh-keygen -t rsa -b 4096 -f ~/.ssh/GitHub-actions-deploy

然后会获得一个公钥和私钥。

# 2、在 GitHub Pages 所在的仓库中添加 “公钥”

找到仓库的 Settings - Deploye keys - Add deploy key

  • Title 填入:ACTION_DEPLOY_KEY
  • Key 填入:# 步骤 1 生成的密钥对中的公钥
    勾上 Allow write access

# 3、在存放 Hexo 源文件的仓库中添加 “私钥”

PS: 跟步骤 2 中的仓库可能是同一个,也可能不是同一个。根据自己的选型设置。

找到仓库的 Settings - Secrets - Add a new secret

  • Name 填入:ACTION_DEPLOY_KEY
  • Value 填入:# 步骤 1 生成的密钥对中的私钥

# 4、修改一下 Actions 脚本

下文贴出的 Actions 的 Wordflows 脚本中,其中 3 个地方要改为自己的信息:

git config --global user.name "eallion"
git config --global user.email "eallion@eallion.com"
git clone https://github.com/eallion/eallion.GitHub.io .deploy_git

# 5、其他注意事项

  • 脚本中插件部分可按自己实际用的插件删改,npm ls --depth 0 可查看自己安装了哪些插件
  • 如果没有用到 hexo douban 插件,部署命令:
    hexo g && hexo douban && hexo deploy 可改为 hexo g -d

另外一个建议是,所使用的 theme 中的 主题目录git subtree 添加为子项目去维护。
这样在多环境多终端发布文章时,不会 clonepull 一个空 theme

# 6、git push

文章写好后,不需要在本地构建,只需要 git push 到 GitHub 仓库即可自动部署。
其中部署方式在 Hexo 根目录的 _config.yml 中配置。

# 脚本配置

注意修改其中提到的几个地方

name: 自动部署 Hexo

#on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]

    steps:
      - name: 开始运行
        uses: actions/checkout@v1

      - name: 设置 Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: 配置 Git 环境
        env:
          ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name "eallion"
          git config --global user.email "eallion@eallion.com"

      - name: 安装 Hexo CI
        run: |
          export TZ='Asia/Shanghai'
          npm i -g hexo-cli
          npm i

      - name: 安装插件
        run: |
          npm install hexo-renderer-pug --save
          npm install hexo-renderer-stylus --save
          npm install hexo-deployer-git --save
          npm install hexo-generator-search --save
          npm install hexo-douban --save
          npm install hexo-generator-feed --save
          npm install hexo-abbrlink --save
          npm install hexo-wordcount --save

      - name: 部署博客
        run: |
          rm -rf .deploy_git
          hexo g && hexo douban && hexo deploy
          rm ~/.ssh/id_rsa

# 部署截图