Auto Update USB.IDS #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Update USB.IDS | |
| permissions: | |
| id-token: write | |
| contents: write | |
| pull-requests: write | |
| on: | |
| schedule: | |
| # 每天UTC 0点执行 | |
| - cron: '0 0 * * *' | |
| workflow_dispatch: # 允许手动触发 | |
| jobs: | |
| auto-update: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - 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: '18' | |
| 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: Fetch USB IDs | |
| run: pnpm run fetch-usb-ids | |
| - name: Check if update is needed | |
| id: check-update | |
| run: | | |
| # 获取当前生成的 contentHash | |
| CURRENT_HASH=$(node -p "require('./usb.ids.version.json').contentHash") | |
| echo "Current hash: $CURRENT_HASH" | |
| # 创建临时目录并安装最新版本的 usb.ids 包 | |
| 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 | |
| # if [ "$CURRENT_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" | |
| # echo "skip=false" >> $GITHUB_OUTPUT | |
| # fi | |
| - name: Build project | |
| if: steps.check-update.outputs.skip != 'true' | |
| run: pnpm run build | |
| - name: Update package.json version | |
| if: steps.check-update.outputs.skip != 'true' | |
| run: | | |
| # 从 usb.ids.version.json 获取版本号并去掉 v 前缀 | |
| 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'); | |
| " | |
| # 配置 git 用户信息 | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| # 提交更改 | |
| git add package.json | |
| git commit -m "chore: update to version $NEW_VERSION" | |
| - 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: Publish to npm | |
| if: steps.check-update.outputs.skip != 'true' | |
| run: pnpm publish --no-git-checks | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |