|
| 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 | + |
1 | 13 | name: Publish to npm |
2 | 14 |
|
3 | 15 | on: |
| 16 | + # Manual trigger with customizable version and tag |
4 | 17 | workflow_dispatch: |
5 | 18 | inputs: |
6 | 19 | version: |
|
11 | 24 | description: "NPM tag (e.g., latest, dev)" |
12 | 25 | required: false |
13 | 26 | default: "dev" |
| 27 | + |
| 28 | + # Automatic trigger when package.json changes in main branch |
| 29 | + push: |
| 30 | + branches: |
| 31 | + - main |
| 32 | + paths: |
| 33 | + - 'package.json' |
14 | 34 |
|
15 | 35 | jobs: |
16 | 36 | build-and-publish: |
17 | 37 | runs-on: ubuntu-latest |
18 | 38 | steps: |
| 39 | + # Checkout the repository to get access to the code |
19 | 40 | - name: Checkout repository |
20 | 41 | uses: actions/checkout@v4 |
21 | 42 |
|
| 43 | + # Set up Node.js with npm registry configuration |
22 | 44 | - name: Setup Node.js |
23 | 45 | uses: actions/setup-node@v4 |
24 | 46 | with: |
25 | 47 | 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 |
28 | 50 |
|
| 51 | + # Install pnpm for faster and more reliable package management |
29 | 52 | - name: Install pnpm |
30 | 53 | uses: pnpm/action-setup@v3 |
31 | 54 | with: |
32 | 55 | version: latest |
33 | 56 |
|
| 57 | + # Install project dependencies |
34 | 58 | - name: Install dependencies |
35 | 59 | run: pnpm install |
36 | 60 |
|
| 61 | + # Build the project (compile TypeScript to JavaScript) |
37 | 62 | - name: Build |
38 | 63 | run: pnpm run build |
39 | 64 |
|
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 |
41 | 67 | run: | |
42 | | - # Get the current version from package.json |
| 68 | + # Get current version from package.json |
43 | 69 | 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 |
46 | 72 | if [ -n "${{ inputs.version }}" ]; then |
47 | 73 | 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 |
48 | 79 | else |
49 | 80 | 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}" |
50 | 112 | fi |
51 | 113 |
|
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 |
61 | 125 |
|
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 |
65 | 135 |
|
| 136 | + # Publish the package to npm if conditions are met |
66 | 137 | - 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!" |
68 | 143 | env: |
| 144 | + # Uses NPM_TOKEN from repository secrets for authentication |
69 | 145 | 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." |
0 commit comments