Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Created November 16, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdanyow/d2db983bc769168fad755cf1c556fb43 to your computer and use it in GitHub Desktop.
Save jdanyow/d2db983bc769168fad755cf1c556fb43 to your computer and use it in GitHub Desktop.
Optimize PNGs using GitHub actions

A GitHub action to optimize PNG files that were created/modified in a pull request.

name: Optimize PNGs
on:
pull_request:
branches:
- main
jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Optimize PNGs
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.number }}
run: |
PNGS=$(gh pr view $PR_NUMBER --json files --jq '.files.[].path | select(endswith(".png"))')
if [ -z "$PNGS" ]; then
echo "No PNGs found"
exit 0
fi
sudo apt-get install -y optipng
echo $PNGS | xargs optipng -nc -nb -o7
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Optimize PNGs
branch: ${{ github.head_ref }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment