Automating Image Conversion in Your CI/CD Pipeline
Every commit that adds or modifies images is an opportunity for optimization. Instead of relying on developers to manually compress and convert images before pushing, build it into your CI/CD pipeline and make it automatic.
Why Automate in CI/CD?
- Consistent output: Every image gets the same treatment regardless of who commits it
- No forgotten optimizations: New team members don’t need to know the process
- Audit trail: The pipeline logs exactly what was converted and how much was saved
- Shift-left approach: Catch oversized images before they reach production
GitHub Actions Example
Here’s a workflow that converts and compresses any new or modified images on pull requests:
name: Optimize Images
on:
pull_request:
paths:
- '**/*.png'
- '**/*.jpg'
- '**/*.jpeg'
jobs:
optimize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Find changed images
id: images
run: |
FILES=$(git diff --name-only HEAD~1 -- '*.png' '*.jpg' '*.jpeg')
echo "files=$FILES" >> $GITHUB_OUTPUT
- name: Convert to WebP
if: steps.images.outputs.files != ''
env:
KOALAPIC_KEY: ${{ secrets.KOALAPIC_API_KEY }}
run: |
for img in ${{ steps.images.outputs.files }}; do
curl -s -X POST https://koalapic.com/api/v1/convert \
-H "Authorization: Bearer $KOALAPIC_KEY" \
-F "file=@$img" \
-F "output_format=webp" \
-F "smart_quality=medium" \
-o "${img%.*}.webp"
done
Docker Integration
Add image conversion to your Docker build:
FROM python:3.12-slim AS builder
RUN pip install requests
COPY optimize_images.py /opt/
COPY assets/images/ /opt/images/
RUN python /opt/optimize_images.py /opt/images/ --format webp --quality medium
FROM nginx:alpine
COPY --from=builder /opt/images/ /usr/share/nginx/html/images/
Pre-Commit Hook Alternative
For smaller teams, a git pre-commit hook can optimize images before they’re even committed:
#!/bin/bash
# .git/hooks/pre-commit
for img in $(git diff --cached --name-only -- '*.png' '*.jpg'); do
curl -s -X POST https://koalapic.com/api/v1/compress \
-H "Authorization: Bearer $KOALAPIC_KEY" \
-F "file=@$img" \
-F "smart_quality=medium" \
-o "$img"
git add "$img"
done
Handling Failures Gracefully
API calls in CI can fail due to network issues or rate limits. Make your pipeline resilient:
- Retry with backoff: If a conversion returns 429 or 5xx, wait and retry
- Non-blocking: Make image optimization a warning, not a failure — don’t block merges because the API was temporarily unavailable
- Cache results: Skip re-processing images that haven’t changed since the last build
- Timeout: Set a reasonable timeout (30 seconds per image) to prevent hung builds
Measuring Impact
Track the before/after sizes in your CI output. A typical project sees:
- PNG screenshots: 60–80% smaller as WebP
- JPEG photos: 25–35% smaller with smart quality
- Overall bandwidth savings: 40–60%
These savings compound across every page load for every user. Over the lifetime of a web application, automated image optimization in CI pays for itself many times over in reduced bandwidth costs and improved user experience.
Next Steps
- Get your API key for CI/CD integration
- Read the full API documentation for all conversion parameters
- Set up webhooks for async pipeline integration