抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >


使用 Github Actions 部署 Hexo

事件触发器

on:
workflow_dispatch:
push:
branches:
- main

检出仓库分支源码

- name: Checkout Repository main branch
uses: actions/checkout@v3
with:
submodules: "true"

安装 Node.js

- name: Setup Node.js 24.x
uses: actions/setup-node@main
with:
node-version: 24
registry-url: https://registry.npmjs.org

安装部署密钥

- name: Setup Deploy Private Key
env:
DEPLOY_KEY: ${{ secrets.TOKEN }}
NAME: ${{ secrets.NAME }}
EMAIL: ${{ secrets.EMAIL }}
run: |
mkdir -p ~/.ssh/
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "$EMAIL"
git config --global user.name "$NAME"

缓存 node modules

- name: Cache node modules
uses: actions/cache@v4
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-

安装依赖

- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install

生成站点静态资源

- name: Generate Public Files
run: |
# Restore last modified time
git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done
hexo clean && hexo g

部署到 Github Pages

- name: Deploy Github
uses: peaceiris/actions-gh-pages@v3
with:
deploy_key: ${{ secrets.TOKEN }}
publish_dir: ./public
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
external_repository: XXX/XXX.github.io
publish_branch: main

完整 Actions 脚本

.github/workflows/DeployToGithubPages.yml
name: Deploy To Github Pages

on:
workflow_dispatch:
push:
branches:
- main

jobs:
deploy:
if: ${{ contains(github.event.head_commit.message, 'draft')!= true }}
name: Deploy Hexo Public To Pages
runs-on: ubuntu-latest
env:
TZ: Asia/Shanghai
steps:
- name: Checkout Repository main branch
uses: actions/checkout@v3
with:
submodules: "true"
- name: Setup Node.js 24.x
uses: actions/setup-node@main
with:
node-version: 24
registry-url: https://registry.npmjs.org
- name: Setup Deploy Private Key
env:
DEPLOY_KEY: ${{ secrets.TOKEN }}
NAME: ${{ secrets.NAME }}
EMAIL: ${{ secrets.EMAIL }}
run: |
mkdir -p ~/.ssh/
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "$EMAIL"
git config --global user.name "$NAME"

- name: Install Globel Dependencies
run: |
npm install hexo-cli -g
- name: Cache node modules
uses: actions/cache@v4
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install
- name: Generate Public Files
run: |
# Restore last modified time
git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done
hexo clean && hexo g

- name: Deploy Github
uses: peaceiris/actions-gh-pages@v3
with:
deploy_key: ${{ secrets.TOKEN }}
publish_dir: ./public
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
external_repository: XXX/XXX.github.io
publish_branch: main

相关文档

https://docs.github.com/zh/actions

推荐阅读
Hexo Hexo 如何配置可信发布从 CI/CD 工作流发布 npm 包 如何配置可信发布从 CI/CD 工作流发布 npm 包 关于 Hexo 的文档链接 关于 Hexo 的文档链接 Hexo 命令及 Markdown 语法 Hexo 命令及 Markdown 语法 定时备份服务器/网站数据到Github私人仓库 定时备份服务器/网站数据到Github私人仓库 kafka的安装部署 kafka的安装部署

留言区

Are You A Robot?