11name : Publish to NPM
2- description : Publish package to NPM with version generation and idempotent checks
3-
4- inputs :
5- is-tag :
6- description : ' Whether this is a tag push (true) or main branch push (false)'
7- required : true
2+ description : Publish package to NPM with automatic version generation and idempotent checks
83
94runs :
105 using : composite
@@ -22,34 +17,37 @@ runs:
2217 # Get base version from package.json
2318 BASE_VERSION=$(jq -r .version package.json)
2419
25- # Generate git describe version
26- GIT_DESCRIBE=$(git describe --tags --always --dirty 2>/dev/null || echo "unknown")
27-
28- if [[ "${{ inputs.is-tag }}" == "true" ]]; then
20+ # Auto-detect if this is a tag or branch push
21+ if [[ $GITHUB_REF == refs/tags/* ]]; then
22+ IS_TAG=true
2923 # For tags, use the base version as-is (stable release)
3024 NPM_VERSION="${BASE_VERSION}"
3125 NPM_TAG="latest"
32- echo "Publishing stable release: ${NPM_VERSION}"
26+ echo "📦 Publishing stable release: ${NPM_VERSION}"
27+ echo " Trigger: Tag ${GITHUB_REF#refs/tags/}"
3328 else
29+ IS_TAG=false
3430 # For main branch, create a pre-release version using git describe
3531 # Format: 0.3.0-next.5.g1a2b3c4 (base-next.commits.hash)
3632 GIT_COMMIT=$(git rev-parse --short HEAD)
3733 COMMITS_SINCE_TAG=$(git rev-list --count HEAD ^$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD) 2>/dev/null || echo "0")
3834 NPM_VERSION="${BASE_VERSION}-next.${COMMITS_SINCE_TAG}.g${GIT_COMMIT}"
3935 NPM_TAG="next"
40- echo "Publishing pre-release: ${NPM_VERSION}"
36+ echo "🚧 Publishing pre-release: ${NPM_VERSION}"
37+ echo " Trigger: Branch ${GITHUB_REF#refs/heads/}"
4138 fi
4239
4340 echo "version=${NPM_VERSION}" >> $GITHUB_OUTPUT
4441 echo "tag=${NPM_TAG}" >> $GITHUB_OUTPUT
42+ echo "is_tag=${IS_TAG}" >> $GITHUB_OUTPUT
4543
4644 # Update package.json with the new version
4745 node -e "const fs = require('fs'); const pkg = JSON.parse(fs.readFileSync('package.json')); pkg.version = '${NPM_VERSION}'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');"
4846
4947 echo "Updated package.json to version ${NPM_VERSION}"
5048
5149 - name : Validate tag matches package.json version
52- if : inputs.is-tag == 'true'
50+ if : steps.version.outputs.is_tag == 'true'
5351 shell : bash
5452 run : |
5553 # Extract version from package.json
9391 run : npm publish --tag ${{ steps.version.outputs.tag }} --provenance --access public
9492
9593 - name : Update dist-tag (version already exists)
96- if : steps.check-exists.outputs.exists == 'true' && inputs.is-tag == 'true'
94+ if : steps.check-exists.outputs.exists == 'true' && steps.version.outputs.is_tag == 'true'
9795 shell : bash
9896 run : |
9997 PACKAGE_NAME=$(node -p "require('./package.json').name")
@@ -104,7 +102,7 @@ runs:
104102 npm dist-tag add "${PACKAGE_NAME}@${VERSION}" "${TAG}"
105103
106104 - name : Skip (pre-release already exists)
107- if : steps.check-exists.outputs.exists == 'true' && inputs.is-tag != 'true'
105+ if : steps.check-exists.outputs.exists == 'true' && steps.version.outputs.is_tag != 'true'
108106 shell : bash
109107 run : |
110108 echo "⏭️ Pre-release version already exists, skipping"
0 commit comments