-
-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (136 loc) · 5.45 KB
/
auto-update.yml
File metadata and controls
165 lines (136 loc) · 5.45 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
name: Auto Update USB.IDS
permissions:
id-token: write
contents: write
pull-requests: write
on:
schedule:
# 每天UTC 2:30执行(避开高峰期)
- cron: '30 2 * * *'
workflow_dispatch: # 允许手动触发
jobs:
auto-update:
runs-on: ubuntu-latest
timeout-minutes: 30 # 设置超时时间为30分钟
steps:
- name: Record execution time
run: |
echo "Workflow started at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "Scheduled time was: $(date -u -d 'today 02:30' '+%Y-%m-%d %H:%M:%S UTC')"
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
cache: pnpm
registry-url: 'https://registry.npmjs.org'
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9.15.9
- name: Install dependencies
run: pnpm install
- name: Check if update is needed
id: check-update
run: |
# 获取npm最新版本的contentHash作为基准
mkdir -p /tmp/npm-check
cd /tmp/npm-check
npm install usb.ids --silent 2>/dev/null || true
# 获取 npm 最新版本的 contentHash
if [ -f "node_modules/usb.ids/usb.ids.version.json" ]; then
NPM_HASH=$(node -p "require('./node_modules/usb.ids/usb.ids.version.json').contentHash" 2>/dev/null || echo "")
else
NPM_HASH=""
fi
echo "NPM hash: $NPM_HASH"
# 清理临时目录
cd /
rm -rf /tmp/npm-check
# 回到工作目录,获取远程数据但不保存,只计算hash
cd $GITHUB_WORKSPACE
# 下载远程数据并计算contentHash(不保存文件)
REMOTE_CONTENT=$(curl -s "http://www.linux-usb.org/usb.ids" || curl -s "https://raw.githubusercontent.com/systemd/systemd/main/hwdb.d/usb.ids" || echo "")
if [ -n "$REMOTE_CONTENT" ]; then
# 使用Node.js计算SHA256 hash
REMOTE_HASH=$(node -e "const crypto = require('crypto'); console.log(crypto.createHash('sha256').update(process.argv[1]).digest('hex'));" "$REMOTE_CONTENT")
echo "Remote hash: $REMOTE_HASH"
if [ "$REMOTE_HASH" = "$NPM_HASH" ] && [ -n "$NPM_HASH" ]; then
echo "No update needed, contentHash is the same"
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "Update needed, contentHash is different or no previous version"
echo "skip=false" >> $GITHUB_OUTPUT
fi
else
echo "Failed to fetch remote data, forcing update"
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Fetch USB IDs
if: steps.check-update.outputs.skip != 'true'
run: pnpm run fetch-usb-ids
- name: Build Lib
if: steps.check-update.outputs.skip != 'true'
run: pnpm run build:lib
- name: Build UI
if: steps.check-update.outputs.skip != 'true'
run: pnpm run build:app --outDir dist/ui
- name: Upload build artifacts
if: steps.check-update.outputs.skip != 'true'
uses: actions/upload-artifact@v4
with:
name: usb-ids-build-${{ github.run_number }}
path: |
dist/
usb.ids.json
usb.ids.version.json
retention-days: 30
- name: Update package.json version and README
if: steps.check-update.outputs.skip != 'true'
run: |
# 从 usb.ids.version.json 获取版本号
NEW_VERSION=$(node -p "require('./usb.ids.version.json').version.replace(/^v/, '')")
echo "New version: $NEW_VERSION"
# 更新 package.json 中的版本
node -e "
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
pkg.version = '$NEW_VERSION';
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
"
# 使用专门的脚本更新 README.md 版本信息
pnpm run update-readme-version
# 配置 git 用户信息
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# 提交更改
git add package.json README.md
git commit -m "chore: update to version $NEW_VERSION
- Updated package.json version to $NEW_VERSION
- Updated README.md with latest version info
- Data updated automatically"
- name: Create and push tag
if: steps.check-update.outputs.skip != 'true'
run: |
# 从 usb.ids.version.json 获取完整版本号作为标签
TAG_VERSION=$(node -p "require('./usb.ids.version.json').version")
echo "Creating tag: $TAG_VERSION"
# 创建标签
git tag "$TAG_VERSION"
# 推送提交和标签
git push origin main
git push origin "$TAG_VERSION"
- name: Generate changelog
if: steps.check-update.outputs.skip != 'true'
run: pnpm dlx changelogithub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to npm
if: steps.check-update.outputs.skip != 'true'
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}