-
-
Notifications
You must be signed in to change notification settings - Fork 285
262 lines (221 loc) · 9.02 KB
/
ci.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
253
254
255
256
257
258
259
260
261
262
name: CI Workflow
on:
push:
branches:
- "*" # 所有分支触发
tags:
- 'v*'
workflow_dispatch:
env:
TEST_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:${{ github.ref_name }}
LATEST_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:latest
STABLE_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/xiaomusic:stable
permissions:
contents: write
id-token: write
jobs:
# Job 构建镜像
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Docker image (linux/amd64)
id: build_amd64
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-amd64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Build Docker image (linux/arm64)
id: build_arm64
uses: docker/build-push-action@v6
with:
platforms: linux/arm64
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Build Docker image (linux/arm/v7)
id: build_armv7
uses: docker/build-push-action@v6
with:
platforms: linux/arm/v7
context: .
push: false
load: true
tags: ${{ env.TEST_TAG }}-linux-arm-v7
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: List Docker images
run: docker images
- name: Test amd64 image
run: |
docker run --rm ${{ env.TEST_TAG }}-linux-amd64 /app/.venv/bin/python3 /app/xiaomusic.py -h
- name: Test arm64 image
run: |
docker run --rm ${{ env.TEST_TAG }}-linux-arm64 /app/.venv/bin/python3 /app/xiaomusic.py -h
- name: Test armv7 image
run: |
docker run --rm ${{ env.TEST_TAG }}-linux-arm-v7 /app/.venv/bin/python3 /app/xiaomusic.py -h
- name: Docker Hub Description
if: github.ref == 'refs/heads/main'
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: hanxi/xiaomusic
# 发布 PyPI 版本
# 仅在 ref 为以 v 开头的标签时执行
- uses: actions/setup-node@v4
if: startsWith(github.ref, 'refs/tags/v')
with:
registry-url: https://registry.npmjs.org/
node-version: lts/*
- run: npx changelogithub
if: startsWith(github.ref, 'refs/tags/v')
continue-on-error: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- uses: pdm-project/setup-pdm@v3
if: startsWith(github.ref, 'refs/tags/v')
- name: Publish package distributions to PyPI
if: startsWith(github.ref, 'refs/tags/v')
run: pdm publish
continue-on-error: true
# Job 打包应用, 发布镜像和 Release
# 仅在 main 分支或以 v 开头的标签运行
- name: Package /app for amd64
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-amd64 tar czf /workspace/app-amd64.tar.gz -C / app
- name: Package /app (lite) for amd64
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-amd64 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-amd64-lite.tar.gz .[!.]* *"
- name: Package /app for arm64
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-arm64 tar czf /workspace/app-arm64.tar.gz -C / app
- name: Package /app (lite) for arm64
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-arm64 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm64-lite.tar.gz .[!.]* *"
- name: Package /app for arm/v7
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-arm-v7 tar czf /workspace/app-arm-v7.tar.gz -C / app
- name: Package /app (lite) for arm/v7
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
docker run --rm -v $PWD:/workspace ${{ env.TEST_TAG }}-linux-arm-v7 bash -c \
"cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm-v7-lite.tar.gz .[!.]* *"
- name: Publish to Docker Hub main
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
push: true
tags: ${{ env.TEST_TAG }}
cache-from: type=local,src=/tmp/.buildx-cache-new
cache-to: type=local,dest=/tmp/.buildx-cache-new
# 仅在 ref 为以 v 开头的标签时执行
- name: Publish to Docker Hub latest and stable
if: startsWith(github.ref, 'refs/tags/v')
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
push: true
tags: ${{ env.TEST_TAG }}, ${{ env.LATEST_TAG }}, ${{ env.STABLE_TAG }}
cache-from: type=local,src=/tmp/.buildx-cache-new
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Move cache to limit growth
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
# 上传文件到 release 页面
- name: Install GitHub CLI
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: |
sudo apt update
sudo apt install -y gh
# 创建或更新 Release
- name: Create or update Release main
if: github.ref == 'refs/heads/main'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 设置 GH_TOKEN 环境变量
run: |
RELEASE_NAME=test
RELEASE_BODY="This release is automatically updated from the main branch."
# 检查是否已有同名 Release
EXISTING_RELEASE=$(gh release view "${RELEASE_NAME}" --json id --jq .id || echo "")
if [[ -n "${EXISTING_RELEASE}" ]]; then
echo "release exist"
else
# 创建新的 Release
gh release create "${RELEASE_NAME}" \
--prerelease=false \
--title "${RELEASE_NAME}" \
--notes "${RELEASE_BODY}"
fi
- name: Create or update Release tag
if: startsWith(github.ref, 'refs/tags/v')
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 设置 GH_TOKEN 环境变量
run: |
RELEASE_NAME=${{ github.ref_name }}
RELEASE_BODY="This release is automatically updated from the ${RELEASE_NAME} branch."
# 检查是否已有同名 Release
EXISTING_RELEASE=$(gh release view "${RELEASE_NAME}" --json id --jq .id || echo "")
if [[ -n "${EXISTING_RELEASE}" ]]; then
echo "release exist"
else
# 创建新的 Release
gh release create "${RELEASE_NAME}" \
--prerelease=false \
--title "${RELEASE_NAME}" \
--notes "${RELEASE_BODY}"
fi
# 上传多个文件并覆盖
- name: Upload assets to Release main
if: github.ref == 'refs/heads/main'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 设置 GH_TOKEN 环境变量
run: |
RELEASE_NAME=test
FILES=$(find . -type f -name "app-*.tar.gz")
for FILE in ${FILES}; do
echo "type upload ${FILE}"
gh release upload "${RELEASE_NAME}" "${FILE}" --clobber
done
- name: Upload assets to Release tag
if: startsWith(github.ref, 'refs/tags/v')
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 设置 GH_TOKEN 环境变量
run: |
RELEASE_NAME=${{ github.ref_name }}
FILES=$(find . -type f -name "app-*.tar.gz")
for FILE in ${FILES}; do
echo "type upload ${FILE}"
gh release upload "${RELEASE_NAME}" "${FILE}" --clobber
done