Skip to content

NPM Publish and Release #21

NPM Publish and Release

NPM Publish and Release #21

Workflow file for this run

name: NPM Publish and Release
on:
workflow_dispatch:
inputs:
version_type:
description: 'Version bump type'
required: true
default: 'patch'
type: choice
options:
- major
- minor
- patch
changelog:
description: 'Release changelog (required)'
required: true
type: string
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Validate changelog input
run: |
if [ -z "${{ github.event.inputs.changelog }}" ]; then
echo "❌ Changelog is required for release"
echo "Please provide a detailed changelog describing the changes in this release."
exit 1
fi
echo "✅ Changelog provided"
publish:
runs-on: ubuntu-latest
needs: validate
permissions:
contents: write
packages: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
registry-url: 'https://registry.npmjs.org'
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9.0.0
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Install dependencies
run: pnpm install
- name: Install Playwright browsers
run: pnpm --dir example/test-fast-md5 exec playwright install --with-deps chromium
- name: Run linting
run: pnpm run lint
- name: Run type checking
run: pnpm run type-check
- name: Run formatting check
run: pnpm run format:check
- name: Configure git
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Bump version
id: version
run: |
# Get current version
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current version: $CURRENT_VERSION"
# Bump version based on input
case "${{ github.event.inputs.version_type }}" in
"major")
NEW_VERSION=$(npm version major --no-git-tag-version)
;;
"minor")
NEW_VERSION=$(npm version minor --no-git-tag-version)
;;
"patch")
NEW_VERSION=$(npm version patch --no-git-tag-version)
;;
esac
# Remove 'v' prefix from npm version output
NEW_VERSION=${NEW_VERSION#v}
echo "New version: $NEW_VERSION"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
- name: Generate changelog file
run: |
# Create or update CHANGELOG.md
if [ ! -f CHANGELOG.md ]; then
echo "# Changelog" > CHANGELOG.md
echo "" >> CHANGELOG.md
echo "All notable changes to this project will be documented in this file." >> CHANGELOG.md
echo "" >> CHANGELOG.md
fi
# Prepare new changelog entry
TODAY=$(date +"%Y-%m-%d")
NEW_ENTRY="## [${{ steps.version.outputs.version }}] - $TODAY"
# Create temporary file with new entry
cat > temp_changelog.md << EOF
# Changelog
All notable changes to this project will be documented in this file.
$NEW_ENTRY
${{ github.event.inputs.changelog }}
EOF
# Append existing changelog (skip header)
if [ -f CHANGELOG.md ]; then
tail -n +4 CHANGELOG.md >> temp_changelog.md
fi
# Replace original changelog
mv temp_changelog.md CHANGELOG.md
- name: Build project
run: |
pnpm run build
- name: Run tests (if available)
run: |
if [ -f "package.json" ] && grep -q '"test"' package.json; then
pnpm test
else
echo "No tests found, skipping..."
fi
continue-on-error: false
- name: Publish to NPM
run: pnpm publish --access public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- name: Commit and push changes
run: |
# Commit version change and changelog after successful NPM publish
git add package.json CHANGELOG.md
git commit -m "chore: release v${{ steps.version.outputs.version }}
${{ github.event.inputs.changelog }}"
git push origin main
- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ steps.version.outputs.version }}
name: Release v${{ steps.version.outputs.version }}
body: |
## Release v${{ steps.version.outputs.version }}
${{ github.event.inputs.changelog }}
---
### 📦 Package Information
- **Version**: ${{ steps.version.outputs.version }}
- **Previous Version**: ${{ steps.version.outputs.current_version }}
- **NPM Package**: [fast-md5-web](https://www.npmjs.com/package/fast-md5-web)
- **Build Type**: ${{ github.event.inputs.version_type }} version bump
- **Release Date**: ${{ steps.date.outputs.date }}
### 📥 Installation
```bash
npm install fast-md5-web@${{ steps.version.outputs.version }}
# or
pnpm add fast-md5-web@${{ steps.version.outputs.version }}
# or
yarn add fast-md5-web@${{ steps.version.outputs.version }}
```
### 📋 Full Changelog
**Full Changelog**: https://github.com/${{ github.repository }}/compare/v${{ steps.version.outputs.current_version }}...v${{ steps.version.outputs.version }}
draft: false
prerelease: false
generate_release_notes: true
notify:
runs-on: ubuntu-latest
needs: publish
if: success()
steps:
- name: Release notification
run: |
echo "🎉 Successfully released v${{ needs.publish.outputs.version }}"
echo "📦 NPM: https://www.npmjs.com/package/fast-md5-web"
echo "🏷️ GitHub Release: https://github.com/${{ github.repository }}/releases/tag/v${{ needs.publish.outputs.version }}"