Skip to content

Commit d9b07df

Browse files
committed
ci(workflow): 添加自动更新USB IDs的GitHub Actions工作流
1 parent 3e5262e commit d9b07df

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

.github/workflows/auto-update.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,107 @@ on:
1212
workflow_dispatch: # 允许手动触发
1313

1414
jobs:
15+
auto-update:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
token: ${{ secrets.GITHUB_TOKEN }}
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: '18'
28+
registry-url: 'https://registry.npmjs.org'
29+
30+
- name: Setup pnpm
31+
uses: pnpm/action-setup@v4
32+
with:
33+
version: 9.15.9
34+
35+
- name: Install dependencies
36+
run: pnpm install
37+
38+
- name: Fetch USB IDs
39+
run: pnpm run fetch-usb-ids
40+
41+
- name: Check if update is needed
42+
id: check-update
43+
run: |
44+
# 获取当前生成的 contentHash
45+
CURRENT_HASH=$(node -p "require('./usb.ids.version.json').contentHash")
46+
echo "Current hash: $CURRENT_HASH"
47+
48+
# 创建临时目录并安装最新版本的 usb.ids 包
49+
mkdir -p /tmp/npm-check
50+
cd /tmp/npm-check
51+
npm install usb.ids --silent 2>/dev/null || true
52+
53+
# 获取 npm 最新版本的 contentHash
54+
if [ -f "node_modules/usb.ids/usb.ids.version.json" ]; then
55+
NPM_HASH=$(node -p "require('./node_modules/usb.ids/usb.ids.version.json').contentHash" 2>/dev/null || echo "")
56+
else
57+
NPM_HASH=""
58+
fi
59+
echo "NPM hash: $NPM_HASH"
60+
61+
# 清理临时目录
62+
cd /
63+
rm -rf /tmp/npm-check
64+
65+
if [ "$CURRENT_HASH" = "$NPM_HASH" ] && [ -n "$NPM_HASH" ]; then
66+
echo "No update needed, contentHash is the same"
67+
echo "skip=true" >> $GITHUB_OUTPUT
68+
else
69+
echo "Update needed, contentHash is different"
70+
echo "skip=false" >> $GITHUB_OUTPUT
71+
fi
72+
73+
- name: Build project
74+
if: steps.check-update.outputs.skip != 'true'
75+
run: pnpm run build
76+
77+
- name: Update package.json version
78+
if: steps.check-update.outputs.skip != 'true'
79+
run: |
80+
# 从 usb.ids.version.json 获取版本号并去掉 v 前缀
81+
NEW_VERSION=$(node -p "require('./usb.ids.version.json').version.replace(/^v/, '')")
82+
echo "New version: $NEW_VERSION"
83+
84+
# 更新 package.json 中的版本
85+
node -e "
86+
const fs = require('fs');
87+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
88+
pkg.version = '$NEW_VERSION';
89+
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
90+
"
91+
92+
# 配置 git 用户信息
93+
git config --local user.email "action@github.com"
94+
git config --local user.name "GitHub Action"
95+
96+
# 提交更改
97+
git add package.json usb.ids.version.json
98+
git commit -m "chore: update to version $NEW_VERSION"
99+
100+
- name: Create and push tag
101+
if: steps.check-update.outputs.skip != 'true'
102+
run: |
103+
# 从 usb.ids.version.json 获取完整版本号作为标签
104+
TAG_VERSION=$(node -p "require('./usb.ids.version.json').version")
105+
echo "Creating tag: $TAG_VERSION"
106+
107+
# 创建标签
108+
git tag "$TAG_VERSION"
109+
110+
# 推送提交和标签
111+
git push origin main
112+
git push origin "$TAG_VERSION"
113+
114+
- name: Publish to npm
115+
if: steps.check-update.outputs.skip != 'true'
116+
run: pnpm publish --no-git-checks
117+
env:
118+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)