ci: add test package publishing workflow #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: CI | |
| on: | |
| push: | |
| pull_request: | |
| branches: [main, master] | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| node-version: [18.x, 20.x, 22.x] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Use Node.js ${{ matrix.node-version }} | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ matrix.node-version }} | |
| - name: Clean and install dependencies | |
| run: | | |
| rm -rf node_modules package-lock.json | |
| npm install | |
| - name: Type check | |
| run: npm run typecheck | |
| - name: Lint | |
| run: npm run lint | |
| - name: Test (unit tests only) | |
| run: npm run test -- --testPathPattern="unittests" | |
| - name: Build | |
| run: npm run build | |
| coverage: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Use Node.js 18.x | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 18.x | |
| - name: Clean and install dependencies | |
| run: | | |
| rm -rf node_modules package-lock.json | |
| npm install | |
| - name: Run tests with coverage (unit tests only) | |
| run: npm run test:coverage -- --testPathPattern="unittests" | |
| - name: Upload coverage to Codecov | |
| uses: codecov/codecov-action@v4 | |
| with: | |
| token: ${{ secrets.CODECOV_TOKEN }} | |
| files: ./coverage/coverage-final.json | |
| fail_ci_if_error: false | |
| # 自动发布测试包(所有分支 push 且测试通过时) | |
| publish-test: | |
| name: Publish Test Package | |
| runs-on: ubuntu-latest | |
| needs: [build, coverage] | |
| if: github.event_name == 'push' | |
| permissions: | |
| contents: write | |
| id-token: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| registry-url: 'https://registry.npmjs.org' | |
| always-auth: true | |
| - name: Determine version | |
| id: version | |
| run: | | |
| # 测试包名 | |
| PACKAGE_NAME="@agentrun/sdk-test" | |
| echo "PACKAGE_NAME=${PACKAGE_NAME}" >> $GITHUB_OUTPUT | |
| # 从 npm 获取当前最新版本 | |
| NPM_RESPONSE=$(npm view "$PACKAGE_NAME" version 2>/dev/null || echo "") | |
| if [ -z "$NPM_RESPONSE" ]; then | |
| CURRENT_VERSION="0.0.0" | |
| echo "Package not found on npm, starting from 0.0.0" | |
| else | |
| CURRENT_VERSION="$NPM_RESPONSE" | |
| echo "Latest version on npm: $CURRENT_VERSION" | |
| fi | |
| # 解析版本号并递增 patch | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" | |
| PATCH=$((PATCH + 1)) | |
| VERSION="${MAJOR}.${MINOR}.${PATCH}" | |
| echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT | |
| echo "New version: ${VERSION}" | |
| - name: Update package.json for test publish | |
| run: | | |
| VERSION="${{ steps.version.outputs.VERSION }}" | |
| PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}" | |
| node -e " | |
| const fs = require('fs'); | |
| const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); | |
| pkg.name = process.env.PACKAGE_NAME; | |
| pkg.version = process.env.VERSION; | |
| fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n'); | |
| " | |
| echo "Updated package.json:" | |
| cat package.json | head -10 | |
| env: | |
| VERSION: ${{ steps.version.outputs.VERSION }} | |
| PACKAGE_NAME: ${{ steps.version.outputs.PACKAGE_NAME }} | |
| - name: Clean and install dependencies | |
| run: | | |
| rm -rf node_modules package-lock.json | |
| npm install | |
| - name: Build | |
| run: npm run build | |
| - name: Publish test package | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}" | |
| VERSION="${{ steps.version.outputs.VERSION }}" | |
| echo "Publishing ${PACKAGE_NAME}@${VERSION} with tag=test" | |
| npm publish --tag test --access public | |
| - name: Summary | |
| run: | | |
| PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}" | |
| VERSION="${{ steps.version.outputs.VERSION }}" | |
| echo "## 🧪 Test Package Published!" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Package:** ${PACKAGE_NAME}@${VERSION}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "Install: \`npm install ${PACKAGE_NAME}@${VERSION}\`" >> $GITHUB_STEP_SUMMARY |