Skip to content

Auto Update USB.IDS

Auto Update USB.IDS #5

Workflow file for this run

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