Generate WebP Photos List with CDN Mirror #163
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate WebP Photos List with CDN Mirror | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # 每天UTC 0点执行(北京时间8点) | |
| push: | |
| paths: | |
| - 'img/**' # 当img目录有变更时触发 | |
| workflow_dispatch: # 允许手动触发 | |
| jobs: | |
| generate-list: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| ref: main | |
| - name: Setup environment | |
| run: | | |
| sudo timedatectl set-timezone Asia/Shanghai | |
| sudo apt-get update | |
| sudo apt-get install -y tree | |
| - name: Generate CDN links for WebP | |
| id: generate-links | |
| run: | | |
| mkdir -p tmp | |
| echo "开始处理 WebP 图片..." | |
| # 1. 查找所有webp图片(包括子目录) | |
| find img/ -type f -iname "*.webp" > tmp/all_webp.txt | |
| # 记录找到的文件数量 | |
| FILE_COUNT=$(wc -l < tmp/all_webp.txt) | |
| echo "找到 $FILE_COUNT 个 WebP 文件" | |
| # 2. 生成带国内CDN镜像的链接(使用分支名而不是commit SHA) | |
| echo "生成 CDN 链接..." | |
| cat tmp/all_webp.txt | while read -r file; do | |
| # 移除前面的img/路径 | |
| relative_path="${file#img/}" | |
| # 使用分支名(master)而不是commit SHA | |
| echo "https://cdn.mengze.vip/gh/${{ github.repository }}@master/img/$relative_path" | |
| done > photos.txt | |
| # 3. 添加统计信息 | |
| LINK_COUNT=$(wc -l < photos.txt) | |
| echo "生成 $LINK_COUNT 个有效链接" | |
| # 4. 验证生成的链接格式 | |
| echo "验证链接格式..." | |
| if grep -q "@master/img/" photos.txt; then | |
| echo "链接格式正确" | |
| else | |
| echo "错误:链接格式不正确" | |
| exit 1 | |
| fi | |
| # 保存统计信息 | |
| echo "FILE_COUNT=$FILE_COUNT" >> $GITHUB_ENV | |
| echo "LINK_COUNT=$LINK_COUNT" >> $GITHUB_ENV | |
| # 显示示例链接 | |
| echo "示例链接:" | |
| head -3 photos.txt | |
| - name: Create README info | |
| run: | | |
| echo "# 图片链接列表" > info.md | |
| echo "最后更新: $(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S')" >> info.md | |
| echo "图片数量: ${{ env.FILE_COUNT }}" >> info.md | |
| echo "有效链接: ${{ env.LINK_COUNT }}" >> info.md | |
| echo "" >> info.md | |
| echo "## 示例链接" >> info.md | |
| head -3 photos.txt | while read url; do | |
| echo "- [$url]($url)" >> info.md | |
| done | |
| - name: Commit and push changes | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "Auto: 更新图片列表 (${{ env.LINK_COUNT }} 个链接)" | |
| file_pattern: | | |
| photos.txt | |
| info.md | |
| commit_user_name: 'GitHub Actions Bot' | |
| commit_user_email: '[email protected]' | |
| commit_author: 'GitHub Actions <[email protected]>' | |
| add: '--all' | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Upload photos.txt as artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: photos-list | |
| path: | | |
| photos.txt | |
| info.md | |
| retention-days: 7 | |
| - name: Notify on failure | |
| if: ${{ failure() }} | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: '⚠️ 图片列表生成失败', | |
| body: `工作流运行失败!请检查: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}` | |
| }) |