166 lines
6.0 KiB
YAML
166 lines
6.0 KiB
YAML
# GitHub Actions: 字体子集化自动化
|
|
# 当内容更新时自动提取字符并生成优化的子集字体
|
|
|
|
name: Font Subset Optimization
|
|
|
|
on:
|
|
# 当推送到main分支且content目录有变更时触发
|
|
push:
|
|
branches:
|
|
- main
|
|
paths:
|
|
- 'content/**'
|
|
- 'layouts/**'
|
|
|
|
# 允许手动触发
|
|
workflow_dispatch:
|
|
inputs:
|
|
force_rebuild:
|
|
description: '强制重新生成子集字体'
|
|
required: false
|
|
default: 'false'
|
|
type: boolean
|
|
|
|
# 可选:每周自动运行一次(检查是否需要更新)
|
|
schedule:
|
|
- cron: '0 2 * * 1' # 每周一凌晨2点
|
|
|
|
# 限制同时运行的工作流数量
|
|
concurrency:
|
|
group: font-subset-${{ github.ref }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
subset-fonts:
|
|
name: Subset Fonts
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 30
|
|
|
|
# 只在主仓库上运行(fork不运行)
|
|
if: github.repository == 'zqlit/blog'
|
|
|
|
steps:
|
|
# 1. 检出代码
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0 # 获取完整历史用于比较
|
|
|
|
# 2. 设置Python环境
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: '3.11'
|
|
cache: 'pip'
|
|
|
|
# 3. 安装Python依赖
|
|
- name: Install dependencies
|
|
run: |
|
|
python -m pip install --upgrade pip
|
|
pip install fonttools brotli
|
|
|
|
# 4. 检查是否需要更新(可选)
|
|
- name: Check if update needed
|
|
id: check
|
|
run: |
|
|
# 检查内容文件是否有变更
|
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
|
echo "needs_update=true" >> $GITHUB_OUTPUT
|
|
else
|
|
# 比较content目录的变更
|
|
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- content/ layouts/ || echo "")
|
|
if [ -n "$CHANGED_FILES" ]; then
|
|
echo "needs_update=true" >> $GITHUB_OUTPUT
|
|
echo "Changed files: $CHANGED_FILES"
|
|
else
|
|
echo "needs_update=false" >> $GITHUB_OUTPUT
|
|
echo "No content changes detected"
|
|
fi
|
|
fi
|
|
|
|
# 5. 构建Hugo站点(用于提取字符)
|
|
- name: Build Hugo site
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
uses: peaceiris/actions-hugo@v2
|
|
with:
|
|
hugo-version: 'latest'
|
|
extended: true
|
|
|
|
- name: Build
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: hugo --destination=public --minify
|
|
|
|
# 6. 运行字体子集化脚本
|
|
- name: Subset fonts
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: |
|
|
python scripts/subset-font-safe.py
|
|
|
|
# 7. 检查子集字体是否真的优化了
|
|
- name: Verify optimization
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: |
|
|
ORIGINAL_SIZE=$(stat -f%z themes/Ying/static/font/zql-v2.woff2 2>/dev/null || stat -c%s themes/Ying/static/font/zql-v2.woff2)
|
|
SUBSET_SIZE=$(stat -f%z themes/Ying/static/font/zql-v2-subset.woff2 2>/dev/null || stat -c%s themes/Ying/static/font/zql-v2-subset.woff2)
|
|
|
|
echo "Original font size: $ORIGINAL_SIZE bytes"
|
|
echo "Subset font size: $SUBSET_SIZE bytes"
|
|
|
|
# 检查子集字体是否真的更小
|
|
if [ $SUBSET_SIZE -ge $ORIGINAL_SIZE ]; then
|
|
echo "⚠️ Warning: Subset font is not smaller than original"
|
|
echo "Skipping commit..."
|
|
exit 0
|
|
fi
|
|
|
|
REDUCTION=$((ORIGINAL_SIZE - SUBSET_SIZE))
|
|
PERCENTAGE=$((REDUCTION * 100 / ORIGINAL_SIZE))
|
|
echo "✅ Font optimized: reduced $REDUCTION bytes ($PERCENTAGE%)"
|
|
|
|
# 8. 提交更改(如果有优化)
|
|
- name: Commit changes
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: |
|
|
# 配置git
|
|
git config --local user.email "github-actions[bot]@users.noreply.github.com"
|
|
git config --local user.name "github-actions[bot]"
|
|
|
|
# 检查是否有变更
|
|
if git diff --quiet; then
|
|
echo "No changes to commit"
|
|
exit 0
|
|
fi
|
|
|
|
# 添加变更的文件
|
|
git add themes/Ying/static/font/zql-v2-subset.*
|
|
git add themes/Ying/static/font/used_chars.txt
|
|
|
|
# 提交
|
|
git commit -m "chore: update font subset (automated)
|
|
|
|
- Updated font subset based on latest content
|
|
- Subset characters: $(wc -c < themes/Ying/static/font/used_chars.txt) chars
|
|
- Font size: $(stat -c%s themes/Ying/static/font/zql-v2-subset.woff2 2>/dev/null || echo 'N/A') bytes"
|
|
|
|
# 9. 推送更改
|
|
- name: Push changes
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: |
|
|
git push origin main
|
|
|
|
# 10. 创建摘要
|
|
- name: Create summary
|
|
if: steps.check.outputs.needs_update == 'true' || github.event.inputs.force_rebuild == 'true'
|
|
run: |
|
|
echo "## 🔤 Font Subset Optimization" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "✅ Font subset updated successfully!" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "### 📊 Statistics" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Characters in subset:** $(wc -c < themes/Ying/static/font/used_chars.txt)" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Subset font size:** $(stat -c%s themes/Ying/static/font/zql-v2-subset.woff2 2>/dev/null || echo 'N/A') bytes" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "### 📝 Triggered by" >> $GITHUB_STEP_SUMMARY
|
|
echo "- Event: ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "- Commit: ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
|