forked from qiin2333/moonlight-vplus
-
Notifications
You must be signed in to change notification settings - Fork 0
210 lines (180 loc) · 7.04 KB
/
android-ci.yml
File metadata and controls
210 lines (180 loc) · 7.04 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
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
name: Android CI
on:
pull_request:
branches:
- master
types:
- opened
- synchronize
- reopened
push:
branches:
- master
tags: [ 'v*' ]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: write
packages: write
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0
fetch-tags: true
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'gradle'
- name: Set up Android SDK/NDK
uses: android-actions/setup-android@v3
with:
packages: platform-tools platforms;android-34 build-tools;34.0.0 ndk;27.0.12077973
- name: Accept Android licenses
run: yes | sdkmanager --licenses > /dev/null
- name: Remove local.properties (if present)
run: rm -f local.properties
- name: Create google-services.json from secrets
shell: bash
run: |
if [ -n "${{ secrets.GOOGLE_SERVICES_JSON }}" ]; then
mkdir -p app
# 清理并验证 JSON 格式
echo '${{ secrets.GOOGLE_SERVICES_JSON }}' | tr -d '\r' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' > app/google-services.json
# 验证 JSON 格式
if python3 -m json.tool app/google-services.json > /dev/null 2>&1; then
echo "Created valid google-services.json from secrets"
else
echo "ERROR: Invalid JSON format in GOOGLE_SERVICES_JSON secret"
exit 1
fi
else
echo "GOOGLE_SERVICES_JSON not set; skipping file creation"
fi
- name: Grant execute permission for Gradle
run: chmod +x gradlew
- name: Get version from tag
if: startsWith(github.ref, 'refs/tags/')
run: |
VERSION=${GITHUB_REF#refs/tags/}
VERSION=${VERSION#v}
echo "VERSION_NAME=$VERSION" >> $GITHUB_ENV
echo "Using version from tag: $VERSION"
- name: Update version in build.gradle
if: startsWith(github.ref, 'refs/tags/')
run: |
VERSION=${GITHUB_REF#refs/tags/}
# 移除版本号前缀 'v' 如果存在
VERSION=${VERSION#v}
echo "Updating version to: $VERSION"
# 读取当前 versionCode 并递增
CURRENT_VERSION_CODE=$(grep -o 'versionCode = [0-9]*' app/build.gradle | grep -o '[0-9]*')
NEW_VERSION_CODE=$((CURRENT_VERSION_CODE + 1))
# 更新 versionName
sed -i "s/versionName \".*\"/versionName \"$VERSION\"/" app/build.gradle
# 更新 versionCode
sed -i "s/versionCode = [0-9]*/versionCode = $NEW_VERSION_CODE/" app/build.gradle
echo "Updated versionName to: $VERSION"
echo "Updated versionCode from $CURRENT_VERSION_CODE to $NEW_VERSION_CODE"
- name: Build NonRoot Debug APK
run: ./gradlew :app:assembleNonRootDebug --no-daemon --stacktrace
- name: Upload APK artifact
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: app-nonroot-debug-apk
path: |
app/build/outputs/apk/nonRoot/debug/*.apk
# ===== Release (Signed) Build (tags only) =====
- name: Decode keystore for Release
if: startsWith(github.ref, 'refs/tags/')
shell: bash
env:
KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
run: |
echo "$KEYSTORE_BASE64" | base64 -d > my-release.keystore
ls -l my-release.keystore
- name: Build signed NonRoot Release APK
if: startsWith(github.ref, 'refs/tags/')
env:
KEYSTORE_PATH: ${{ github.workspace }}/my-release.keystore
KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
run: ./gradlew :app:assembleNonRootRelease --no-daemon --stacktrace
- name: Rename Release APK to Moonlight.V+.<version>.apk
if: startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
set -e
REL_DIR=app/build/outputs/apk/nonRoot/release
# 优先使用 tag 版本号,否则从 metadata 读取
if [ -n "$VERSION_NAME" ]; then
VERSION="$VERSION_NAME"
echo "Using tag version: $VERSION"
else
VERSION=$(python3 -c "import json; data=json.load(open('$REL_DIR/output-metadata.json')); print(data['elements'][0]['versionName'])")
echo "Using metadata version: $VERSION"
fi
SRC_APK=$(ls "$REL_DIR"/*.apk | head -n 1)
DEST="$REL_DIR/Moonlight.V+.${VERSION}.apk"
mv "$SRC_APK" "$DEST"
echo "Renamed APK to: $DEST"
echo "REL_APK=$DEST" >> $GITHUB_ENV
- name: Generate cute release notes
if: startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
set -e
DATE=$(date +'%Y-%m-%d')
V="${VERSION_NAME:-${GITHUB_REF#refs/tags/}}"; V="${V#v}"
CUR_TAG="${GITHUB_REF#refs/tags/}"
PREV_TAG=$(git tag --sort=-creatordate | sed -n '2p' || true)
if [ -n "$PREV_TAG" ]; then
CHANGES=$(git log --pretty=format:'- %s (%h)' "${PREV_TAG}..${CUR_TAG}" || true)
else
CHANGES=$(git log --pretty=format:'- %s (%h)' -n 100 || true)
fi
[ -z "$CHANGES" ] && CHANGES="- 初始发布"
cat > release_notes.md << EOF
## 🌙 Moonlight V+ ${V}
感谢你使用 Moonlight V+!如果喜欢,记得点亮一颗 ⭐~
### ✨ 更新内容
${CHANGES}
### 📦 下载
- APK 文件:Moonlight.V+.${V}.apk
### 🧭 另外
- 如遇到问题,请在 Issues 反馈。祝你游戏顺利、低延迟满帧!🎮
—— 发布于 ${DATE}
EOF
- name: Create GitHub Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ github.ref_name }}
name: ${{ env.VERSION_NAME != '' && env.VERSION_NAME || github.ref_name }}
draft: false
prerelease: ${{ contains(github.ref_name, '-rc') || contains(github.ref_name, '-beta') }}
body_path: release_notes.md
files: |
${{ env.REL_APK }}
app/build/outputs/mapping/nonRootRelease/mapping.txt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Release APK and mapping
if: startsWith(github.ref, 'refs/tags/')
uses: actions/upload-artifact@v4
with:
name: app-nonroot-release-signed
path: |
app/build/outputs/apk/nonRoot/release/Moonlight.V+.*.apk
app/build/outputs/mapping/nonRootRelease/mapping.txt