release.yml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. name: Release
  2. on:
  3. push:
  4. branches:
  5. - main
  6. workflow_dispatch:
  7. concurrency:
  8. group: ${{ github.workflow }}-${{ github.ref }}
  9. cancel-in-progress: false
  10. permissions:
  11. contents: read
  12. jobs:
  13. check-release:
  14. name: Check for new version
  15. runs-on: ubuntu-latest
  16. timeout-minutes: 5
  17. outputs:
  18. should_release: ${{ steps.check.outputs.should_release }}
  19. needs_github_release: ${{ steps.check.outputs.needs_github_release }}
  20. version: ${{ steps.check.outputs.version }}
  21. steps:
  22. - name: Checkout repository
  23. uses: actions/checkout@v4
  24. - name: Compare package.json version to npm and check GitHub release
  25. id: check
  26. run: |
  27. LOCAL_VERSION=$(node -p "require('./packages/core/package.json').version")
  28. echo "Local version: $LOCAL_VERSION"
  29. NPM_VERSION=$(npm view @json-render/core version 2>/dev/null || echo "0.0.0")
  30. echo "npm version: $NPM_VERSION"
  31. if [ "$LOCAL_VERSION" != "$NPM_VERSION" ]; then
  32. echo "Version changed: $NPM_VERSION -> $LOCAL_VERSION"
  33. echo "should_release=true" >> "$GITHUB_OUTPUT"
  34. echo "needs_github_release=true" >> "$GITHUB_OUTPUT"
  35. else
  36. echo "Version unchanged on npm, skipping build and publish"
  37. echo "should_release=false" >> "$GITHUB_OUTPUT"
  38. TAG="v$LOCAL_VERSION"
  39. if gh release view "$TAG" &>/dev/null; then
  40. echo "GitHub release $TAG exists"
  41. echo "needs_github_release=false" >> "$GITHUB_OUTPUT"
  42. else
  43. echo "GitHub release $TAG is missing, will create it"
  44. echo "needs_github_release=true" >> "$GITHUB_OUTPUT"
  45. fi
  46. fi
  47. echo "version=$LOCAL_VERSION" >> "$GITHUB_OUTPUT"
  48. env:
  49. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  50. publish:
  51. name: Publish to npm
  52. needs: check-release
  53. if: needs.check-release.outputs.should_release == 'true'
  54. runs-on: ubuntu-latest
  55. timeout-minutes: 15
  56. environment: Release
  57. permissions:
  58. contents: read
  59. id-token: write
  60. steps:
  61. - name: Checkout repository
  62. uses: actions/checkout@v4
  63. - name: Install pnpm
  64. uses: pnpm/action-setup@v4
  65. - name: Setup Node.js
  66. uses: actions/setup-node@v4
  67. with:
  68. node-version: 24
  69. cache: pnpm
  70. registry-url: "https://registry.npmjs.org"
  71. - name: Install dependencies
  72. run: pnpm install --frozen-lockfile
  73. - name: Build packages
  74. run: pnpm run build
  75. - name: Publish all public packages
  76. run: |
  77. LOCAL_VERSION="${{ needs.check-release.outputs.version }}"
  78. FAILED=""
  79. publish_pkg() {
  80. local dir="$1" name="$2"
  81. REGISTRY_VERSION=$(npm view "$name" version 2>/dev/null || echo "0.0.0")
  82. if [ "$LOCAL_VERSION" = "$REGISTRY_VERSION" ]; then
  83. echo "$name@$LOCAL_VERSION already published, skipping"
  84. return 0
  85. fi
  86. echo "Publishing $name@$LOCAL_VERSION..."
  87. TARBALL=$(cd "$dir" && pnpm pack --pack-destination /tmp | tail -1)
  88. if ! npm publish "$TARBALL" --provenance --access public; then
  89. FAILED="$FAILED $name"
  90. fi
  91. }
  92. for dir in packages/*/; do
  93. PKG_NAME=$(node -p "try { const p = require('./$dir/package.json'); p.private ? '' : p.name } catch { '' }")
  94. [ -z "$PKG_NAME" ] && continue
  95. publish_pkg "$dir" "$PKG_NAME"
  96. done
  97. if [ -n "$FAILED" ]; then
  98. echo "Failed to publish:$FAILED"
  99. exit 1
  100. fi
  101. github-release:
  102. name: Create GitHub Release
  103. needs: [check-release, publish]
  104. if: >-
  105. always()
  106. && needs.check-release.outputs.needs_github_release == 'true'
  107. && (needs.publish.result == 'success' || needs.publish.result == 'skipped')
  108. runs-on: ubuntu-latest
  109. timeout-minutes: 10
  110. permissions:
  111. contents: write
  112. steps:
  113. - name: Checkout repository
  114. uses: actions/checkout@v4
  115. - name: Extract changelog entry
  116. run: |
  117. VERSION="${{ needs.check-release.outputs.version }}"
  118. awk '/<!-- release:start -->/{found=1; next} /<!-- release:end -->/{found=0} found{print}' CHANGELOG.md > /tmp/release-notes.md
  119. LINES=$(wc -l < /tmp/release-notes.md | tr -d ' ')
  120. if [ "$LINES" -lt 2 ]; then
  121. echo "Error: No release notes found between <!-- release:start --> and <!-- release:end --> markers in CHANGELOG.md"
  122. exit 1
  123. fi
  124. echo "Extracted release notes for $VERSION ($LINES lines)"
  125. - name: Create GitHub Release
  126. run: |
  127. VERSION="${{ needs.check-release.outputs.version }}"
  128. TAG="v$VERSION"
  129. if gh release view "$TAG" &>/dev/null; then
  130. echo "Release $TAG already exists"
  131. else
  132. echo "Creating release $TAG..."
  133. gh release create "$TAG" \
  134. --title "$TAG" \
  135. --target ${{ github.sha }} \
  136. --notes-file /tmp/release-notes.md
  137. fi
  138. env:
  139. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}