Repository navigation
Merge pull request #1035 from brandon-dacrib/fix/unifi-os-self-hosted #54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| # Optimizes PNG icons, committing the results back to the repository. | |
| # Icons larger than the 300x300 maximum enforced by apps.tests.js are first | |
| # resized to fit (aspect ratio preserved, shrink only); everything is then | |
| # losslessly recompressed with optipng, which only recompresses and strips | |
| # metadata. Mirrors svgo.yml (which optimizes SVGs; those have no pixel | |
| # dimensions to resize). | |
| name: Optimize PNG Files | |
| on: | |
| push: | |
| branches: [master] | |
| permissions: | |
| contents: write | |
| # Queue runs instead of racing: rapid-fire merges each trigger a run, and | |
| # concurrent runs pushing to master reject each other (non-fast-forward). | |
| concurrency: | |
| group: png-optimize-master | |
| cancel-in-progress: false | |
| jobs: | |
| pngs: | |
| name: PNGs | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v7 | |
| with: | |
| # Check out the branch tip rather than the trigger SHA so a queued | |
| # run builds on the previous run's optimize commit and its push | |
| # fast-forwards. | |
| ref: master | |
| fetch-depth: 0 | |
| - name: Install optipng and ImageMagick | |
| run: | | |
| set -euo pipefail | |
| sudo apt-get update | |
| sudo apt-get install -y optipng imagemagick | |
| - name: Optimize PNGs | |
| id: optipng | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| # Determine the PNG files changed in this push. Fall back to every | |
| # tracked PNG when the previous commit is unavailable (e.g. the first | |
| # push to a branch or a force push). | |
| before="${{ github.event.before }}" | |
| if [ -z "$before" ] \ | |
| || [ "$before" = "0000000000000000000000000000000000000000" ] \ | |
| || ! git cat-file -e "${before}^{commit}" 2>/dev/null; then | |
| mapfile -t files < <(git ls-files -- '*.png') | |
| else | |
| mapfile -t files < <(git diff --name-only --diff-filter=d \ | |
| "$before" "${{ github.sha }}" -- '*.png') | |
| fi | |
| if [ "${#files[@]}" -eq 0 ]; then | |
| echo "No PNG files to optimize." | |
| echo "optimized_count=0" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "Optimizing ${#files[@]} PNG file(s):" | |
| printf ' %s\n' "${files[@]}" | |
| # Resize any icon exceeding the 300x300 maximum that apps.tests.js | |
| # enforces. The '>' flag makes mogrify shrink-only and the fit is | |
| # proportional, so compliant icons are never rewritten. head -n1 | |
| # guards against multi-frame PNGs printing one size per frame. | |
| resized=0 | |
| for f in "${files[@]}"; do | |
| read -r w h < <(identify -format '%w %h\n' "$f" | head -n1) | |
| if [ "$w" -gt 300 ] || [ "$h" -gt 300 ]; then | |
| echo "Resizing $f (${w}x${h} -> fit within 300x300)" | |
| mogrify -resize '300x300>' "$f" | |
| resized=$((resized + 1)) | |
| fi | |
| done | |
| # -o5: thorough lossless search; -strip all: drop metadata chunks. | |
| optipng -o5 -strip all "${files[@]}" | |
| # Count the files actually changed so the commit message matches. | |
| count=$(git diff --name-only -- '*.png' | wc -l | tr -d '[:space:]') | |
| msg="Optimize ${count} PNG(s)" | |
| if [ "$resized" -gt 0 ]; then | |
| msg="${msg}, resized ${resized} to fit 300x300" | |
| fi | |
| echo "optimized_count=${count}" >> "$GITHUB_OUTPUT" | |
| echo "commit_message=${msg}" >> "$GITHUB_OUTPUT" | |
| - name: Commit optimizations | |
| uses: stefanzweifel/git-auto-commit-action@v7 | |
| if: ${{ steps.optipng.outputs.optimized_count != '0' }} | |
| with: | |
| commit_message: ${{ steps.optipng.outputs.commit_message }} | |
| file_pattern: '*.png' |