forked from guozhigq/pilipala
-
Notifications
You must be signed in to change notification settings - Fork 55
252 lines (219 loc) · 8.93 KB
/
build-alpha.yml
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
name: Build alpha
on:
workflow_dispatch:
inputs:
build_num:
required: true
type: choice
default: "1"
options: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 114514]
push:
branches:
- 'dev'
paths-ignore:
- '**.md'
- '**.txt'
- '.github/**'
- '.idea/**'
- '!.github/workflows/build-alpha.yml'
jobs:
prepare_publish_info:
name: Prepare publish info
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.update_version.outputs.new_version }}
included_commits: ${{ steps.analyze_push_event.outputs.included_commits }}
last_commit_hash: ${{ steps.analyze_push_event.outputs.last_commit_hash }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 解析push事件信息
id: analyze_push_event
run: |
included_commits=$(git log ${{ github.event.before }}..HEAD --pretty="%h %s" --first-parent | jq -Rs '.')
last_commit_hash=$(git log -1 --pretty="%H" --first-parent)
echo "included_commits=$included_commits" >> $GITHUB_OUTPUT
echo "last_commit_hash=$last_commit_hash" >> $GITHUB_OUTPUT
- name: Get previous workflow run
id: get_previous_run
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# TODO: 获取上一个workflow run运行状态以等待其artifact
workflow_runs=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/build-alpha.yml/runs?status=completed&per_page=2")
previous_run_id=$(echo $workflow_runs | jq -r '.workflow_runs[0].id')
previous_run_success=0
if (echo $workflow_runs | jq -r '.workflow_runs[0].conclusion' | grep -q 'success'); then
previous_run_success=1
fi
echo "Previous run ID: $previous_run_id"
echo "previous_run_id=$previous_run_id" >> $GITHUB_OUTPUT
echo "Previous run success: $previous_run_success"
echo "previous_run_success=$previous_run_success" >> $GITHUB_OUTPUT
- name: 从artifact获取上次构建信息
if: ${{ ! inputs.build_num }}
uses: actions/download-artifact@v4
with:
name: build_info
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ steps.get_previous_run.outputs.previous_run_id }}
- name: 生成alpha版本号
id: update_version
run: |
last_tag=$(git tag --sort=committerdate | tail -1)
version_name=$(yq e .version pubspec.yaml | cut -d "+" -f 1)
version_code=$(echo $last_tag | cut -d "+" -f 2)
if [[ ! -e build_info.yml && -z "${{ inputs.build_num }}" ]]; then
echo "Neither build_info.yml exists nor specified build_num manually!"
exit 1
fi
# pubspec.yaml中版本号比tag新,则发布pre版,否则发布pre版
alpha_or_pre=pre
if (echo $last_tag | grep $version_name); then
alpha_or_pre=alpha
fi
# 如果上次workflow run失败,且与当前版本号一致,不递增build_num。
build_num=1
if [[ -n "${{ inputs.build_num }}" ]]; then
build_num=${{ inputs.build_num }}
elif [[ $(yq -r .version build_info.yml) == $version_name ]]; then
let build_num=$(yq -r .build_num build_info.yml)+${{ steps.get_previous_run.outputs.previous_run_success }}
fi
echo "new_version=${version_name}-${alpha_or_pre}.${build_num}+${version_code}" >> $GITHUB_OUTPUT
echo "new_build_num=$build_num" >> $GITHUB_OUTPUT
- name: 生成新build_info.yml
run: |
version=$(yq -er .version pubspec.yaml | cut -d "+" -f 1)
build_num=${{ steps.update_version.outputs.new_build_num}}
rm -f build_info.yml
cat << EOF > build_info.yml
version: $version
build_num: $build_num
EOF
- name: 上传build_info.yml
uses: actions/upload-artifact@v4
with:
name: build_info
path: ./build_info.yml
build_matrix:
name: Build CI (${{ matrix.target_platform }})
needs: prepare_publish_info
runs-on: ${{ matrix.build_os }}
strategy:
matrix:
target_platform: [android-arm, android-arm64, android-x64, android-universal, iOS]
include:
- build_os: ubuntu-latest
- target_platform: iOS
build_os: macos-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: 构建Java环境
if: startsWith(matrix.target_platform, 'android')
uses: actions/setup-java@v3
with:
distribution: "zulu"
java-version: "21"
token: ${{ secrets.GIT_TOKEN }}
- name: 安装Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: 3.24.4
channel: stable
- name: 修复Flutter 3.24中文字重异常
if: startsWith(matrix.target_platform, 'android')
run: |
cd $FLUTTER_ROOT
git config --global user.name "orz12"
git config --global user.email "[email protected]"
git cherry-pick d4124bd --strategy-option theirs
# Flutter precache
flutter --version
cd -
- name: 下载项目依赖
run: flutter pub get
- name: 解码生成 jks
if: startsWith(matrix.target_platform, 'android')
run: echo $KEYSTORE_BASE64 | base64 -di > android/app/vvex.jks
env:
KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
- name: 更新版本号
id: version
run: |
yq ".version=\"${{ needs.prepare_publish_info.outputs.new_version }}\"" pubspec.yaml > tmp.yaml
mv tmp.yaml pubspec.yaml
- name: flutter build apk (universal)
if: matrix.target_platform == 'android-universal'
run: flutter build apk --release
env:
KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
- name: flutter build apk (${{ matrix.target_platform }})
if: startsWith(matrix.target_platform, 'android') && matrix.target_platform != 'android-universal'
run: flutter build apk --release --split-per-abi --target-platform=${{ matrix.target_platform }}
env:
KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
- name: flutter build ipa
if: matrix.target_platform == 'iOS'
run: |
flutter build ios --release --no-codesign
ln -sf ./build/ios/iphoneos Payload
zip -r9 app.ipa Payload/runner.app
- name: 重命名安装包(${{ matrix.target_platform }})
if: startsWith(matrix.target_platform, 'android')
run: |
version_name=$(yq e .version pubspec.yaml | cut -d "+" -f 1)
for file in build/app/outputs/flutter-apk/app-*.apk; do
if [[ $file =~ app-(.?*)release.apk ]]; then
new_file_name="build/app/outputs/flutter-apk/Pili-${BASH_REMATCH[1]}${version_name}.apk"
mv "$file" "$new_file_name"
fi
done
- name: 重命名安装包(iOS)
if: matrix.target_platform == 'iOS'
run: |
for file in app.ipa; do
new_file_name="build/Pili-${{ needs.prepare_publish_info.outputs.new_version }}.ipa"
mv "$file" "$new_file_name"
done
- name: 上传至artifact (${{ matrix.target_platform }})
if: startsWith(matrix.target_platform, 'android')
uses: actions/upload-artifact@v4
with:
name: PiliPalaX-${{ matrix.target_platform }}
path: |
build/app/outputs/flutter-apk/Pili-*.apk
- name: 上传至artifact (iOS)
if: matrix.target_platform == 'iOS'
uses: actions/upload-artifact@v4
with:
name: PiliPalaX-iOS
path: |
build/Pili-*.ipa
upload:
runs-on: ubuntu-latest
needs:
- prepare_publish_info
- build_matrix
steps:
- name: 从artifact下载
uses: actions/download-artifact@v4
- name: 发送到Telegram频道
uses: xireiki/[email protected]
with:
bot_token: ${{ secrets.BOT_TOKEN }}
chat_id: ${{ secrets.CHAT_ID }}
api_id: ${{ secrets.TELEGRAM_API_ID }}
api_hash: ${{ secrets.TELEGRAM_API_HASH }}
large_file: true
method: sendFile
path: PiliPalaX-*/*
parse_mode: Markdown
context: "*v${{ needs.prepare_publish_info.outputs.new_version }}:*\n${{ fromJson(needs.prepare_publish_info.outputs.included_commits) }}"