-
Notifications
You must be signed in to change notification settings - Fork 1
119 lines (102 loc) · 3.92 KB
/
image-optimization.yml
File metadata and controls
119 lines (102 loc) · 3.92 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
name: Optimize Images
on:
push:
branches:
- main
paths:
- "static/images/**"
- "!static/images/optimized/**"
- "!static/images/backup/**"
pull_request:
branches:
- main
paths:
- "static/images/**"
- "!static/images/optimized/**"
- "!static/images/backup/**"
permissions:
contents: write
defaults:
run:
shell: bash
jobs:
optimize-images:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.MY_GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-image-optimization-${{ hashFiles('tools/image-optimization/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-image-optimization-
- name: Install dependencies
run: |
# Install jq for JSON parsing
sudo apt-get update && sudo apt-get install -y jq
# Install Python dependencies
python -m pip install --upgrade pip
pip install -r tools/image-optimization/requirements.txt
- name: Run image optimization
run: |
echo "开始图片优化..."
python tools/image-optimization/image_optimizer.py \
--input-dir static/images \
--output-dir static/images/optimized \
--quality 85 \
--sizes 320 640 960 1280 1920 \
--no-backup
- name: Check for changes
id: verify-changed-files
run: |
if [ -n "$(git status --porcelain static/images/optimized/)" ]; then
echo "changed=true" >> $GITHUB_OUTPUT
echo "检测到图片优化变化"
else
echo "changed=false" >> $GITHUB_OUTPUT
echo "没有图片需要优化"
fi
- name: Commit optimized images
if: steps.verify-changed-files.outputs.changed == 'true'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add static/images/optimized/
git commit -m "🔧 优化图片: 自动生成WebP格式和响应式尺寸" -m "生成多尺寸图片 (320px, 640px, 960px, 1280px, 1920px)" -m "转换为WebP格式以减少文件大小" -m "应用智能压缩 (质量: 85%)" -m "此提交由GitHub Actions自动生成"
echo "图片优化完成并提交"
- name: Push changes
if: steps.verify-changed-files.outputs.changed == 'true'
env:
GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
run: |
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git
# 智能推送逻辑
if [[ "${{ github.ref_name }}" == "main" ]]; then
echo "Pushing to main branch"
git push origin main
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "Pull request detected, pushing to source branch instead"
# 获取PR的源分支名
SOURCE_BRANCH=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.number }} | \
jq -r '.head.ref')
echo "Source branch: $SOURCE_BRANCH"
if [[ "$SOURCE_BRANCH" != "null" && "$SOURCE_BRANCH" != "" ]]; then
git push origin HEAD:$SOURCE_BRANCH
else
echo "Could not determine source branch, skipping push"
fi
else
echo "Pushing to current branch"
git push origin HEAD:${{ github.ref_name }}
fi
echo "推送优化后的图片到仓库"