Skip to content

Commit 65ae4e0

Browse files
committed
refactor: separate tag detection into dedicated step
Moved trigger detection into its own step for better separation of concerns. Benefits: - Single Responsibility: Detection logic isolated from version generation - Clear outputs: is_tag, trigger_type, trigger_name - Easier to test and debug - Other steps can reference ${{ steps.detect.outputs.is_tag }} - Better GitHub Actions UI (shows detection as separate step) The detect step runs first and outputs: - is_tag: 'true' or 'false' - trigger_type: 'tag' or 'branch' - trigger_name: e.g. 'v0.1.1' or 'main'
1 parent 9b553f6 commit 65ae4e0

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,60 @@ description: Publish package to NPM with automatic version generation and idempo
44
runs:
55
using: composite
66
steps:
7+
- name: Detect trigger type
8+
id: detect
9+
shell: bash
10+
run: |
11+
if [[ $GITHUB_REF == refs/tags/* ]]; then
12+
echo "is_tag=true" >> $GITHUB_OUTPUT
13+
echo "trigger_type=tag" >> $GITHUB_OUTPUT
14+
echo "trigger_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
15+
echo "📦 Detected tag push: ${GITHUB_REF#refs/tags/}"
16+
else
17+
echo "is_tag=false" >> $GITHUB_OUTPUT
18+
echo "trigger_type=branch" >> $GITHUB_OUTPUT
19+
echo "trigger_name=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
20+
echo "🚧 Detected branch push: ${GITHUB_REF#refs/heads/}"
21+
fi
22+
723
- name: Setup Node.js with npm 11+ (for trusted publishing)
824
uses: actions/setup-node@v4
925
with:
1026
node-version: '20'
1127
registry-url: 'https://registry.npmjs.org'
1228

13-
- name: Generate unique version from git
29+
- name: Generate version
1430
id: version
1531
shell: bash
1632
run: |
1733
# Get base version from package.json
1834
BASE_VERSION=$(jq -r .version package.json)
1935
20-
# Auto-detect if this is a tag or branch push
21-
if [[ $GITHUB_REF == refs/tags/* ]]; then
22-
IS_TAG=true
36+
if [[ "${{ steps.detect.outputs.is_tag }}" == "true" ]]; then
2337
# For tags, use the base version as-is (stable release)
2438
NPM_VERSION="${BASE_VERSION}"
2539
NPM_TAG="latest"
2640
echo "📦 Publishing stable release: ${NPM_VERSION}"
27-
echo " Trigger: Tag ${GITHUB_REF#refs/tags/}"
2841
else
29-
IS_TAG=false
3042
# For main branch, create a pre-release version using git describe
3143
# Format: 0.3.0-next.5.g1a2b3c4 (base-next.commits.hash)
3244
GIT_COMMIT=$(git rev-parse --short HEAD)
3345
COMMITS_SINCE_TAG=$(git rev-list --count HEAD ^$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD) 2>/dev/null || echo "0")
3446
NPM_VERSION="${BASE_VERSION}-next.${COMMITS_SINCE_TAG}.g${GIT_COMMIT}"
3547
NPM_TAG="next"
3648
echo "🚧 Publishing pre-release: ${NPM_VERSION}"
37-
echo " Trigger: Branch ${GITHUB_REF#refs/heads/}"
3849
fi
3950
4051
echo "version=${NPM_VERSION}" >> $GITHUB_OUTPUT
4152
echo "tag=${NPM_TAG}" >> $GITHUB_OUTPUT
42-
echo "is_tag=${IS_TAG}" >> $GITHUB_OUTPUT
4353
4454
# Update package.json with the new version
4555
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');"
4656
4757
echo "Updated package.json to version ${NPM_VERSION}"
4858
4959
- name: Validate tag matches package.json version
50-
if: steps.version.outputs.is_tag == 'true'
60+
if: steps.detect.outputs.is_tag == 'true'
5161
shell: bash
5262
run: |
5363
# Extract version from package.json
@@ -91,7 +101,7 @@ runs:
91101
run: npm publish --tag ${{ steps.version.outputs.tag }} --provenance --access public
92102

93103
- name: Update dist-tag (version already exists)
94-
if: steps.check-exists.outputs.exists == 'true' && steps.version.outputs.is_tag == 'true'
104+
if: steps.check-exists.outputs.exists == 'true' && steps.detect.outputs.is_tag == 'true'
95105
shell: bash
96106
run: |
97107
PACKAGE_NAME=$(node -p "require('./package.json').name")
@@ -102,7 +112,7 @@ runs:
102112
npm dist-tag add "${PACKAGE_NAME}@${VERSION}" "${TAG}"
103113
104114
- name: Skip (pre-release already exists)
105-
if: steps.check-exists.outputs.exists == 'true' && steps.version.outputs.is_tag != 'true'
115+
if: steps.check-exists.outputs.exists == 'true' && steps.detect.outputs.is_tag != 'true'
106116
shell: bash
107117
run: |
108118
echo "⏭️ Pre-release version already exists, skipping"

0 commit comments

Comments
 (0)