Skip to content

Commit 0113545

Browse files
committed
Add compress script
1 parent ed02e72 commit 0113545

File tree

4 files changed

+1110
-165
lines changed

4 files changed

+1110
-165
lines changed

.github/workflows/preview.yml

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,72 @@ 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+
26+
- name: Enforce Image Size Limits on Changed Files
27+
if: steps.changed-files.outputs.all_changed_files
28+
run: |
29+
#!/bin/bash
30+
set -e
1531
16-
# This job is used to render datasheets, but only if they have changed.
32+
# Input from the previous step
33+
CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}"
34+
35+
IMAGE_LIMIT_MB=2
36+
GIF_LIMIT_MB=10
37+
38+
OVERSIZED_IMAGES=""
39+
OVERSIZED_GIFS=""
40+
41+
for file in $CHANGED_FILES; do
42+
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.jpeg || "$file" == *.svg ]]; then
43+
size_in_bytes=$(stat -c%s "$file")
44+
limit_in_bytes=$((IMAGE_LIMIT_MB * 1024 * 1024))
45+
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
46+
size_human=$(ls -lh "$file" | awk '{print $5}')
47+
OVERSIZED_IMAGES+=" - $file ($size_human)\n"
48+
fi
49+
elif [[ "$file" == *.gif ]]; then
50+
size_in_bytes=$(stat -c%s "$file")
51+
limit_in_bytes=$((GIF_LIMIT_MB * 1024 * 1024))
52+
if [ "$size_in_bytes" -gt "$limit_in_bytes" ]; then
53+
size_human=$(ls -lh "$file" | awk '{print $5}')
54+
OVERSIZED_GIFS+=" - $file ($size_human)\n"
55+
fi
56+
fi
57+
done
58+
59+
# --- Report errors if any oversized files are found ---
60+
ERROR_MESSAGE=""
61+
if [ -n "$OVERSIZED_IMAGES" ]; then
62+
ERROR_MESSAGE+="Found images exceeding the ${IMAGE_LIMIT_MB}MB limit:\n"
63+
ERROR_MESSAGE+="${OVERSIZED_IMAGES}"
64+
fi
65+
66+
if [ -n "$OVERSIZED_GIFS" ]; then
67+
ERROR_MESSAGE+="Found GIFs exceeding the ${GIF_LIMIT_MB}MB limit:\n"
68+
ERROR_MESSAGE+="${OVERSIZED_GIFS}"
69+
fi
70+
71+
if [ -n "$ERROR_MESSAGE" ]; then
72+
echo -e "::error::Oversized images found in this PR. Please optimize or remove them.\n"
73+
echo -e "--------------------------------------------------"
74+
echo -e "$ERROR_MESSAGE"
75+
echo -e "--------------------------------------------------"
76+
exit 1
77+
else
78+
echo "All changed images are within their size limits. Check passed."
79+
fi
80+
# This job is used to render datasheets, but only if they have changed.
1781
# It's a separate job so we don't have to cleanup the machine afterwards.
1882
render-datasheets:
1983
name: Render Datasheets
@@ -89,20 +153,6 @@ jobs:
89153
find ./content/hardware -type f -name "*-pinout.png" -exec cp {} ./static/resources/pinouts/ \;
90154
find ./content/hardware -type f -name "*-step.zip" -exec cp {} ./static/resources/models/ \;
91155
92-
- name: Check for large files
93-
run: |
94-
find content -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.svg" -o -name "*.gif" \) -size +2M -print0 |
95-
while IFS= read -r -d $'\0' file; do
96-
size=$(ls -lh "$file" | awk '{print $5}')
97-
echo "::warning file=$file::File '$file' is very large: $size. Consider optimizing it."
98-
done
99-
100-
find content -type f \( -name "*.pdf" -o -name "*.zip" -o -name "*.mp4" \) -size +10M -print0 |
101-
while IFS= read -r -d $'\0' file; do
102-
size=$(ls -lh "$file" | awk '{print $5}')
103-
echo "::warning file=$file::File '$file' is very large: $size. This can impact repository size and checkout times."
104-
done
105-
106156
# - name: Fail on Oversized Files
107157
# id: fail_on_oversized_files
108158
# run: |

0 commit comments

Comments
 (0)