💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Automate Image Compression in GitHub Actions?

calibreapp-image-actions -
GitHub Action
1.1.0
1,418
Contributors
Contributor - benschwarzContributor - tunetheweb
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'Usage of image-actions GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Compress Images
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}

image-actions logo

Image Actions

A Github Action that automatically compresses JPEGs, PNGs and WebPs in Pull Requests.


What is image-actions?

Calibre Image Actions is built by performance experts to offer fast and efficient near-lossless compression using the best algorithms available, like mozjpeg and libvips. It can be customized to fit our needs and supports both on-demand and scheduled operations.

How to customize quality image-actions?

While Calibre Image Actions work great with default settings, you can customize the behavior to better suit your project's requirements:

- name: Calibre Image Compression
uses: calibreapp/image-actions@main
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
jpeg:
quality: '80'
png:
quality: '80-90'
webp:
quality: '80'

How to create a scheduled image-actions?

If you prefer not to run compression on every pull request, you can schedule the action to run at specific times:

on:
schedule:
- cron: '0 0 * * *' # Runs every day at midnight

How to ignore specific paths?

To prevent certain directories or files from being processed, use the ignorePaths option. This is particularly useful for excluding image directories that do not require compression or are managed differently.

steps:
- name: Calibre Image Compression
uses: calibreapp/image-actions@main
with:
ignorePaths: 'node_modules/**,build'

ignorePaths: Accepts a comma-separated string with glob paths, allowing you to specify directories or files to exclude from compression.

How to use with Pull Request?

When integrating Calibre Image Actions in our workflows, we face a challenge with forked repositories due to GitHub Actions' permissions limitations. Here are several strategies to effectively manage image compression for pull requests from forked repositories.

Understanding GitHub Actions Permissions

By default, GitHub Actions running from forked repositories do not have sufficient permissions to push changes back to the base repository. This limits the ability of actions like Image Actions to commit compressed images directly from such pull requests.

Strategies to Overcome Permission Limitations

1. Use a Personal Access Token (PAT)

Replacing the default GITHUB_TOKEN with a PAT that has broader permissions can allow actions to commit changes from forked repositories.

Security Considerations:

Be aware that using a PAT can introduce security risks, as it grants more extensive access. It's crucial to manage these tokens carefully and review GitHub's best practices for token security.

2. Limit Image Actions to Internal Pull Requests

A safer approach is to restrict Image Actions to operate only on pull requests within the same repository:

if: github.event.pull_request.head.repo.full_name == github.repository

This conditional ensures that the action only runs when the pull request originates from the same repository, avoiding issues with permissions on forked repositories.

Use CompressOnly Mode for Main Branch Pushes

For changes pushed directly to the main branch, including those merged from forked repository pull requests, run Image Actions in compressOnly mode. Then, raise a new pull request for the compressed images using an automated process:

name: Compress Images on Push to main branch
on:
push:
branches:
- main
paths:
- '**.jpg'
- '**.jpeg'
- '**.png'
- '**.webp'
jobs:
build:
name: calibreapp/image-actions
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Compress Images
id: calibre
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
compressOnly: true
- name: Create New Pull Request If Needed
if: steps.calibre.outputs.markdown != ''
uses: peter-evans/create-pull-request@v4
with:
title: Compressed Images
branch-suffix: timestamp
commit-message: Compressed Images
body: ${{ steps.calibre.outputs.markdown }}

This configuration ensures that any images merged into the main branch without compression are subsequently processed and a new pull request is created for them.