Skip to content

Commit 59b80ef

Browse files
committed
feat: Refactor image size check to a reusable action
This moves the image size check logic from the preview workflow to a dedicated and reusable GitHub Action. This new action runs on every pull request to enforce size limits on images.
1 parent 467d015 commit 59b80ef

File tree

3 files changed

+87
-70
lines changed

3 files changed

+87
-70
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: 'Image Size Check'
2+
description: 'Checks for oversized images in a pull request.'
3+
runs:
4+
using: 'composite'
5+
steps:
6+
- name: Get changed files
7+
id: changed-files
8+
uses: tj-actions/changed-files@v44
9+
with:
10+
separator: '\n'
11+
files_ignore_pattern: |
12+
**/*.pdf
13+
**/*.zip
14+
15+
- name: Enforce Image Size Limits on Changed Files
16+
if: steps.changed-files.outputs.all_changed_files
17+
shell: bash
18+
run: |
19+
IMAGE_LIMIT_MB=2
20+
GIF_LIMIT_MB=10
21+
22+
OVERSIZED_IMAGES=""
23+
OVERSIZED_GIFS=""
24+
25+
while IFS= read -r file; do
26+
if [ -z "$file" ]; then continue; fi
27+
if [ ! -f "$file" ]; then
28+
echo "Warning: Changed file not found, skipping: $file"
29+
continue
30+
fi
31+
32+
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.jpeg || "$file" == *.svg ]]; then
33+
size_in_bytes=$(stat -c%s "$file")
34+
limit_in_bytes=$((IMAGE_LIMIT_MB * 1024 * 1024))
35+
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
36+
size_human=$(ls -lh "$file" | awk '{print $5}')
37+
OVERSIZED_IMAGES+=" - $file ($size_human)\n"
38+
fi
39+
elif [[ "$file" == *.gif ]]; then
40+
size_in_bytes=$(stat -c%s "$file")
41+
limit_in_bytes=$((GIF_LIMIT_MB * 1024 * 1024))
42+
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
43+
size_human=$(ls -lh "$file" | awk '{print $5}')
44+
OVERSIZED_GIFS+=" - $file ($size_human)\n"
45+
fi
46+
fi
47+
done <<< "${{ steps.changed-files.outputs.all_changed_files }}"
48+
49+
# --- Report errors if any oversized files are found ---
50+
ERROR_MESSAGE=""
51+
if [ -n "$OVERSIZED_IMAGES" ]; then
52+
ERROR_MESSAGE+="Found images exceeding the ${IMAGE_LIMIT_MB}MB limit:\n"
53+
ERROR_MESSAGE+="${OVERSIZED_IMAGES}"
54+
fi
55+
56+
if [ -n "$OVERSIZED_GIFS" ]; then
57+
ERROR_MESSAGE+="Found GIFs exceeding the ${GIF_LIMIT_MB}MB limit:\n"
58+
ERROR_MESSAGE+="${OVERSIZED_GIFS}"
59+
fi
60+
61+
if [ -n "$ERROR_MESSAGE" ]; then
62+
echo -e "::error::Oversized images found in this PR. Please optimize or remove them.\n"
63+
echo -e "--------------------------------------------------"
64+
echo -e "$ERROR_MESSAGE"
65+
echo -e "--------------------------------------------------"
66+
exit 1
67+
else
68+
echo "All changed images are within their size limits. Check passed."
69+
fi
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Image Size Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
7+
jobs:
8+
check-images:
9+
name: Image Size Check
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Repository
13+
uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
17+
- name: Check Image Sizes
18+
uses: ./.github/actions/image-size-check

.github/workflows/preview.yml

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,76 +12,6 @@ concurrency:
1212
cancel-in-progress: true
1313

1414
jobs:
15-
image-size-check:
16-
name: Image Size Check
17-
runs-on: ubuntu-latest
18-
steps:
19-
- name: Checkout Repository
20-
uses: actions/checkout@v4
21-
22-
- name: Get changed files
23-
id: changed-files
24-
uses: tj-actions/changed-files@v44
25-
with:
26-
separator: '\n'
27-
28-
- name: Enforce Image Size Limits on Changed Files
29-
if: steps.changed-files.outputs.all_changed_files
30-
run: |
31-
#!/bin/bash
32-
set -e
33-
34-
IMAGE_LIMIT_MB=2
35-
GIF_LIMIT_MB=10
36-
37-
OVERSIZED_IMAGES=""
38-
OVERSIZED_GIFS=""
39-
40-
while IFS= read -r file; do
41-
if [ -z "$file" ]; then continue; fi
42-
if [ ! -f "$file" ]; then
43-
echo "Warning: Changed file not found, skipping: $file"
44-
continue
45-
fi
46-
47-
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.jpeg || "$file" == *.svg ]]; then
48-
size_in_bytes=$(stat -c%s "$file")
49-
limit_in_bytes=$((IMAGE_LIMIT_MB * 1024 * 1024))
50-
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
51-
size_human=$(ls -lh "$file" | awk '{print $5}')
52-
OVERSIZED_IMAGES+=" - $file ($size_human)\n"
53-
fi
54-
elif [[ "$file" == *.gif ]]; then
55-
size_in_bytes=$(stat -c%s "$file")
56-
limit_in_bytes=$((GIF_LIMIT_MB * 1024 * 1024))
57-
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
58-
size_human=$(ls -lh "$file" | awk '{print $5}')
59-
OVERSIZED_GIFS+=" - $file ($size_human)\n"
60-
fi
61-
fi
62-
done <<< "${{ steps.changed-files.outputs.all_changed_files }}"
63-
64-
# --- Report errors if any oversized files are found ---
65-
ERROR_MESSAGE=""
66-
if [ -n "$OVERSIZED_IMAGES" ]; then
67-
ERROR_MESSAGE+="Found images exceeding the ${IMAGE_LIMIT_MB}MB limit:\n"
68-
ERROR_MESSAGE+="${OVERSIZED_IMAGES}"
69-
fi
70-
71-
if [ -n "$OVERSIZED_GIFS" ]; then
72-
ERROR_MESSAGE+="Found GIFs exceeding the ${GIF_LIMIT_MB}MB limit:\n"
73-
ERROR_MESSAGE+="${OVERSIZED_GIFS}"
74-
fi
75-
76-
if [ -n "$ERROR_MESSAGE" ]; then
77-
echo -e "::error::Oversized images found in this PR. Please optimize or remove them.\n"
78-
echo -e "--------------------------------------------------"
79-
echo -e "$ERROR_MESSAGE"
80-
echo -e "--------------------------------------------------"
81-
exit 1
82-
else
83-
echo "All changed images are within their size limits. Check passed."
84-
fi
8515
# This job is used to render datasheets, but only if they have changed.
8616
# It's a separate job so we don't have to cleanup the machine afterwards.
8717
render-datasheets:

0 commit comments

Comments
 (0)