Merge pull request #5 from Space48/feature/enhance-npm-publishing-wor… #4
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: NPM Publish | |
| on: | |
| push: | |
| branches: | |
| - master | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '18.x' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Get package version | |
| id: package-version | |
| run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT | |
| - name: Publish to npm | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Find related PRs and comment | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const version = process.env.PACKAGE_VERSION; | |
| const packageName = '@space48/sdm'; | |
| const installCmd = `npm install ${packageName}@${version}`; | |
| // Get the commit message to find PR number | |
| const { data: commit } = await github.rest.repos.getCommit({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: context.sha | |
| }); | |
| // Look for PR references in the commit message (like "Merge pull request #123") | |
| const prMatch = commit.commit.message.match(/Merge pull request #(\d+)/i); | |
| if (prMatch && prMatch[1]) { | |
| const prNumber = parseInt(prMatch[1], 10); | |
| // Comment on the PR | |
| await github.rest.issues.createComment({ | |
| issue_number: prNumber, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: `🚀 New version published: **${version}**\n\nThis version has been published to npm and is now available.\n\nYou can install it with:\n\`\`\`bash\n${installCmd}\n\`\`\`` | |
| }); | |
| console.log(`Commented on PR #${prNumber} about the new release ${version}`); | |
| } else { | |
| console.log('No PR reference found in the commit message. Skipping comment.'); | |
| } | |
| env: | |
| PACKAGE_VERSION: ${{ steps.package-version.outputs.version }} |