Skip to content

Commit 8719cf0

Browse files
committed
add npm action
1 parent feccc60 commit 8719cf0

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

.github/workflows/npm-publish.yml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: NPM Package Validation and Publish
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
dry_run:
7+
description: 'Dry run (validate only, do not publish)'
8+
required: true
9+
default: 'true'
10+
type: choice
11+
options:
12+
- 'true'
13+
- 'false'
14+
tag:
15+
description: 'NPM tag (latest, beta, next, etc.)'
16+
required: true
17+
default: 'latest'
18+
type: string
19+
20+
jobs:
21+
validate-and-publish:
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
28+
- name: Setup Node.js
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version: '18'
32+
registry-url: 'https://registry.npmjs.org'
33+
34+
- name: Get package info
35+
id: package
36+
run: |
37+
echo "name=$(node -p "require('./package.json').name")" >> $GITHUB_OUTPUT
38+
echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
39+
40+
- name: Clean build artifacts
41+
run: |
42+
echo "🧹 Cleaning build artifacts..."
43+
find . -name "CMakeFiles" -type d -exec rm -rf {} + 2>/dev/null || true
44+
find . -name "CMakeCache.txt" -type f -delete 2>/dev/null || true
45+
find . -name "cmake_install.cmake" -type f -delete 2>/dev/null || true
46+
find . -name "CMakeTmp" -type d -exec rm -rf {} + 2>/dev/null || true
47+
find . -name "*.log" -type f -delete 2>/dev/null || true
48+
49+
- name: Create npm package
50+
run: |
51+
echo "📦 Creating npm package..."
52+
npm pack
53+
54+
- name: Validate package contents
55+
id: validate
56+
run: |
57+
echo "🔍 Validating package contents..."
58+
PACKAGE_FILE="${{ steps.package.outputs.name }}-${{ steps.package.outputs.version }}.tgz"
59+
60+
# List all files in the package
61+
echo "📋 Package contents:"
62+
tar -tzf "$PACKAGE_FILE" | sort
63+
64+
# Check for unwanted files
65+
echo ""
66+
echo "🚫 Checking for unwanted files..."
67+
UNWANTED_FILES=$(tar -tzf "$PACKAGE_FILE" | grep -E "(CMakeFiles/|CMakeCache\.txt|cmake_install\.cmake|CMakeTmp/|\.log$|\.o$|\.obj$|\.a$|\.lib$|\.so$|\.dylib$|\.dll$|\.exe$|\.node$|\.pdb$|\.ilk$|\.exp$|\.idb$|\.tlog$|\.DS_Store|Thumbs\.db|\.swp$|\.swo$|~$)" || true)
68+
69+
if [ ! -z "$UNWANTED_FILES" ]; then
70+
echo "❌ Found unwanted files in package:"
71+
echo "$UNWANTED_FILES"
72+
echo "validation_passed=false" >> $GITHUB_OUTPUT
73+
exit 1
74+
else
75+
echo "✅ No unwanted files found"
76+
echo "validation_passed=true" >> $GITHUB_OUTPUT
77+
fi
78+
79+
# Show package size
80+
echo ""
81+
echo "📊 Package size: $(du -h "$PACKAGE_FILE" | cut -f1)"
82+
83+
# Count files
84+
FILE_COUNT=$(tar -tzf "$PACKAGE_FILE" | wc -l)
85+
echo "📁 Total files: $FILE_COUNT"
86+
87+
- name: Publish to NPM (if not dry run)
88+
if: ${{ github.event.inputs.dry_run == 'false' }}
89+
env:
90+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
91+
run: |
92+
if [ -z "$NODE_AUTH_TOKEN" ]; then
93+
echo "❌ NPM_TOKEN secret is not set. Please add it to your repository secrets."
94+
echo " Go to Settings > Secrets and variables > Actions"
95+
echo " Add a new secret named 'NPM_TOKEN' with your npm access token"
96+
echo ""
97+
echo " To create an npm token:"
98+
echo " 1. Login to npmjs.com"
99+
echo " 2. Go to Access Tokens in your account settings"
100+
echo " 3. Generate a new Classic Token with 'Publish' permissions"
101+
exit 1
102+
fi
103+
104+
echo "🚀 Publishing to npm with tag: ${{ github.event.inputs.tag }}"
105+
npm publish --tag ${{ github.event.inputs.tag }}
106+
107+
echo "✅ Published successfully!"
108+
echo "📦 View package: https://www.npmjs.com/package/${{ steps.package.outputs.name }}"
109+
110+
- name: Summary
111+
if: always()
112+
run: |
113+
echo "## 📋 Publish Summary" >> $GITHUB_STEP_SUMMARY
114+
echo "" >> $GITHUB_STEP_SUMMARY
115+
echo "- **Package**: ${{ steps.package.outputs.name }}@${{ steps.package.outputs.version }}" >> $GITHUB_STEP_SUMMARY
116+
echo "- **Tag**: ${{ github.event.inputs.tag }}" >> $GITHUB_STEP_SUMMARY
117+
echo "- **Dry Run**: ${{ github.event.inputs.dry_run }}" >> $GITHUB_STEP_SUMMARY
118+
echo "- **Validation**: ${{ steps.validate.outputs.validation_passed == 'true' && '✅ Passed' || '❌ Failed' }}" >> $GITHUB_STEP_SUMMARY
119+
120+
if [ "${{ github.event.inputs.dry_run }}" == "false" ] && [ "${{ steps.validate.outputs.validation_passed }}" == "true" ]; then
121+
echo "- **Published**: ✅ Yes" >> $GITHUB_STEP_SUMMARY
122+
echo "" >> $GITHUB_STEP_SUMMARY
123+
echo "### 🎉 Package published successfully!" >> $GITHUB_STEP_SUMMARY
124+
echo "Install with: \`npm install ${{ steps.package.outputs.name }}@${{ github.event.inputs.tag }}\`" >> $GITHUB_STEP_SUMMARY
125+
else
126+
echo "- **Published**: ❌ No" >> $GITHUB_STEP_SUMMARY
127+
fi

0 commit comments

Comments
 (0)