mirror of
https://github.com/zopiya/blog.git
synced 2025-11-03 12:16:46 +08:00
Add GitHub Actions workflow for automated Hugo blog deployment
This commit is contained in:
65
.github/workflows/deploy-blog.yml
vendored
Normal file
65
.github/workflows/deploy-blog.yml
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
name: Deploy Hugo Blog
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 6 * * *' # 每天早上 6 点(UTC)执行一次
|
||||
push:
|
||||
branches:
|
||||
- main # 当 main 分支有新提交时执行
|
||||
|
||||
jobs:
|
||||
build:
|
||||
# 使用默认环境,并在最新的 Ubuntu 系统上运行
|
||||
environment: default
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
# 1. 克隆当前博客仓库代码(包含完整历史记录)
|
||||
- name: 克隆当前博客仓库
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
# 2. 安装 Hugo 静态站点生成器(extended 版本支持 Sass/SCSS 等扩展功能)
|
||||
- name: 安装 Hugo
|
||||
uses: peaceiris/actions-hugo@v3
|
||||
with:
|
||||
hugo-version: '0.145.0'
|
||||
extended: true
|
||||
|
||||
# 3. 克隆私有 wiki 仓库,作为博客内容的来源
|
||||
- name: 克隆私有 wiki 仓库
|
||||
run: |
|
||||
# 遇到错误立即退出,并确保管道中任一命令失败时退出
|
||||
set -eo pipefail
|
||||
# 清除旧的 wiki 临时目录,避免残留影响
|
||||
rm -rf /tmp/wiki
|
||||
# 使用 secrets 中存储的认证信息从私有仓库克隆 wiki 到 /tmp/wiki
|
||||
git clone https://${{ secrets.GITEA_REPO_READ }}@git.7wate.org/zhouzhongping/wiki.git /tmp/wiki
|
||||
|
||||
# 4. 将 wiki 中的博客内容拷贝到 Hugo 的 content/posts 目录中
|
||||
- name: 拷贝 wiki 内容到 content/posts
|
||||
run: |
|
||||
set -eo pipefail
|
||||
WIKI_BLOG_DIR="/tmp/wiki/Personal/Blog"
|
||||
TARGET_DIR="content/posts"
|
||||
if [ -d "$WIKI_BLOG_DIR" ]; then
|
||||
cp -r "$WIKI_BLOG_DIR"/* "$TARGET_DIR"/ || { echo "拷贝失败"; exit 1; }
|
||||
else
|
||||
echo "目录 $WIKI_BLOG_DIR 不存在,拷贝失败"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 5. 使用 Hugo 构建静态站点,并启用资源压缩优化
|
||||
- name: 构建 Hugo 静态站点
|
||||
run: |
|
||||
set -eo pipefail
|
||||
hugo --minify
|
||||
|
||||
# 6. 使用 gh-pages Action 将生成的静态站点部署到指定分支(此处为 html 分支)
|
||||
- name: 部署站点到 gh-pages 分支
|
||||
uses: peaceiris/actions-gh-pages@v3
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
publish_branch: html
|
||||
publish_dir: ./public
|
||||
Reference in New Issue
Block a user