From 83bdfd4169d91ae64078f455a8418c95af87280e Mon Sep 17 00:00:00 2001 From: "Andrew A. Barber" Date: Wed, 14 May 2025 14:01:42 +0100 Subject: [PATCH] Enhance npm publishing workflows and version bump - Add beta publishing workflow for develop branch and PRs to master - Enhance main publishing workflow with PR commenting functionality - Bump package version to 0.4.16 --- .github/workflows/npm-publish-beta.yml | 26 ++++++++++++++-- .github/workflows/npm-publish.yml | 41 ++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/.github/workflows/npm-publish-beta.yml b/.github/workflows/npm-publish-beta.yml index 699ef70..17909a4 100644 --- a/.github/workflows/npm-publish-beta.yml +++ b/.github/workflows/npm-publish-beta.yml @@ -32,13 +32,16 @@ jobs: # Get the current version from package.json CURRENT_VERSION=$(node -p "require('./package.json').version") + # Generate a timestamp for uniqueness + TIMESTAMP=$(date +"%Y%m%d%H%M%S") + # For PR: use PR number in version if [[ "${{ github.event_name }}" == "pull_request" ]]; then - BETA_VERSION="$CURRENT_VERSION-beta.pr.${{ github.event.pull_request.number }}" + BETA_VERSION="$CURRENT_VERSION-beta.pr.${{ github.event.pull_request.number }}.$TIMESTAMP" else # For develop: use commit hash SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7) - BETA_VERSION="$CURRENT_VERSION-beta.$SHORT_SHA" + BETA_VERSION="$CURRENT_VERSION-beta.$SHORT_SHA.$TIMESTAMP" fi # Set output for use in next step @@ -51,3 +54,22 @@ jobs: run: npm publish --tag beta --access public env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Comment on PR with published version + if: github.event_name == 'pull_request' + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const version = process.env.BETA_VERSION; + const packageName = '@space48/sdm'; + const installCmd = `npm install ${packageName}@${version}`; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🚀 Beta version published: **${version}**\n\nYou can install this version with:\n\`\`\`bash\n${installCmd}\n\`\`\`` + }); + env: + BETA_VERSION: ${{ steps.beta-version.outputs.version }} diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 04986f1..53f292f 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -23,7 +23,48 @@ jobs: - 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 }} diff --git a/package.json b/package.json index 7b047a2..7597ddd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@space48/json-pipe", - "version": "0.4.15", + "version": "0.4.16", "description": "A tiny library for creating JSON pipeline CLI programs", "main": "lib/index.js", "types": "lib/index.d.ts",