Skip to content

Commit 607406c

Browse files
committed
chore: publish whenever there is a version change
1 parent 3a4ec64 commit 607406c

File tree

2 files changed

+105
-20
lines changed

2 files changed

+105
-20
lines changed

.github/workflows/npm-publish.yml

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
# Workflow for publishing the DBHub package to npm
2+
# This workflow has two trigger modes:
3+
#
4+
# 1. Manual trigger (workflow_dispatch):
5+
# - Allows manually specifying version and tag
6+
# - Useful for deliberate releases
7+
#
8+
# 2. Automatic trigger (on push to main branch that modifies package.json):
9+
# - Detects if the version has changed
10+
# - Automatically determines the appropriate npm tag based on version format
11+
# - Skips publishing if the version already exists on npm
12+
113
name: Publish to npm
214

315
on:
16+
# Manual trigger with customizable version and tag
417
workflow_dispatch:
518
inputs:
619
version:
@@ -11,59 +24,131 @@ on:
1124
description: "NPM tag (e.g., latest, dev)"
1225
required: false
1326
default: "dev"
27+
28+
# Automatic trigger when package.json changes in main branch
29+
push:
30+
branches:
31+
- main
32+
paths:
33+
- 'package.json'
1434

1535
jobs:
1636
build-and-publish:
1737
runs-on: ubuntu-latest
1838
steps:
39+
# Checkout the repository to get access to the code
1940
- name: Checkout repository
2041
uses: actions/checkout@v4
2142

43+
# Set up Node.js with npm registry configuration
2244
- name: Setup Node.js
2345
uses: actions/setup-node@v4
2446
with:
2547
node-version: "22"
26-
registry-url: "https://registry.npmjs.org/"
27-
scope: "@bytebase"
48+
registry-url: "https://registry.npmjs.org/" # Use the public npm registry
49+
scope: "@bytebase" # Set the npm scope for publishing
2850

51+
# Install pnpm for faster and more reliable package management
2952
- name: Install pnpm
3053
uses: pnpm/action-setup@v3
3154
with:
3255
version: latest
3356

57+
# Install project dependencies
3458
- name: Install dependencies
3559
run: pnpm install
3660

61+
# Build the project (compile TypeScript to JavaScript)
3762
- name: Build
3863
run: pnpm run build
3964

40-
- name: Update package.json for npm publishing
65+
# Determine if we need to publish and what version/tag to use
66+
- name: Check version and prepare for publishing
4167
run: |
42-
# Get the current version from package.json
68+
# Get current version from package.json
4369
CURRENT_VERSION=$(jq -r '.version' package.json)
44-
45-
# Use input version if provided, otherwise use current version
70+
71+
# CASE 1: Manual workflow trigger with specified version
4672
if [ -n "${{ inputs.version }}" ]; then
4773
VERSION="${{ inputs.version }}"
74+
TAG="${{ inputs.tag }}"
75+
SHOULD_PUBLISH="true"
76+
echo "Manual trigger: Using provided version ${VERSION} with tag ${TAG}"
77+
78+
# CASE 2: Automatic trigger from package.json changes
4879
else
4980
VERSION="${CURRENT_VERSION}"
81+
82+
# Check if this version already exists in npm registry to avoid duplicates
83+
if npm view @bytebase/dbhub@${VERSION} version &> /dev/null; then
84+
echo "Version ${VERSION} already exists in npm registry. Skipping publish."
85+
SHOULD_PUBLISH="false"
86+
else
87+
echo "Version ${VERSION} is new. Proceeding with publish."
88+
SHOULD_PUBLISH="true"
89+
90+
# Determine appropriate npm tag based on version format:
91+
# - For prerelease versions like "0.1.0-beta", use "beta" as the tag
92+
# - For stable versions like "1.0.0", use "latest" as the tag
93+
if [[ "${VERSION}" == *"-"* ]]; then
94+
# Extract tag from version string (e.g., "beta" from "0.1.0-beta")
95+
TAG=$(echo "${VERSION}" | cut -d'-' -f2 | cut -d'.' -f1)
96+
echo "Prerelease version detected. Using '${TAG}' npm tag."
97+
else
98+
TAG="latest"
99+
echo "Stable version detected. Using 'latest' npm tag."
100+
fi
101+
fi
102+
fi
103+
104+
# Store values as environment variables for use in later steps
105+
echo "PACKAGE_VERSION=${VERSION}" >> $GITHUB_ENV
106+
echo "NPM_TAG=${TAG}" >> $GITHUB_ENV
107+
echo "SHOULD_PUBLISH=${SHOULD_PUBLISH}" >> $GITHUB_ENV
108+
109+
# Summary message
110+
if [ "${SHOULD_PUBLISH}" = "true" ]; then
111+
echo "Publishing version: ${VERSION} with tag: ${TAG}"
50112
fi
51113
52-
echo "Publishing version: ${VERSION} with tag: ${{ inputs.tag }}"
53-
54-
# Update package.json to use @bytebase scope and set version
55-
jq --arg version "$VERSION" '.name = "@bytebase/dbhub" | .version = $version' package.json > package.json.tmp
56-
mv package.json.tmp package.json
57-
58-
# Set files to include in the package
59-
jq '.files = ["dist/**/*", "LICENSE", "README.md"]' package.json > package.json.tmp
60-
mv package.json.tmp package.json
114+
# Only modify package.json if we're going to publish
115+
if [ "${SHOULD_PUBLISH}" = "true" ]; then
116+
# Step 1: Update package name and version
117+
echo "Preparing package.json for publishing..."
118+
jq --arg version "$VERSION" '.name = "@bytebase/dbhub" | .version = $version' package.json > package.json.tmp
119+
mv package.json.tmp package.json
120+
121+
# Step 2: Configure which files to include in the published package
122+
echo "Setting files to include in the npm package..."
123+
jq '.files = ["dist/**/*", "LICENSE", "README.md"]' package.json > package.json.tmp
124+
mv package.json.tmp package.json
61125
62-
# Add bin entry for CLI usage
63-
jq '.bin = {"dbhub": "dist/index.js"}' package.json > package.json.tmp
64-
mv package.json.tmp package.json
126+
# Step 3: Add binary entry for CLI usage (makes it executable with 'npx' or after global install)
127+
echo "Adding bin entry for CLI usage..."
128+
jq '.bin = {"dbhub": "dist/index.js"}' package.json > package.json.tmp
129+
mv package.json.tmp package.json
130+
131+
echo "Package.json prepared successfully for publishing"
132+
else
133+
echo "Skipping package.json modifications as we won't be publishing"
134+
fi
65135
136+
# Publish the package to npm if conditions are met
66137
- name: Publish to npm
67-
run: pnpm publish --no-git-checks --access public --tag ${{ inputs.tag }}
138+
if: env.SHOULD_PUBLISH == 'true'
139+
run: |
140+
echo "Publishing @bytebase/dbhub@${{ env.PACKAGE_VERSION }} with tag ${{ env.NPM_TAG }}..."
141+
pnpm publish --no-git-checks --access public --tag ${{ env.NPM_TAG }}
142+
echo "✅ Successfully published to npm!"
68143
env:
144+
# Uses NPM_TOKEN from repository secrets for authentication
69145
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
146+
147+
# Display a message when skipping publication
148+
- name: Skip publishing
149+
if: env.SHOULD_PUBLISH != 'true'
150+
run: |
151+
echo "⏭️ Skipping publish step because:"
152+
echo " - Version has not changed, or"
153+
echo " - Version already exists in the npm registry"
154+
echo "To force publication, use the manual workflow trigger with a custom version."

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dbhub",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Universal Database MCP Server",
55
"main": "dist/index.js",
66
"type": "module",

0 commit comments

Comments
 (0)