| 1 | name: Publish npm binary packages |
| 2 | |
| 3 | on: |
| 4 | workflow_dispatch: |
| 5 | inputs: |
| 6 | package_version: |
| 7 | description: Version to write to package.json before publishing, for example 1.3.2-beta.0 or 1.3.2-next.1 |
| 8 | required: false |
| 9 | type: string |
| 10 | dry_run: |
| 11 | description: Run npm publish with --dry-run |
| 12 | required: true |
| 13 | type: boolean |
| 14 | default: true |
| 15 | upload_npm_debug_logs: |
| 16 | description: Upload sanitized npm debug logs when a job fails; logs may still contain sensitive data |
| 17 | required: true |
| 18 | type: boolean |
| 19 | default: false |
| 20 | |
| 21 | jobs: |
| 22 | publish-jieba-binaries: |
| 23 | name: Publish opencc-jieba binary (${{ matrix.target }}) |
| 24 | runs-on: ${{ matrix.runner }} |
| 25 | permissions: |
| 26 | contents: read |
| 27 | id-token: write |
| 28 | strategy: |
| 29 | fail-fast: false |
| 30 | matrix: |
| 31 | include: |
| 32 | - target: darwin-arm64 |
| 33 | runner: macos-15 |
| 34 | library: libopencc-jieba.dylib |
| 35 | - target: darwin-x64 |
| 36 | runner: macos-15-intel |
| 37 | library: libopencc-jieba.dylib |
| 38 | - target: linux-arm64 |
| 39 | runner: ubuntu-24.04-arm |
| 40 | library: libopencc-jieba.so |
| 41 | - target: linux-x64 |
| 42 | runner: ubuntu-24.04 |
| 43 | library: libopencc-jieba.so |
| 44 | - target: win32-x64 |
| 45 | runner: windows-2025 |
| 46 | library: opencc-jieba.dll |
| 47 | |
| 48 | steps: |
| 49 | - uses: actions/checkout@v5 |
| 50 | with: |
| 51 | fetch-depth: 0 |
| 52 | |
| 53 | - name: Use Node.js |
| 54 | uses: actions/setup-node@v5 |
| 55 | with: |
| 56 | node-version: 24 |
| 57 | registry-url: https://registry.npmjs.org/ |
| 58 | |
| 59 | - name: Install dependencies |
| 60 | run: npm install --omit=optional |
| 61 | |
| 62 | - name: Prepare package versions |
| 63 | shell: bash |
| 64 | env: |
| 65 | PACKAGE_VERSION: ${{ inputs.package_version }} |
| 66 | run: | |
| 67 | node - "$PACKAGE_VERSION" <<'NODE' |
| 68 | const fs = require('fs'); |
| 69 | const requestedVersion = (process.argv[2] || '') |
| 70 | .replace(/^ver\./, '') |
| 71 | .replace(/^v(?=\d)/, ''); |
| 72 | function updatePackage(file, optionalPrefix) { |
| 73 | const pkg = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 74 | const version = requestedVersion || pkg.version; |
| 75 | pkg.version = version; |
| 76 | if (pkg.optionalDependencies) { |
| 77 | for (const name of Object.keys(pkg.optionalDependencies)) { |
| 78 | if (name.startsWith(optionalPrefix)) { |
| 79 | pkg.optionalDependencies[name] = version; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`); |
| 84 | } |
| 85 | updatePackage('plugins/jieba/node/package.json', '@opencc/opencc-jieba-'); |
| 86 | NODE |
| 87 | |
| 88 | - name: Check published package version |
| 89 | id: publish_check |
| 90 | shell: bash |
| 91 | run: | |
| 92 | package="@opencc/opencc-jieba-${{ matrix.target }}" |
| 93 | version="$(node -p "require('./plugins/jieba/node/package.json').version")" |
| 94 | published="$(npm view "$package@$version" version 2>/dev/null || true)" |
| 95 | if [ "$published" = "$version" ]; then |
| 96 | echo "$package@$version is already published; skipping build and publish." |
| 97 | echo "published=true" >> "$GITHUB_OUTPUT" |
| 98 | else |
| 99 | echo "published=false" >> "$GITHUB_OUTPUT" |
| 100 | fi |
| 101 | |
| 102 | - name: Build opencc-jieba binary |
| 103 | if: steps.publish_check.outputs.published != 'true' |
| 104 | shell: bash |
| 105 | run: | |
| 106 | cd plugins/jieba/node |
| 107 | npm run build -- "${{ matrix.target }}" |
| 108 | test -f "prebuilds/${{ matrix.target }}/${{ matrix.library }}" |
| 109 | |
| 110 | - name: Prepare scoped package |
| 111 | if: steps.publish_check.outputs.published != 'true' |
| 112 | shell: bash |
| 113 | run: | |
| 114 | cd plugins/jieba/node |
| 115 | npm run prepare:scoped-packages |
| 116 | npm pack --dry-run "dist/scoped-packages/@opencc/opencc-jieba-${{ matrix.target }}" |
| 117 | |
| 118 | - name: Pack scoped package |
| 119 | if: steps.publish_check.outputs.published != 'true' |
| 120 | shell: bash |
| 121 | run: | |
| 122 | mkdir -p plugins/jieba/node/dist/npm-artifacts |
| 123 | npm pack "plugins/jieba/node/dist/scoped-packages/@opencc/opencc-jieba-${{ matrix.target }}" \ |
| 124 | --pack-destination plugins/jieba/node/dist/npm-artifacts |
| 125 | |
| 126 | - name: Upload package artifact |
| 127 | if: steps.publish_check.outputs.published != 'true' |
| 128 | uses: actions/upload-artifact@v7 |
| 129 | with: |
| 130 | name: npm-opencc-jieba-binary-${{ matrix.target }} |
| 131 | path: plugins/jieba/node/dist/npm-artifacts/*.tgz |
| 132 | if-no-files-found: error |
| 133 | |
| 134 | - name: Determine npm dist-tag |
| 135 | id: npm_metadata |
| 136 | shell: bash |
| 137 | run: | |
| 138 | version="$(node -p "require('./plugins/jieba/node/package.json').version")" |
| 139 | tag="$(node -e "const version = process.argv[1]; const match = version.match(/-([0-9A-Za-z-]+)(?:[.+]|$)/); console.log(match ? match[1] : 'latest');" "$version")" |
| 140 | echo "version=$version" >> "$GITHUB_OUTPUT" |
| 141 | echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 142 | echo "Publishing @opencc/opencc-jieba-${{ matrix.target }}@$version with npm dist-tag '$tag'" |
| 143 | |
| 144 | - name: Publish scoped package |
| 145 | if: steps.publish_check.outputs.published != 'true' |
| 146 | shell: bash |
| 147 | env: |
| 148 | DRY_RUN: ${{ inputs.dry_run }} |
| 149 | NPM_TAG: ${{ steps.npm_metadata.outputs.tag }} |
| 150 | run: | |
| 151 | package="@opencc/opencc-jieba-${{ matrix.target }}" |
| 152 | version="${{ steps.npm_metadata.outputs.version }}" |
| 153 | if [ "$(npm view "$package@$version" version 2>/dev/null || true)" = "$version" ]; then |
| 154 | echo "$package@$version is already published; skipping." |
| 155 | exit 0 |
| 156 | fi |
| 157 | publish_args=(--access public --provenance --loglevel verbose) |
| 158 | if [ "$DRY_RUN" = "true" ]; then |
| 159 | publish_args+=(--dry-run) |
| 160 | fi |
| 161 | npm publish "plugins/jieba/node/dist/scoped-packages/@opencc/opencc-jieba-${{ matrix.target }}" --tag "$NPM_TAG" "${publish_args[@]}" |
| 162 | |
| 163 | - name: Collect npm debug logs |
| 164 | if: ${{ failure() && inputs.upload_npm_debug_logs }} |
| 165 | shell: bash |
| 166 | run: | |
| 167 | cache_dir="$(npm config get cache)" |
| 168 | mkdir -p npm-debug-logs |
| 169 | if [ -d "$cache_dir/_logs" ]; then |
| 170 | for log in "$cache_dir/_logs"/*-debug-*.log; do |
| 171 | [ -f "$log" ] || continue |
| 172 | sed -E \ |
| 173 | -e 's#(//[^[:space:]]+:_authToken=).+#\1[REDACTED]#g' \ |
| 174 | -e 's/(Authorization: Bearer )[A-Za-z0-9._~+\/=-]+/\1[REDACTED]/g' \ |
| 175 | -e 's/(npm_[A-Za-z0-9]{20,})/[REDACTED]/g' \ |
| 176 | "$log" > "npm-debug-logs/$(basename "$log")" |
| 177 | done |
| 178 | fi |
| 179 | |
| 180 | - name: Upload npm debug logs |
| 181 | if: ${{ failure() && inputs.upload_npm_debug_logs }} |
| 182 | uses: actions/upload-artifact@v7 |
| 183 | with: |
| 184 | name: npm-debug-logs-opencc-jieba-binary-${{ matrix.target }} |
| 185 | path: npm-debug-logs/*.log |
| 186 | if-no-files-found: warn |
| 187 | |
| 188 | publish-opencc-binaries: |
| 189 | name: Publish opencc binary (${{ matrix.target }}) |
| 190 | runs-on: ${{ matrix.runner }} |
| 191 | permissions: |
| 192 | contents: read |
| 193 | id-token: write |
| 194 | strategy: |
| 195 | fail-fast: false |
| 196 | matrix: |
| 197 | include: |
| 198 | - target: darwin-arm64 |
| 199 | runner: macos-15 |
| 200 | - target: darwin-x64 |
| 201 | runner: macos-15-intel |
| 202 | - target: linux-arm64 |
| 203 | runner: ubuntu-24.04-arm |
| 204 | - target: linux-x64 |
| 205 | runner: ubuntu-24.04 |
| 206 | - target: win32-x64 |
| 207 | runner: windows-2025 |
| 208 | |
| 209 | steps: |
| 210 | - uses: actions/checkout@v5 |
| 211 | with: |
| 212 | fetch-depth: 0 |
| 213 | |
| 214 | - name: Use Node.js |
| 215 | uses: actions/setup-node@v5 |
| 216 | with: |
| 217 | node-version: 24 |
| 218 | registry-url: https://registry.npmjs.org/ |
| 219 | |
| 220 | - uses: bazel-contrib/[email protected] |
| 221 | with: |
| 222 | bazelisk-cache: true |
| 223 | repository-cache: true |
| 224 | |
| 225 | - name: Install dependencies |
| 226 | run: npm install --omit=optional --ignore-scripts |
| 227 | |
| 228 | - name: Prepare package versions |
| 229 | shell: bash |
| 230 | env: |
| 231 | PACKAGE_VERSION: ${{ inputs.package_version }} |
| 232 | run: | |
| 233 | node - "$PACKAGE_VERSION" <<'NODE' |
| 234 | const fs = require('fs'); |
| 235 | const requestedVersion = (process.argv[2] || '') |
| 236 | .replace(/^ver\./, '') |
| 237 | .replace(/^v(?=\d)/, ''); |
| 238 | const file = 'package.json'; |
| 239 | const pkg = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 240 | const version = requestedVersion || pkg.version; |
| 241 | pkg.version = version; |
| 242 | for (const name of Object.keys(pkg.optionalDependencies || {})) { |
| 243 | if (name.startsWith('@opencc/opencc-')) { |
| 244 | pkg.optionalDependencies[name] = version; |
| 245 | } |
| 246 | } |
| 247 | fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`); |
| 248 | NODE |
| 249 | |
| 250 | - name: Check published package version |
| 251 | id: publish_check |
| 252 | shell: bash |
| 253 | run: | |
| 254 | package="@opencc/opencc-${{ matrix.target }}" |
| 255 | version="$(node -p "require('./package.json').version")" |
| 256 | published="$(npm view "$package@$version" version 2>/dev/null || true)" |
| 257 | if [ "$published" = "$version" ]; then |
| 258 | echo "$package@$version is already published; skipping build and publish." |
| 259 | echo "published=true" >> "$GITHUB_OUTPUT" |
| 260 | else |
| 261 | echo "published=false" >> "$GITHUB_OUTPUT" |
| 262 | fi |
| 263 | |
| 264 | - name: Build opencc binary |
| 265 | if: steps.publish_check.outputs.published != 'true' |
| 266 | shell: bash |
| 267 | run: | |
| 268 | ./scripts/build-node-prebuild-bazel.sh "${{ matrix.target }}" |
| 269 | test -f "prebuilds/${{ matrix.target }}/opencc.node" |
| 270 | |
| 271 | - name: Prepare scoped package |
| 272 | if: steps.publish_check.outputs.published != 'true' |
| 273 | shell: bash |
| 274 | run: | |
| 275 | npm run prepare:scoped-packages |
| 276 | npm pack --dry-run "dist/scoped-packages/@opencc/opencc-${{ matrix.target }}" |
| 277 | |
| 278 | - name: Pack scoped package |
| 279 | if: steps.publish_check.outputs.published != 'true' |
| 280 | shell: bash |
| 281 | run: | |
| 282 | mkdir -p dist/npm-artifacts |
| 283 | npm pack "dist/scoped-packages/@opencc/opencc-${{ matrix.target }}" \ |
| 284 | --pack-destination dist/npm-artifacts |
| 285 | |
| 286 | - name: Upload package artifact |
| 287 | if: steps.publish_check.outputs.published != 'true' |
| 288 | uses: actions/upload-artifact@v7 |
| 289 | with: |
| 290 | name: npm-opencc-binary-${{ matrix.target }} |
| 291 | path: dist/npm-artifacts/*.tgz |
| 292 | if-no-files-found: error |
| 293 | |
| 294 | - name: Determine npm dist-tag |
| 295 | id: npm_metadata |
| 296 | shell: bash |
| 297 | run: | |
| 298 | version="$(node -p "require('./package.json').version")" |
| 299 | tag="$(node -e "const version = process.argv[1]; const match = version.match(/-([0-9A-Za-z-]+)(?:[.+]|$)/); console.log(match ? match[1] : 'latest');" "$version")" |
| 300 | echo "version=$version" >> "$GITHUB_OUTPUT" |
| 301 | echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 302 | echo "Publishing @opencc/opencc-${{ matrix.target }}@$version with npm dist-tag '$tag'" |
| 303 | |
| 304 | - name: Publish scoped package |
| 305 | if: steps.publish_check.outputs.published != 'true' |
| 306 | shell: bash |
| 307 | env: |
| 308 | DRY_RUN: ${{ inputs.dry_run }} |
| 309 | NPM_TAG: ${{ steps.npm_metadata.outputs.tag }} |
| 310 | run: | |
| 311 | package="@opencc/opencc-${{ matrix.target }}" |
| 312 | version="${{ steps.npm_metadata.outputs.version }}" |
| 313 | if [ "$(npm view "$package@$version" version 2>/dev/null || true)" = "$version" ]; then |
| 314 | echo "$package@$version is already published; skipping." |
| 315 | exit 0 |
| 316 | fi |
| 317 | publish_args=(--access public --provenance --loglevel verbose) |
| 318 | if [ "$DRY_RUN" = "true" ]; then |
| 319 | publish_args+=(--dry-run) |
| 320 | fi |
| 321 | npm publish "dist/scoped-packages/@opencc/opencc-${{ matrix.target }}" --tag "$NPM_TAG" "${publish_args[@]}" |
| 322 | |
| 323 | - name: Collect npm debug logs |
| 324 | if: ${{ failure() && inputs.upload_npm_debug_logs }} |
| 325 | shell: bash |
| 326 | run: | |
| 327 | cache_dir="$(npm config get cache)" |
| 328 | mkdir -p npm-debug-logs |
| 329 | if [ -d "$cache_dir/_logs" ]; then |
| 330 | for log in "$cache_dir/_logs"/*-debug-*.log; do |
| 331 | [ -f "$log" ] || continue |
| 332 | sed -E \ |
| 333 | -e 's#(//[^[:space:]]+:_authToken=).+#\1[REDACTED]#g' \ |
| 334 | -e 's/(Authorization: Bearer )[A-Za-z0-9._~+\/=-]+/\1[REDACTED]/g' \ |
| 335 | -e 's/(npm_[A-Za-z0-9]{20,})/[REDACTED]/g' \ |
| 336 | "$log" > "npm-debug-logs/$(basename "$log")" |
| 337 | done |
| 338 | fi |
| 339 | |
| 340 | - name: Upload npm debug logs |
| 341 | if: ${{ failure() && inputs.upload_npm_debug_logs }} |
| 342 | uses: actions/upload-artifact@v7 |
| 343 | with: |
| 344 | name: npm-debug-logs-opencc-binary-${{ matrix.target }} |
| 345 | path: npm-debug-logs/*.log |
| 346 | if-no-files-found: warn |
| 347 | |
| 348 | update-lock-files: |
| 349 | name: Update lock files and open PR |
| 350 | needs: [publish-jieba-binaries, publish-opencc-binaries] |
| 351 | if: ${{ !inputs.dry_run }} |
| 352 | runs-on: ubuntu-24.04 |
| 353 | permissions: |
| 354 | contents: write |
| 355 | pull-requests: write |
| 356 | outputs: |
| 357 | pr_url: ${{ steps.pr.outputs.url }} |
| 358 | |
| 359 | steps: |
| 360 | - uses: actions/checkout@v5 |
| 361 | with: |
| 362 | ref: ${{ github.ref_name }} |
| 363 | token: ${{ secrets.GITHUB_TOKEN }} |
| 364 | |
| 365 | - name: Use Node.js |
| 366 | uses: actions/setup-node@v5 |
| 367 | with: |
| 368 | node-version: 24 |
| 369 | registry-url: https://registry.npmjs.org/ |
| 370 | |
| 371 | - name: Apply package version |
| 372 | shell: bash |
| 373 | env: |
| 374 | PACKAGE_VERSION: ${{ inputs.package_version }} |
| 375 | run: | |
| 376 | node - "$PACKAGE_VERSION" <<'NODE' |
| 377 | const fs = require('fs'); |
| 378 | const requestedVersion = (process.argv[2] || '') |
| 379 | .replace(/^ver\./, '') |
| 380 | .replace(/^v(?=\d)/, ''); |
| 381 | function updatePackage(file, optionalPrefix) { |
| 382 | const pkg = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 383 | const version = requestedVersion || pkg.version; |
| 384 | pkg.version = version; |
| 385 | if (pkg.optionalDependencies) { |
| 386 | for (const name of Object.keys(pkg.optionalDependencies)) { |
| 387 | if (name.startsWith(optionalPrefix)) { |
| 388 | pkg.optionalDependencies[name] = version; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`); |
| 393 | } |
| 394 | updatePackage('package.json', '@opencc/opencc-'); |
| 395 | updatePackage('plugins/jieba/node/package.json', '@opencc/opencc-jieba-'); |
| 396 | NODE |
| 397 | |
| 398 | - name: Regenerate lock files |
| 399 | run: | |
| 400 | npm install --package-lock-only --ignore-scripts |
| 401 | |
| 402 | - name: Open PR if lock files changed |
| 403 | id: pr |
| 404 | env: |
| 405 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 406 | run: | |
| 407 | git add package.json package-lock.json plugins/jieba/node/package.json |
| 408 | if git diff --staged --quiet; then |
| 409 | echo "Package metadata and lock file already up to date; skipping PR." |
| 410 | exit 0 |
| 411 | fi |
| 412 | version="$(node -p "require('./package.json').version")" |
| 413 | branch="chore/lock-files-$version-${{ github.run_id }}" |
| 414 | git config user.name "github-actions[bot]" |
| 415 | git config user.email "github-actions[bot]@users.noreply.github.com" |
| 416 | git checkout -b "$branch" |
| 417 | git commit -m "Update lock files for $version binary package hashes" |
| 418 | git push origin "$branch" |
| 419 | url=$(gh pr create \ |
| 420 | --title "Update package metadata and lock file for $version binary package hashes" \ |
| 421 | --body "Updates package metadata and \`package-lock.json\` with integrity hashes for the newly published binary packages. |
| 422 | |
| 423 | **Next steps**: merge this PR, then create the release tag pointing to the merge commit and publish the GitHub Release. The \`release: published\` event will trigger the main package publishing workflow." \ |
| 424 | --base "${{ github.ref_name }}" \ |
| 425 | --head "$branch") |
| 426 | echo "url=$url" >> "$GITHUB_OUTPUT" |
| 427 | echo "PR opened: $url" |