Auto Update USB.IDS #202
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:00执行 | |
| - cron: '0 0 * * *' | |
| workflow_dispatch: # 允许手动触发 | |
| inputs: | |
| update_mode: | |
| description: 'Update mode: auto (check hash), force (skip hash check), skip (no update)' | |
| required: false | |
| default: auto | |
| type: choice | |
| options: | |
| - auto | |
| - force | |
| - skip | |
| 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 pnpm | |
| uses: pnpm/action-setup@v4 | |
| with: | |
| version: 9.15.9 | |
| - 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 | |
| - name: Check if update is needed | |
| id: check-update | |
| run: | | |
| # 检查触发方式和用户输入参数 | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| echo "Manual trigger detected" | |
| UPDATE_MODE="${{ github.event.inputs.update_mode }}" | |
| case "$UPDATE_MODE" in | |
| "skip") | |
| echo "User chose to skip all operations" | |
| echo "skip=true" >> $GITHUB_OUTPUT | |
| ;; | |
| "force") | |
| echo "User chose to force update, skipping hash check" | |
| echo "skip=false" >> $GITHUB_OUTPUT | |
| ;; | |
| "auto"|*) | |
| echo "User chose auto mode, checking for updates..." | |
| # 使用TypeScript脚本进行哈希差异检查,避免shell参数过长问题 | |
| if pnpm run diff-hash; then | |
| echo "No update needed, content hash is the same" | |
| echo "skip=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Update needed, content hash is different or error occurred" | |
| echo "skip=false" >> $GITHUB_OUTPUT | |
| fi | |
| ;; | |
| esac | |
| else | |
| echo "Scheduled trigger detected, checking for updates..." | |
| # 使用TypeScript脚本进行哈希差异检查,避免shell参数过长问题 | |
| if pnpm run diff-hash; then | |
| echo "No update needed, content hash is the same" | |
| echo "skip=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Update needed, content hash is different or error occurred" | |
| echo "skip=false" >> $GITHUB_OUTPUT | |
| fi | |
| 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'); | |
| " | |
| # 配置 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 | |
| - Updated package.json version to $NEW_VERSION | |
| - 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 (trusted publishing via OIDC) | |
| if: steps.check-update.outputs.skip != 'true' | |
| run: npm publish |