Skip to content

Backfill Blog Images #13

Backfill Blog Images

Backfill Blog Images #13

name: Backfill Blog Images
on:
schedule:
- cron: "0 2 * * 1" # 每周一UTC 2点(北京时间10点)
workflow_dispatch: # 允许手动触发
jobs:
backfill-images:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.MY_GITHUB_TOKEN }}
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
cd scripts
pip install -r requirements.txt
- name: Check rate limits
id: rate-check
run: |
# 检查今日处理数量
TODAY=$(date +%Y-%m-%d)
DAILY_COUNT=$(git log --since="$TODAY 00:00:00" --grep="Auto-generate blog images" --oneline | wc -l)
# 检查小时处理数量
HOUR=$(date +%Y-%m-%dT%H)
HOURLY_COUNT=$(git log --since="$HOUR:00:00" --grep="Auto-generate blog images" --oneline | wc -l)
echo "daily_count=$DAILY_COUNT" >> $GITHUB_OUTPUT
echo "hourly_count=$HOURLY_COUNT" >> $GITHUB_OUTPUT
# 检查是否超过限制
if [ $DAILY_COUNT -ge 5 ]; then
echo "can_process=false" >> $GITHUB_OUTPUT
echo "Daily limit reached (5 images per day)"
elif [ $HOURLY_COUNT -ge 1 ]; then
echo "can_process=false" >> $GITHUB_OUTPUT
echo "Hourly limit reached (1 image per hour)"
else
echo "can_process=true" >> $GITHUB_OUTPUT
echo "Rate limits OK"
fi
- name: Generate images for historical blogs
if: steps.rate-check.outputs.can_process == 'true'
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
run: |
cd scripts
python generate_image.py --backfill
- name: Create images directory
run: |
mkdir -p static/images/articles
echo "Created static/images/articles directory"
- name: Commit generated images
if: always() && steps.rate-check.outputs.can_process == 'true'
env:
GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
run: |
# 检查是否有新的图片文件
if [ -d "static/images/articles" ] && [ "$(ls -A static/images/articles 2>/dev/null)" ]; then
echo "Found new images to commit"
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git
git add static/images/articles/
git commit -m "Auto-generate blog images (backfill) [skip ci]" || echo "No changes to commit"
git push
else
echo "No new images to commit"
fi
- name: Create summary
if: always()
run: |
echo "## Blog Image Backfill Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Daily processed:** ${{ steps.rate-check.outputs.daily_count }}/5" >> $GITHUB_STEP_SUMMARY
echo "**Hourly processed:** ${{ steps.rate-check.outputs.hourly_count }}/1" >> $GITHUB_STEP_SUMMARY
echo "**Can process:** ${{ steps.rate-check.outputs.can_process }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Generated Images:" >> $GITHUB_STEP_SUMMARY
if [ -d "static/images/articles" ]; then
ls -la static/images/articles/ | tail -n +2 | while read line; do
echo "- $line" >> $GITHUB_STEP_SUMMARY
done
else
echo "No images directory found" >> $GITHUB_STEP_SUMMARY
fi