Skip to content

Commit 9b553f6

Browse files
committed
refactor: remove is-tag input, action now auto-detects context
The action now automatically detects whether it's publishing a stable release (from tag) or pre-release (from main) by checking $GITHUB_REF. Benefits: - No flimsy boolean flag to pass around - Action is self-contained and context-aware - Less error-prone (can't pass wrong flag) - Cleaner workflow files (no inputs needed) - Better logging (shows trigger source) The action inspects $GITHUB_REF: - refs/tags/* → stable release (latest tag) - refs/heads/main → pre-release (next tag)
1 parent 37ff83b commit 9b553f6

3 files changed

Lines changed: 13 additions & 19 deletions

File tree

.github/actions/npm-publish/action.yml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
name: 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

94
runs:
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
@@ -93,7 +91,7 @@ runs:
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"

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,3 @@ jobs:
131131

132132
- name: Publish to NPM
133133
uses: ./.github/actions/npm-publish
134-
with:
135-
is-tag: 'false'

.github/workflows/publish.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,3 @@ jobs:
5454

5555
- name: Publish to NPM
5656
uses: ./.github/actions/npm-publish
57-
with:
58-
is-tag: 'true'

0 commit comments

Comments
 (0)