Skip to content

Enhance npm publishing workflows and version bump #3

Enhance npm publishing workflows and version bump

Enhance npm publishing workflows and version bump #3

name: NPM Publish Beta
on:
push:
branches:
- develop
pull_request:
branches:
- master
jobs:
publish-beta:
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: Generate beta version
id: beta-version
run: |
# 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 }}.$TIMESTAMP"
else
# For develop: use commit hash
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
BETA_VERSION="$CURRENT_VERSION-beta.$SHORT_SHA.$TIMESTAMP"
fi
# Set output for use in next step
echo "version=$BETA_VERSION" >> $GITHUB_OUTPUT
# Update package.json with new version
npm version $BETA_VERSION --no-git-tag-version
- name: Publish beta to npm
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 }}