|
4 | 4 | push: |
5 | 5 | tags: |
6 | 6 | - 'v*' |
| 7 | + workflow_call: |
| 8 | + # Allow calling from other workflows (e.g., ci.yml) |
7 | 9 |
|
8 | 10 | permissions: |
9 | 11 | contents: read |
|
14 | 16 | name: publish to npm |
15 | 17 | runs-on: ubuntu-latest |
16 | 18 | steps: |
17 | | - - name: Checkout tag |
| 19 | + - name: Checkout code |
18 | 20 | uses: actions/checkout@v4 |
19 | 21 | with: |
20 | 22 | ref: ${{ github.ref }} |
@@ -52,5 +54,108 @@ jobs: |
52 | 54 | - name: Build library |
53 | 55 | run: bun run build |
54 | 56 |
|
55 | | - - name: Publish to NPM |
56 | | - uses: ./.github/actions/npm-publish |
| 57 | + - name: Setup Node.js for npm |
| 58 | + uses: actions/setup-node@v4 |
| 59 | + with: |
| 60 | + node-version: '20' |
| 61 | + registry-url: 'https://registry.npmjs.org' |
| 62 | + |
| 63 | + - name: Detect trigger type |
| 64 | + id: detect |
| 65 | + run: | |
| 66 | + if [[ $GITHUB_REF == refs/tags/* ]]; then |
| 67 | + echo "is_tag=true" >> $GITHUB_OUTPUT |
| 68 | + echo "trigger_type=tag" >> $GITHUB_OUTPUT |
| 69 | + echo "trigger_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT |
| 70 | + echo "📦 Detected tag push: ${GITHUB_REF#refs/tags/}" |
| 71 | + else |
| 72 | + echo "is_tag=false" >> $GITHUB_OUTPUT |
| 73 | + echo "trigger_type=branch" >> $GITHUB_OUTPUT |
| 74 | + echo "trigger_name=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT |
| 75 | + echo "🚧 Detected branch push: ${GITHUB_REF#refs/heads/}" |
| 76 | + fi |
| 77 | +
|
| 78 | + - name: Generate version |
| 79 | + id: version |
| 80 | + run: | |
| 81 | + # Get base version from package.json |
| 82 | + BASE_VERSION=$(jq -r .version package.json) |
| 83 | +
|
| 84 | + if [[ "${{ steps.detect.outputs.is_tag }}" == "true" ]]; then |
| 85 | + # For tags, use the base version as-is (stable release) |
| 86 | + NPM_VERSION="${BASE_VERSION}" |
| 87 | + NPM_TAG="latest" |
| 88 | + echo "📦 Publishing stable release: ${NPM_VERSION}" |
| 89 | + else |
| 90 | + # For main branch, create a pre-release version using git describe |
| 91 | + # Format: 0.3.0-next.5.g1a2b3c4 (base-next.commits.hash) |
| 92 | + GIT_COMMIT=$(git rev-parse --short HEAD) |
| 93 | + COMMITS_SINCE_TAG=$(git rev-list --count HEAD ^$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD) 2>/dev/null || echo "0") |
| 94 | + NPM_VERSION="${BASE_VERSION}-next.${COMMITS_SINCE_TAG}.g${GIT_COMMIT}" |
| 95 | + NPM_TAG="next" |
| 96 | + echo "🚧 Publishing pre-release: ${NPM_VERSION}" |
| 97 | + fi |
| 98 | +
|
| 99 | + echo "version=${NPM_VERSION}" >> $GITHUB_OUTPUT |
| 100 | + echo "tag=${NPM_TAG}" >> $GITHUB_OUTPUT |
| 101 | +
|
| 102 | + # Update package.json with the new version |
| 103 | + 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');" |
| 104 | +
|
| 105 | + echo "Updated package.json to version ${NPM_VERSION}" |
| 106 | +
|
| 107 | + - name: Validate tag matches package.json version |
| 108 | + if: steps.detect.outputs.is_tag == 'true' |
| 109 | + run: | |
| 110 | + # Extract version from package.json |
| 111 | + PKG_VERSION=$(jq -r .version package.json) |
| 112 | +
|
| 113 | + # Extract version from git tag (strip 'v' prefix) |
| 114 | + TAG_VERSION=${GITHUB_REF#refs/tags/v} |
| 115 | +
|
| 116 | + echo "Package version: $PKG_VERSION" |
| 117 | + echo "Tag version: $TAG_VERSION" |
| 118 | +
|
| 119 | + if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then |
| 120 | + echo "❌ Error: Version mismatch!" |
| 121 | + echo " package.json version: $PKG_VERSION" |
| 122 | + echo " Git tag version: $TAG_VERSION" |
| 123 | + echo "" |
| 124 | + echo "Please ensure the git tag matches the version in package.json" |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | +
|
| 128 | + echo "✅ Version validation passed: $PKG_VERSION" |
| 129 | +
|
| 130 | + - name: Check if version exists |
| 131 | + id: check-exists |
| 132 | + run: | |
| 133 | + PACKAGE_NAME=$(node -p "require('./package.json').name") |
| 134 | + VERSION="${{ steps.version.outputs.version }}" |
| 135 | +
|
| 136 | + if npm view "${PACKAGE_NAME}@${VERSION}" version &>/dev/null; then |
| 137 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 138 | + echo "Version ${VERSION} already exists on npm" |
| 139 | + else |
| 140 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 141 | + echo "Version ${VERSION} does not exist, will publish" |
| 142 | + fi |
| 143 | +
|
| 144 | + - name: Publish to npm (with OIDC trusted publishing) |
| 145 | + if: steps.check-exists.outputs.exists == 'false' |
| 146 | + run: npm publish --tag ${{ steps.version.outputs.tag }} --provenance --access public |
| 147 | + |
| 148 | + - name: Update dist-tag (version already exists) |
| 149 | + if: steps.check-exists.outputs.exists == 'true' && steps.detect.outputs.is_tag == 'true' |
| 150 | + run: | |
| 151 | + PACKAGE_NAME=$(node -p "require('./package.json').name") |
| 152 | + VERSION="${{ steps.version.outputs.version }}" |
| 153 | + TAG="${{ steps.version.outputs.tag }}" |
| 154 | +
|
| 155 | + echo "Version ${VERSION} already published, updating dist-tag to ${TAG}" |
| 156 | + npm dist-tag add "${PACKAGE_NAME}@${VERSION}" "${TAG}" |
| 157 | +
|
| 158 | + - name: Skip (pre-release already exists) |
| 159 | + if: steps.check-exists.outputs.exists == 'true' && steps.detect.outputs.is_tag != 'true' |
| 160 | + run: | |
| 161 | + echo "⏭️ Pre-release version already exists, skipping" |
0 commit comments