release.yml 5.3 KB

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