better preview versions - real npm registry #44
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: Package Preview Publish | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| jobs: | |
| publish-preview: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Enable corepack | |
| run: corepack enable | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: "npm" | |
| registry-url: "https://registry.npmjs.org" | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Build package | |
| run: npm run build | |
| - name: Generate preview package name and version | |
| id: preview_info | |
| run: | | |
| PR_NUMBER="${{ github.event.number }}" | |
| COMMIT_HASH="${{ github.sha }}" | |
| SHORT_COMMIT="${COMMIT_HASH:0:7}" | |
| if [ ! -z "$PR_NUMBER" ]; then | |
| PREVIEW_VERSION="pr-$PR_NUMBER" | |
| else | |
| PREVIEW_VERSION="commit-$SHORT_COMMIT" | |
| fi | |
| echo "version=$PREVIEW_VERSION" >> $GITHUB_OUTPUT | |
| echo "package_name=@base44/preview-sdk" >> $GITHUB_OUTPUT | |
| echo "full_package=@base44/preview-sdk@$PREVIEW_VERSION" >> $GITHUB_OUTPUT | |
| - name: Update package.json for preview | |
| run: | | |
| # Create a backup of original package.json | |
| cp package.json package.json.bak | |
| # Update name and version for preview | |
| npm pkg set name="${{ steps.preview_info.outputs.package_name }}" | |
| npm pkg set version="${{ steps.preview_info.outputs.version }}" | |
| - name: Publish preview package | |
| run: npm publish | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Restore original package.json | |
| run: mv package.json.bak package.json | |
| - name: Comment PR with install instructions | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const fullPackage = '${{ steps.preview_info.outputs.full_package }}'; | |
| const installCmd = `npm i ${fullPackage}`; | |
| const body = `### π Package Preview Available! | |
| --- | |
| **Install this PR's preview build with npm:** | |
| \`\`\`sh | |
| ${installCmd} | |
| \`\`\` | |
| - π¦ **Preview Package**: \`${fullPackage}\` | |
| - π [View this commit on GitHub](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) | |
| --- | |
| <sub>Preview published to npm registry β try new features instantly!</sub> | |
| `; | |
| const botCommentIdentifier = '### π Package Preview Available!'; | |
| async function findBotComment(issueNumber) { | |
| if (!issueNumber) return null; | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issueNumber, | |
| }); | |
| return comments.data.find((comment) => | |
| comment.body.includes(botCommentIdentifier) | |
| ); | |
| } | |
| async function createOrUpdateComment(issueNumber) { | |
| if (!issueNumber) { | |
| console.log('No issue number provided. Cannot post or update comment.'); | |
| return; | |
| } | |
| const existingComment = await findBotComment(issueNumber); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| issue_number: issueNumber, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: body, | |
| }); | |
| } | |
| } | |
| if (context.eventName === 'pull_request') { | |
| if (context.issue.number) { | |
| await createOrUpdateComment(context.issue.number); | |
| } | |
| } | |
| permissions: | |
| contents: read | |
| pull-requests: write |