|
| 1 | +name: Publish to NPM |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + version: |
| 10 | + description: 'Version to publish (patch, minor, major, or specific version like 1.2.3)' |
| 11 | + required: true |
| 12 | + default: 'patch' |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - patch |
| 16 | + - minor |
| 17 | + - major |
| 18 | + packages: |
| 19 | + description: 'Packages to publish (comma-separated: mcp-server,types or leave empty for all)' |
| 20 | + required: false |
| 21 | + type: string |
| 22 | + |
| 23 | +jobs: |
| 24 | + publish: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: write |
| 28 | + id-token: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout code |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + |
| 37 | + - name: Setup Node.js |
| 38 | + uses: actions/setup-node@v4 |
| 39 | + with: |
| 40 | + node-version: '18' |
| 41 | + registry-url: 'https://registry.npmjs.org' |
| 42 | + |
| 43 | + - name: Setup pnpm |
| 44 | + uses: pnpm/action-setup@v4 |
| 45 | + with: |
| 46 | + version: 9 |
| 47 | + run_install: false |
| 48 | + |
| 49 | + - name: Get pnpm store directory |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV |
| 53 | +
|
| 54 | + - name: Setup pnpm cache |
| 55 | + uses: actions/cache@v4 |
| 56 | + with: |
| 57 | + path: ${{ env.STORE_PATH }} |
| 58 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 59 | + restore-keys: | |
| 60 | + ${{ runner.os }}-pnpm-store- |
| 61 | +
|
| 62 | + - name: Install dependencies |
| 63 | + run: pnpm install --frozen-lockfile |
| 64 | + |
| 65 | + - name: Build packages |
| 66 | + run: pnpm build |
| 67 | + |
| 68 | + - name: Run tests |
| 69 | + run: pnpm test |
| 70 | + |
| 71 | + - name: Configure git |
| 72 | + run: | |
| 73 | + git config --local user.email "[email protected]" |
| 74 | + git config --local user.name "GitHub Action" |
| 75 | +
|
| 76 | + - name: Determine version bump |
| 77 | + id: version |
| 78 | + run: | |
| 79 | + if [ "${{ github.event_name }}" == "push" ] && [[ "${{ github.ref }}" == refs/tags/* ]]; then |
| 80 | + VERSION="${{ github.ref_name }}" |
| 81 | + VERSION="${VERSION#v}" # Remove 'v' prefix |
| 82 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 83 | + echo "bump=false" >> $GITHUB_OUTPUT |
| 84 | + else |
| 85 | + VERSION="${{ github.event.inputs.version }}" |
| 86 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 87 | + echo "bump=true" >> $GITHUB_OUTPUT |
| 88 | + fi |
| 89 | +
|
| 90 | + - name: Determine packages to publish |
| 91 | + id: packages |
| 92 | + run: | |
| 93 | + if [ -n "${{ github.event.inputs.packages }}" ]; then |
| 94 | + PACKAGES="${{ github.event.inputs.packages }}" |
| 95 | + else |
| 96 | + PACKAGES="mcp-server,types" |
| 97 | + fi |
| 98 | + echo "packages=$PACKAGES" >> $GITHUB_OUTPUT |
| 99 | +
|
| 100 | + - name: Version bump packages |
| 101 | + if: steps.version.outputs.bump == 'true' |
| 102 | + run: | |
| 103 | + IFS=',' read -ra PACKAGE_ARRAY <<< "${{ steps.packages.outputs.packages }}" |
| 104 | + for pkg in "${PACKAGE_ARRAY[@]}"; do |
| 105 | + pkg=$(echo "$pkg" | xargs) # trim whitespace |
| 106 | + if [ "$pkg" == "mcp-server" ]; then |
| 107 | + cd packages/mcp-server |
| 108 | + npm version ${{ steps.version.outputs.version }} --no-git-tag-version |
| 109 | + cd ../.. |
| 110 | + elif [ "$pkg" == "types" ]; then |
| 111 | + cd packages/types |
| 112 | + npm version ${{ steps.version.outputs.version }} --no-git-tag-version |
| 113 | + cd ../.. |
| 114 | + fi |
| 115 | + done |
| 116 | +
|
| 117 | + - name: Set specific version for packages |
| 118 | + if: steps.version.outputs.bump == 'false' |
| 119 | + run: | |
| 120 | + IFS=',' read -ra PACKAGE_ARRAY <<< "${{ steps.packages.outputs.packages }}" |
| 121 | + for pkg in "${PACKAGE_ARRAY[@]}"; do |
| 122 | + pkg=$(echo "$pkg" | xargs) # trim whitespace |
| 123 | + if [ "$pkg" == "mcp-server" ]; then |
| 124 | + cd packages/mcp-server |
| 125 | + npm version ${{ steps.version.outputs.version }} --no-git-tag-version --allow-same-version |
| 126 | + cd ../.. |
| 127 | + elif [ "$pkg" == "types" ]; then |
| 128 | + cd packages/types |
| 129 | + npm version ${{ steps.version.outputs.version }} --no-git-tag-version --allow-same-version |
| 130 | + cd ../.. |
| 131 | + fi |
| 132 | + done |
| 133 | +
|
| 134 | + - name: Rebuild after version update |
| 135 | + run: pnpm build |
| 136 | + |
| 137 | + - name: Publish packages |
| 138 | + env: |
| 139 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
| 140 | + run: | |
| 141 | + IFS=',' read -ra PACKAGE_ARRAY <<< "${{ steps.packages.outputs.packages }}" |
| 142 | + for pkg in "${PACKAGE_ARRAY[@]}"; do |
| 143 | + pkg=$(echo "$pkg" | xargs) # trim whitespace |
| 144 | + if [ "$pkg" == "mcp-server" ]; then |
| 145 | + echo "Publishing @devlog/mcp-server..." |
| 146 | + cd packages/mcp-server |
| 147 | + npm publish --access public |
| 148 | + cd ../.. |
| 149 | + elif [ "$pkg" == "types" ]; then |
| 150 | + echo "Publishing @devlog/types..." |
| 151 | + cd packages/types |
| 152 | + npm publish --access public |
| 153 | + cd ../.. |
| 154 | + fi |
| 155 | + done |
| 156 | +
|
| 157 | + - name: Commit version changes |
| 158 | + if: steps.version.outputs.bump == 'true' |
| 159 | + run: | |
| 160 | + git add . |
| 161 | + git commit -m "chore: bump version to ${{ steps.version.outputs.version }}" || echo "No changes to commit" |
| 162 | + git push origin main |
| 163 | +
|
| 164 | + - name: Create GitHub release |
| 165 | + if: steps.version.outputs.bump == 'true' |
| 166 | + uses: actions/create-release@v1 |
| 167 | + env: |
| 168 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 169 | + with: |
| 170 | + tag_name: v${{ steps.version.outputs.version }} |
| 171 | + release_name: Release v${{ steps.version.outputs.version }} |
| 172 | + body: | |
| 173 | + ## Changes |
| 174 | + |
| 175 | + Published packages: ${{ steps.packages.outputs.packages }} |
| 176 | + |
| 177 | + ### Packages |
| 178 | + - @devlog/mcp-server@${{ steps.version.outputs.version }} |
| 179 | + - @devlog/types@${{ steps.version.outputs.version }} |
| 180 | + draft: false |
| 181 | + prerelease: false |
0 commit comments