|
| 1 | +name: Convert PNG to WebP, Update Markdown, and Comment |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['**'] |
| 6 | + pull_request: |
| 7 | + |
| 8 | +jobs: |
| 9 | + convert-images: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Install cwebp |
| 17 | + run: sudo apt-get update && sudo apt-get install -y webp |
| 18 | + |
| 19 | + - name: Convert and Replace |
| 20 | + id: convert |
| 21 | + run: | |
| 22 | + echo "converted_images=" >> $GITHUB_OUTPUT |
| 23 | + echo "updated_docs=" >> $GITHUB_OUTPUT |
| 24 | +
|
| 25 | + mkdir -p .tmp_logs |
| 26 | + touch .tmp_logs/converted.txt .tmp_logs/updated.txt |
| 27 | +
|
| 28 | + find . -type f -name "*.png" ! -name "*.webp.png" | while read img; do |
| 29 | + webp="${img%.png}.webp" |
| 30 | + echo "Converting $img → $webp" |
| 31 | + cwebp -q 80 "$img" -o "$webp" && rm "$img" && echo "$img → $webp (deleted PNG)" >> .tmp_logs/converted.txt |
| 32 | +
|
| 33 | + escaped_img=$(printf '%s\n' "$img" | sed 's|[][\.*^$(){}+?|]|\\&|g') |
| 34 | + escaped_webp=$(printf '%s\n' "$webp" | sed 's|[][\.*^$(){}+?|]|\\&|g') |
| 35 | +
|
| 36 | + grep -rl --include="*.md" "$img" . | while read md; do |
| 37 | + sed -i "s|$escaped_img|$escaped_webp|g" "$md" && echo "$md" >> .tmp_logs/updated.txt |
| 38 | + done |
| 39 | + done |
| 40 | +
|
| 41 | + echo "converted_images<<EOF" >> $GITHUB_OUTPUT |
| 42 | + cat .tmp_logs/converted.txt >> $GITHUB_OUTPUT |
| 43 | + echo "EOF" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + echo "updated_docs<<EOF" >> $GITHUB_OUTPUT |
| 46 | + sort -u .tmp_logs/updated.txt >> $GITHUB_OUTPUT |
| 47 | + echo "EOF" >> $GITHUB_OUTPUT |
| 48 | +
|
| 49 | + - name: Commit changes |
| 50 | + run: | |
| 51 | + git config user.name "github-actions" |
| 52 | + git config user.email "[email protected]" |
| 53 | + git add '**/*.webp' '**/*.md' |
| 54 | + git commit -m "Convert PNGs to WebP, delete originals, and update markdown references" || echo "No changes to commit" |
| 55 | + git push || echo "No changes to push" |
| 56 | +
|
| 57 | + - name: Comment on PR or commit |
| 58 | + env: |
| 59 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + run: | |
| 61 | + CONVERTED="${{ steps.convert.outputs.converted_images }}" |
| 62 | + UPDATED="${{ steps.convert.outputs.updated_docs }}" |
| 63 | +
|
| 64 | + BODY="### 🖼️ PNG to WebP Conversion Report |
| 65 | +
|
| 66 | +**Converted & Deleted:** |
| 67 | +\`\`\` |
| 68 | +$CONVERTED |
| 69 | +\`\`\` |
| 70 | + |
| 71 | +**Markdown Files Updated:** |
| 72 | +\`\`\` |
| 73 | +$UPDATED |
| 74 | +\`\`\` |
| 75 | +" |
| 76 | +
|
| 77 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 78 | + PR_URL="${{ github.event.pull_request.comments_url }}" |
| 79 | + else |
| 80 | + PR_URL="https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments" |
| 81 | + fi |
| 82 | + |
| 83 | + echo "$BODY" | jq -Rs '{body: .}' | curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -d @- "$PR_URL" |
0 commit comments