Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub Action to check image upload sizes #857

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/check-image-upload-size
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Check Image Upload Size

on:
pull_request:
paths:
- '**/*.jpg'
- '**/*.jpeg'
- '**/*.png'
- '**/*.gif'

jobs:
check_image_size:
runs-on: ubuntu-latest
env:
MAX_IMAGE_SIZE_MB: 5
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Check image sizes
run: |
#!/bin/bash
set -e

MAX_SIZE_BYTES=$((${MAX_IMAGE_SIZE_MB} * 1024 * 1024))
OVERSIZED_IMAGES=()

for img in $(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(jpg|jpeg|png|gif)$'); do
size=$(wc -c < "$img")
if [ $size -gt $MAX_SIZE_BYTES ]; then
OVERSIZED_IMAGES+=("$img ($(numfmt --to=iec-i --suffix=B --format="%.2f" $size))")
fi
done

if [ ${#OVERSIZED_IMAGES[@]} -gt 0 ]; then
echo "Error: The following images exceed the maximum allowed size of ${MAX_IMAGE_SIZE_MB}MB:"
printf '%s\n' "${OVERSIZED_IMAGES[@]}"
exit 1
else
echo "All new or modified images are within the size limit."
fi
Loading