|
| 1 | +name: Optimize Images |
| 2 | + |
| 3 | +on: |
| 4 | + # Manual trigger |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + mode: |
| 8 | + description: 'Compression mode' |
| 9 | + required: true |
| 10 | + default: 'lossless' |
| 11 | + type: choice |
| 12 | + options: |
| 13 | + - lossless |
| 14 | + - lossy |
| 15 | + path: |
| 16 | + description: 'Path to optimize (default: build/html/)' |
| 17 | + required: false |
| 18 | + default: 'build/html/' |
| 19 | + |
| 20 | + # Run on PRs that add/modify images |
| 21 | + pull_request: |
| 22 | + paths: |
| 23 | + - '**.png' |
| 24 | + - '**.PNG' |
| 25 | + |
| 26 | +permissions: |
| 27 | + contents: write |
| 28 | + pull-requests: write |
| 29 | + |
| 30 | +jobs: |
| 31 | + optimize: |
| 32 | + runs-on: ubuntu-latest |
| 33 | + name: Optimize PNG images |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Harden Runner |
| 37 | + uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1 |
| 38 | + with: |
| 39 | + egress-policy: audit |
| 40 | + |
| 41 | + - name: Check out repository |
| 42 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 43 | + with: |
| 44 | + ref: ${{ github.head_ref || github.ref }} |
| 45 | + fetch-depth: 0 |
| 46 | + |
| 47 | + - name: Install optimization tools |
| 48 | + run: | |
| 49 | + sudo apt-get update |
| 50 | + sudo apt-get install -y optipng pngquant |
| 51 | +
|
| 52 | + - name: Get changed images (PR only) |
| 53 | + if: github.event_name == 'pull_request' |
| 54 | + id: changed-images |
| 55 | + run: | |
| 56 | + # Get list of changed PNG files |
| 57 | + CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- '*.png' '*.PNG' | tr '\n' ' ') |
| 58 | + echo "files=$CHANGED" >> $GITHUB_OUTPUT |
| 59 | + if [ -z "$CHANGED" ]; then |
| 60 | + echo "No PNG files changed" |
| 61 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 62 | + else |
| 63 | + echo "Changed files: $CHANGED" |
| 64 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Optimize images (PR - changed files only) |
| 68 | + if: github.event_name == 'pull_request' && steps.changed-images.outputs.has_changes == 'true' |
| 69 | + run: | |
| 70 | + for file in ${{ steps.changed-images.outputs.files }}; do |
| 71 | + if [ -f "$file" ]; then |
| 72 | + echo "Optimizing: $file" |
| 73 | + BEFORE=$(stat -c%s "$file") |
| 74 | + optipng -o2 -quiet "$file" || true |
| 75 | + AFTER=$(stat -c%s "$file") |
| 76 | + if [ "$AFTER" -lt "$BEFORE" ]; then |
| 77 | + SAVED=$((BEFORE - AFTER)) |
| 78 | + echo " Saved: $SAVED bytes" |
| 79 | + fi |
| 80 | + fi |
| 81 | + done |
| 82 | +
|
| 83 | + - name: Optimize images (Manual trigger) |
| 84 | + if: github.event_name == 'workflow_dispatch' |
| 85 | + run: | |
| 86 | + MODE_FLAG="" |
| 87 | + if [ "${{ inputs.mode }}" == "lossy" ]; then |
| 88 | + MODE_FLAG="--lossy" |
| 89 | + fi |
| 90 | + ./scripts/optimize_images.sh $MODE_FLAG "${{ inputs.path }}" |
| 91 | +
|
| 92 | + - name: Check for changes |
| 93 | + id: git-check |
| 94 | + run: | |
| 95 | + git diff --exit-code || echo "has_changes=true" >> $GITHUB_OUTPUT |
| 96 | +
|
| 97 | + - name: Commit optimized images (PR) |
| 98 | + if: github.event_name == 'pull_request' && steps.git-check.outputs.has_changes == 'true' |
| 99 | + run: | |
| 100 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 101 | + git config --local user.name "github-actions[bot]" |
| 102 | + git add -A |
| 103 | + git commit -m "Optimize PNG images |
| 104 | +
|
| 105 | + Automated image optimization using optipng." |
| 106 | + git push |
| 107 | +
|
| 108 | + - name: Create PR with optimized images (Manual trigger) |
| 109 | + if: github.event_name == 'workflow_dispatch' && steps.git-check.outputs.has_changes == 'true' |
| 110 | + run: | |
| 111 | + BRANCH="optimize-images-$(date +%Y%m%d-%H%M%S)" |
| 112 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 113 | + git config --local user.name "github-actions[bot]" |
| 114 | + git checkout -b "$BRANCH" |
| 115 | + git add -A |
| 116 | + git commit -m "Optimize PNG images (${{ inputs.mode }} mode) |
| 117 | +
|
| 118 | + Automated image optimization using optipng${{ inputs.mode == 'lossy' && ' and pngquant' || '' }}." |
| 119 | + git push -u origin "$BRANCH" |
| 120 | +
|
| 121 | + gh pr create \ |
| 122 | + --title "Optimize PNG images (${{ inputs.mode }} mode)" \ |
| 123 | + --body "## Summary |
| 124 | +
|
| 125 | + Automated image optimization run. |
| 126 | +
|
| 127 | + - **Mode**: ${{ inputs.mode }} |
| 128 | + - **Path**: ${{ inputs.path }} |
| 129 | +
|
| 130 | + This PR contains optimized PNG images to reduce repository size and improve page load times." \ |
| 131 | + --base ${{ github.ref_name }} |
| 132 | + env: |
| 133 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + |
| 135 | + - name: Summary |
| 136 | + run: | |
| 137 | + echo "## Image Optimization Complete" >> $GITHUB_STEP_SUMMARY |
| 138 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 139 | + if [ "${{ steps.git-check.outputs.has_changes }}" == "true" ]; then |
| 140 | + echo "Images were optimized and changes committed." >> $GITHUB_STEP_SUMMARY |
| 141 | + else |
| 142 | + echo "No optimization improvements found." >> $GITHUB_STEP_SUMMARY |
| 143 | + fi |
0 commit comments