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: Convert PNG to WebP, Update Markdown Refs, and Comment | |
on: | |
push: | |
branches: ['**'] | |
workflow_dispatch: | |
pull_request: | |
jobs: | |
convert-images: | |
if: false | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Install cwebp | |
run: sudo apt-get update && sudo apt-get install -y webp | |
- name: Convert and Replace | |
id: convert | |
run: | | |
echo "converted_images=" >> $GITHUB_OUTPUT | |
echo "updated_docs=" >> $GITHUB_OUTPUT | |
mkdir -p .tmp_logs | |
touch .tmp_logs/converted.txt .tmp_logs/updated.txt | |
find . -type f -name "*.png" ! -name "*.webp.png" | while read img; do | |
webp="${img%.png}.webp" | |
echo "Converting $img → $webp" | |
cwebp -q 80 "$img" -o "$webp" && rm "$img" && echo "$img → $webp (deleted PNG)" >> .tmp_logs/converted.txt | |
escaped_img=$(printf '%s\n' "$img" | sed 's|[][\.*^$(){}+?|]|\\&|g') | |
escaped_webp=$(printf '%s\n' "$webp" | sed 's|[][\.*^$(){}+?|]|\\&|g') | |
grep -rl --include="*.md" "$img" . | while read md; do | |
sed -i "s|$escaped_img|$escaped_webp|g" "$md" && echo "$md" >> .tmp_logs/updated.txt | |
done | |
done | |
echo "converted_images<<EOF" >> $GITHUB_OUTPUT | |
cat .tmp_logs/converted.txt >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
echo "updated_docs<<EOF" >> $GITHUB_OUTPUT | |
sort -u .tmp_logs/updated.txt >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Commit changes | |
run: | | |
git config user.name "github-actions" | |
git config user.email "[email protected]" | |
git add '**/*.webp' '**/*.md' | |
git commit -m "Convert PNGs to WebP, delete originals, and update markdown references" || echo "No changes to commit" | |
git push || echo "No changes to push" | |
- name: Comment on PR or commit | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
CONVERTED="${{ steps.convert.outputs.converted_images }}" | |
UPDATED="${{ steps.convert.outputs.updated_docs }}" | |
# Create comment body using heredoc | |
BODY=$(cat <<'EOF' | |
### 🖼️ PNG to WebP Conversion Report | |
**Converted & Deleted:** | |
``` | |
CONVERTED_PLACEHOLDER | |
``` | |
**Markdown Files Updated:** | |
``` | |
UPDATED_PLACEHOLDER | |
``` | |
EOF | |
) | |
# Replace placeholders with actual values | |
BODY="${BODY//CONVERTED_PLACEHOLDER/$CONVERTED}" | |
BODY="${BODY//UPDATED_PLACEHOLDER/$UPDATED}" | |
if [ "${{ github.event_name }}" = "pull_request" ]; then | |
PR_URL="${{ github.event.pull_request.comments_url }}" | |
else | |
PR_URL="https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments" | |
fi | |
echo "$BODY" | jq -Rs '{body: .}' | curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -d @- "$PR_URL" |