forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (115 loc) · 4.78 KB
/
Copy pathoptimize-images.yml
File metadata and controls
133 lines (115 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: Auto-Optimize Docs PNG Images
on:
pull_request:
types:
- labeled
jobs:
optimize-images:
runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
if: ${{ contains(github.event.pull_request.labels.*.name,'Type:Documentation') }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
ref: ${{ github.head_ref }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Install pngcrush and bc
run: |
sudo apt-get update
sudo apt-get install -y pngcrush bc
- name: Get changed PNG files
id: changed-files
uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
with:
files: |
docs/**/*.png
- name: Optimize changed PNG files
if: steps.changed-files.outputs.any_changed == 'true'
id: optimize
run: |
echo "Optimizing new PNG files with pngcrush..."
echo "Branch: $(git_current_branch)"
# Initialize tracking variables
total_files=0
total_before=0
total_after=0
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "Processing: $file"
before_size=$(stat -c%s "$file")
# Use pngcrush with maximum lossless compression settings
# -rem alla removes all ancillary chunks (metadata)
# -brute tries all filter types and compression levels
# -ow overwrites the original file
pngcrush -rem alla -brute -ow "$file"
after_size=$(stat -c%s "$file")
echo " > Before: ${before_size} bytes, After: ${after_size} bytes"
# Track totals
total_files=$((total_files + 1))
total_before=$((total_before + before_size))
total_after=$((total_after + after_size))
done
# Calculate savings
total_saved=$((total_before - total_after))
# Output results for use in commit message
echo "files_optimized=${total_files}" >> "$GITHUB_OUTPUT"
echo "bytes_saved=${total_saved}" >> "$GITHUB_OUTPUT"
echo "total_before=${total_before}" >> "$GITHUB_OUTPUT"
echo "total_after=${total_after}" >> "$GITHUB_OUTPUT"
echo "Optimization complete"
echo "Files processed: ${total_files}"
echo "Total bytes saved: ${total_saved} (${total_before} -> ${total_after})"
- name: Check for changes after optimization
if: steps.changed-files.outputs.any_changed == 'true'
id: check_changes
run: |
git add ${{ steps.changed-files.outputs.all_changed_files }}
if git diff --quiet --cached; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
echo "No changes detected after optimization"
else
echo "has_changes=true" >> "$GITHUB_OUTPUT"
echo "Changes detected after optimization"
git diff --cached --stat
fi
- name: Commit optimized images
if: steps.changed-files.outputs.any_changed == 'true' && steps.check_changes.outputs.has_changes == 'true'
env:
GITHUB_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
run: |
git config --global user.email "github-automation@metabase.com"
git config --global user.name "Metabase Automation"
git add docs/**/*.png
# Format bytes saved for human readability
bytes_saved="${{ steps.optimize.outputs.bytes_saved }}"
bytes_before="${{ steps.optimize.outputs.total_before }}"
if [ "$bytes_saved" -gt 1048576 ]; then
# Convert to MB
size_saved=$(echo "scale=1; $bytes_saved / 1048576" | bc)
size_unit="MB"
elif [ "$bytes_saved" -gt 1024 ]; then
# Convert to KB
size_saved=$(echo "scale=1; $bytes_saved / 1024" | bc)
size_unit="KB"
else
size_saved="$bytes_saved"
size_unit="bytes"
fi
# Calculate percentage reduction
if [ "$bytes_before" -gt 0 ]; then
percentage=$(echo "scale=1; $bytes_saved * 100 / $bytes_before" | bc)
git commit -m "Optimized ${{ steps.optimize.outputs.files_optimized }} images, saved ${size_saved} ${size_unit} (${percentage}% smaller)"
else
git commit -m "Optimized ${{ steps.optimize.outputs.files_optimized }} images, saved ${size_saved} ${size_unit}"
fi
git push
echo 'Files optimized:'
echo ${{ steps.changed-files.outputs.all_changed_files }}
echo 'Applied: pngcrush -rem alla -brute -ow'
- name: Output summary
if: steps.changed-files.outputs.any_changed == 'true'
run: |
if [ "${{ steps.check_changes.outputs.has_changes }}" = "false" ]; then
echo "PNG files were already optimized - no changes needed"
else
echo "PNG files optimized and committed successfully"
fi