Skip to content

Commit cb05302

Browse files
authored
Update convert-pngs.yml
1 parent 3ae6478 commit cb05302

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

.github/workflows/convert-pngs.yml

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,78 @@
1-
name: Convert PNG to WebP, Update Markdown, and Comment
2-
1+
name: Convert PNG to WebP, Update Markdown Refs, and Comment
32
on:
43
push:
54
branches: ['**']
5+
workflow_dispatch:
66
pull_request:
7-
87
jobs:
98
convert-images:
109
runs-on: ubuntu-latest
11-
1210
steps:
1311
- name: Checkout code
1412
uses: actions/checkout@v4
15-
1613
- name: Install cwebp
1714
run: sudo apt-get update && sudo apt-get install -y webp
18-
1915
- name: Convert and Replace
2016
id: convert
2117
run: |
2218
echo "converted_images=" >> $GITHUB_OUTPUT
2319
echo "updated_docs=" >> $GITHUB_OUTPUT
24-
2520
mkdir -p .tmp_logs
2621
touch .tmp_logs/converted.txt .tmp_logs/updated.txt
27-
2822
find . -type f -name "*.png" ! -name "*.webp.png" | while read img; do
2923
webp="${img%.png}.webp"
3024
echo "Converting $img → $webp"
3125
cwebp -q 80 "$img" -o "$webp" && rm "$img" && echo "$img → $webp (deleted PNG)" >> .tmp_logs/converted.txt
32-
3326
escaped_img=$(printf '%s\n' "$img" | sed 's|[][\.*^$(){}+?|]|\\&|g')
3427
escaped_webp=$(printf '%s\n' "$webp" | sed 's|[][\.*^$(){}+?|]|\\&|g')
35-
3628
grep -rl --include="*.md" "$img" . | while read md; do
3729
sed -i "s|$escaped_img|$escaped_webp|g" "$md" && echo "$md" >> .tmp_logs/updated.txt
3830
done
3931
done
40-
4132
echo "converted_images<<EOF" >> $GITHUB_OUTPUT
4233
cat .tmp_logs/converted.txt >> $GITHUB_OUTPUT
4334
echo "EOF" >> $GITHUB_OUTPUT
44-
4535
echo "updated_docs<<EOF" >> $GITHUB_OUTPUT
4636
sort -u .tmp_logs/updated.txt >> $GITHUB_OUTPUT
4737
echo "EOF" >> $GITHUB_OUTPUT
48-
4938
- name: Commit changes
5039
run: |
5140
git config user.name "github-actions"
5241
git config user.email "[email protected]"
5342
git add '**/*.webp' '**/*.md'
5443
git commit -m "Convert PNGs to WebP, delete originals, and update markdown references" || echo "No changes to commit"
5544
git push || echo "No changes to push"
56-
5745
- name: Comment on PR or commit
5846
env:
5947
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6048
run: |
6149
CONVERTED="${{ steps.convert.outputs.converted_images }}"
6250
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-
51+
52+
# Create comment body using heredoc
53+
BODY=$(cat <<'EOF'
54+
### 🖼️ PNG to WebP Conversion Report
55+
56+
**Converted & Deleted:**
57+
```
58+
CONVERTED_PLACEHOLDER
59+
```
60+
61+
**Markdown Files Updated:**
62+
```
63+
UPDATED_PLACEHOLDER
64+
```
65+
EOF
66+
)
67+
68+
# Replace placeholders with actual values
69+
BODY="${BODY//CONVERTED_PLACEHOLDER/$CONVERTED}"
70+
BODY="${BODY//UPDATED_PLACEHOLDER/$UPDATED}"
71+
7772
if [ "${{ github.event_name }}" = "pull_request" ]; then
7873
PR_URL="${{ github.event.pull_request.comments_url }}"
7974
else
8075
PR_URL="https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments"
8176
fi
82-
77+
8378
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

Comments
 (0)