-
-
Notifications
You must be signed in to change notification settings - Fork 0
185 lines (156 loc) · 6.15 KB
/
auto-update.yml
File metadata and controls
185 lines (156 loc) · 6.15 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
name: Auto Update USB.IDS
permissions:
id-token: write
contents: write
pull-requests: write
on:
schedule:
# 每天UTC 0点执行
- cron: '0 0 * * *'
workflow_dispatch: # 允许手动触发
jobs:
check-and-update:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
cache: pnpm
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: |
pnpm install
# 确保 jq 可用
if ! command -v jq &> /dev/null; then
sudo apt-get update
sudo apt-get install -y jq
fi
- name: Get current version info
id: current-version
run: |
if [ -f "usb.ids.version.json" ]; then
CURRENT_HASH=$(jq -r '.contentHash' usb.ids.version.json)
echo "current_hash=$CURRENT_HASH" >> $GITHUB_OUTPUT
echo "has_version=true" >> $GITHUB_OUTPUT
else
echo "has_version=false" >> $GITHUB_OUTPUT
fi
- name: Fetch latest USB.IDS data
run: pnpm run fetch-usb-ids --force
- name: Get new version info
id: new-version
run: |
NEW_HASH=$(jq -r '.contentHash' usb.ids.version.json)
NEW_VERSION=$(jq -r '.version' usb.ids.version.json)
echo "new_hash=$NEW_HASH" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Check if update needed
id: check-update
run: |
if [ "${{ steps.current-version.outputs.has_version }}" = "false" ] || [ "${{ steps.current-version.outputs.current_hash }}" != "${{ steps.new-version.outputs.new_hash }}" ]; then
echo "needs_update=true" >> $GITHUB_OUTPUT
echo "USB.IDS data has changed, update needed"
else
echo "needs_update=false" >> $GITHUB_OUTPUT
echo "USB.IDS data unchanged, no update needed"
fi
- name: Update version and regenerate data
if: steps.check-update.outputs.needs_update == 'true'
run: |
# 重新生成版本信息(会自动更新package.json版本)
pnpm run fetch-usb-ids --force
# 验证生成的文件
if [ ! -f "usb.ids.version.json" ]; then
echo "Error: usb.ids.version.json not generated"
exit 1
fi
- name: Build project
if: steps.check-update.outputs.needs_update == 'true'
run: |
pnpm run build
# 验证构建产物
if [ ! -d "dist" ]; then
echo "Error: Build output directory 'dist' not found"
exit 1
fi
- name: Commit and tag
if: steps.check-update.outputs.needs_update == 'true'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# 获取新的版本号(从生成的版本文件中读取)
NEW_VERSION=$(jq -r '.version' usb.ids.version.json)
if [ "$NEW_VERSION" = "null" ] || [ -z "$NEW_VERSION" ]; then
echo "Error: Failed to get version from usb.ids.version.json"
exit 1
fi
echo "NEW_VERSION=v${NEW_VERSION}" >> $GITHUB_ENV
# 检查是否有变更需要提交
if git diff --quiet && git diff --cached --quiet; then
echo "No changes to commit"
exit 0
fi
# 提交所有变更
git add .
git commit -m "chore: update USB.IDS data to v${NEW_VERSION}"
# 创建标签(添加v前缀)
git tag "v${NEW_VERSION}"
# 推送变更和标签
git push origin main
git push origin "v${NEW_VERSION}"
- name: Get release info
if: steps.check-update.outputs.needs_update == 'true'
id: release-info
run: |
VENDOR_COUNT=$(jq -r '.vendorCount' usb.ids.version.json)
DEVICE_COUNT=$(jq -r '.deviceCount' usb.ids.version.json)
UPDATE_TIME=$(jq -r '.fetchTimeFormatted' usb.ids.version.json)
echo "vendor_count=${VENDOR_COUNT}" >> $GITHUB_OUTPUT
echo "device_count=${DEVICE_COUNT}" >> $GITHUB_OUTPUT
echo "update_time=${UPDATE_TIME}" >> $GITHUB_OUTPUT
- name: Generate Changelog
if: steps.check-update.outputs.needs_update == 'true'
run: pnpm dlx changelogithub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Release
if: steps.check-update.outputs.needs_update == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.NEW_VERSION }}
name: Release ${{ env.NEW_VERSION }}
body: |
🔄 自动更新USB.IDS数据
📊 数据统计:
- 供应商数量: ${{ steps.release-info.outputs.vendor_count }}
- 设备数量: ${{ steps.release-info.outputs.device_count }}
🕒 更新时间: ${{ steps.release-info.outputs.update_time }}
🔗 数据来源: http://www.linux-usb.org/usb.ids
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to npm
if: steps.check-update.outputs.needs_update == 'true'
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Summary
run: |
if [ "${{ steps.check-update.outputs.needs_update }}" = "true" ]; then
echo "✅ USB.IDS data updated and new version released"
VERSION=$(jq -r '.version' usb.ids.version.json)
echo "📦 New version: $VERSION"
echo "🔗 Release: https://github.com/${{ github.repository }}/releases/tag/v$VERSION"
echo "📊 NPM: https://www.npmjs.com/package/usb.ids"
else
echo "ℹ️ No update needed - USB.IDS data is already up to date"
fi