Debian Codegraph @

.github/workflows/release-npm.yml

raw symbols SCIP
1 name: Publish npm packages
2
3 on:
4 release:
5 types: [published]
6 workflow_dispatch:
7 inputs:
8 package_version:
9 description: Version to write to package.json before publishing, for example 1.3.2-beta.0 or 1.3.2-next.1
10 required: false
11 type: string
12 dry_run:
13 description: Run npm publish with --dry-run
14 required: true
15 type: boolean
16 default: true
17 upload_npm_debug_logs:
18 description: Upload sanitized npm debug logs when a job fails; logs may still contain sensitive data
19 required: true
20 type: boolean
21 default: false
22
23 jobs:
24 publish-jieba:
25 name: Publish opencc-jieba
26 needs: publish-opencc
27 runs-on: macos-15
28 permissions:
29 contents: read
30 id-token: write
31
32 steps:
33 - uses: actions/checkout@v5
34 with:
35 ref: ${{ github.ref_name }}
36 fetch-depth: 0
37
38 - name: Use Node.js
39 uses: actions/setup-node@v5
40 with:
41 node-version: 24
42 registry-url: https://registry.npmjs.org/
43
44 - name: Install dependencies
45 run: npm ci --omit=optional
46
47 - name: Prepare package versions
48 shell: bash
49 env:
50 PACKAGE_VERSION: ${{ inputs.package_version }}
51 RELEASE_TAG: ${{ github.event.release.tag_name }}
52 run: |
53 node - "$PACKAGE_VERSION" "$RELEASE_TAG" <<'NODE'
54 const fs = require('fs');
55 const requestedVersion = (process.argv[2] || process.argv[3] || '')
56 .replace(/^ver\./, '')
57 .replace(/^v(?=\d)/, '');
58 const file = 'plugins/jieba/node/package.json';
59 const pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
60 const version = requestedVersion || pkg.version;
61 pkg.version = version;
62 if (pkg.peerDependencies && pkg.peerDependencies.opencc) {
63 pkg.peerDependencies.opencc = `>=${version}`;
64 }
65 fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`);
66 NODE
67
68 - name: Check published package version
69 id: publish_check
70 shell: bash
71 run: |
72 package="opencc-jieba"
73 version="$(node -p "require('./plugins/jieba/node/package.json').version")"
74 published="$(npm view "$package@$version" version 2>/dev/null || true)"
75 if [ "$published" = "$version" ]; then
76 echo "$package@$version is already published; skipping build and publish."
77 echo "published=true" >> "$GITHUB_OUTPUT"
78 else
79 echo "published=false" >> "$GITHUB_OUTPUT"
80 fi
81
82 - name: Build opencc-jieba data
83 if: steps.publish_check.outputs.published != 'true'
84 shell: bash
85 run: |
86 cd plugins/jieba/node
87 npm run build -- darwin-arm64
88
89 - name: Pack opencc-jieba
90 if: steps.publish_check.outputs.published != 'true'
91 shell: bash
92 run: |
93 cd plugins/jieba/node
94 mkdir -p dist/npm-artifacts
95 npm pack --pack-destination dist/npm-artifacts
96
97 - name: Upload package artifact
98 if: steps.publish_check.outputs.published != 'true'
99 uses: actions/upload-artifact@v7
100 with:
101 name: npm-opencc-jieba
102 path: plugins/jieba/node/dist/npm-artifacts/*.tgz
103 if-no-files-found: error
104
105 - name: Determine npm dist-tag
106 id: npm_metadata
107 shell: bash
108 run: |
109 version="$(node -p "require('./plugins/jieba/node/package.json').version")"
110 tag="$(node -e "const version = process.argv[1]; const match = version.match(/-([0-9A-Za-z-]+)(?:[.+]|$)/); console.log(match ? match[1] : 'latest');" "$version")"
111 echo "version=$version" >> "$GITHUB_OUTPUT"
112 echo "tag=$tag" >> "$GITHUB_OUTPUT"
113 echo "Publishing opencc-jieba@$version with npm dist-tag '$tag'"
114
115 - name: Publish opencc-jieba
116 if: steps.publish_check.outputs.published != 'true'
117 shell: bash
118 env:
119 DRY_RUN: ${{ inputs.dry_run }}
120 NPM_TAG: ${{ steps.npm_metadata.outputs.tag }}
121 OPENCC_NPM_PUBLISH_DRY_RUN: ${{ inputs.dry_run }}
122 run: |
123 package="opencc-jieba"
124 version="${{ steps.npm_metadata.outputs.version }}"
125 if [ "$(npm view "$package@$version" version 2>/dev/null || true)" = "$version" ]; then
126 echo "$package@$version is already published; skipping."
127 exit 0
128 fi
129 publish_args=(--provenance --loglevel verbose)
130 if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "$DRY_RUN" = "true" ]; then
131 publish_args+=(--dry-run)
132 fi
133 cd plugins/jieba/node
134 npm publish --tag "$NPM_TAG" "${publish_args[@]}"
135
136 - name: Collect npm debug logs
137 if: ${{ failure() && github.event_name == 'workflow_dispatch' && inputs.upload_npm_debug_logs }}
138 shell: bash
139 run: |
140 cache_dir="$(npm config get cache)"
141 mkdir -p npm-debug-logs
142 if [ -d "$cache_dir/_logs" ]; then
143 for log in "$cache_dir/_logs"/*-debug-*.log; do
144 [ -f "$log" ] || continue
145 sed -E \
146 -e 's#(//[^[:space:]]+:_authToken=).+#\1[REDACTED]#g' \
147 -e 's/(Authorization: Bearer )[A-Za-z0-9._~+\/=-]+/\1[REDACTED]/g' \
148 -e 's/(npm_[A-Za-z0-9]{20,})/[REDACTED]/g' \
149 "$log" > "npm-debug-logs/$(basename "$log")"
150 done
151 fi
152
153 - name: Upload npm debug logs
154 if: ${{ failure() && github.event_name == 'workflow_dispatch' && inputs.upload_npm_debug_logs }}
155 uses: actions/upload-artifact@v7
156 with:
157 name: npm-debug-logs-opencc-jieba
158 path: npm-debug-logs/*.log
159 if-no-files-found: warn
160
161 publish-opencc:
162 name: Publish opencc
163 runs-on: macos-15
164 permissions:
165 contents: read
166 id-token: write
167
168 steps:
169 - uses: actions/checkout@v5
170 with:
171 ref: ${{ github.ref_name }}
172 fetch-depth: 0
173
174 - name: Use Node.js
175 uses: actions/setup-node@v5
176 with:
177 node-version: 24
178 registry-url: https://registry.npmjs.org/
179
180 - uses: bazel-contrib/[email protected]
181 with:
182 bazelisk-cache: true
183 repository-cache: true
184
185 - name: Install dependencies
186 run: npm ci --omit=optional --ignore-scripts
187
188 - name: Prepare package versions
189 shell: bash
190 env:
191 PACKAGE_VERSION: ${{ inputs.package_version }}
192 RELEASE_TAG: ${{ github.event.release.tag_name }}
193 run: |
194 node - "$PACKAGE_VERSION" "$RELEASE_TAG" <<'NODE'
195 const fs = require('fs');
196 const requestedVersion = (process.argv[2] || process.argv[3] || '')
197 .replace(/^ver\./, '')
198 .replace(/^v(?=\d)/, '');
199 const file = 'package.json';
200 const pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
201 const version = requestedVersion || pkg.version;
202 pkg.version = version;
203 fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`);
204 NODE
205
206 - name: Check published package version
207 id: publish_check
208 shell: bash
209 run: |
210 package="opencc"
211 version="$(node -p "require('./package.json').version")"
212 published="$(npm view "$package@$version" version 2>/dev/null || true)"
213 if [ "$published" = "$version" ]; then
214 echo "$package@$version is already published; skipping build and publish."
215 echo "published=true" >> "$GITHUB_OUTPUT"
216 else
217 echo "published=false" >> "$GITHUB_OUTPUT"
218 fi
219
220 - name: Build opencc package assets
221 if: steps.publish_check.outputs.published != 'true'
222 run: ./scripts/build-node-prebuild-bazel.sh darwin-arm64
223
224 - name: Test
225 if: steps.publish_check.outputs.published != 'true'
226 env:
227 PREBUILDS_ONLY: 1
228 run: npm test
229
230 - name: Pack opencc
231 if: steps.publish_check.outputs.published != 'true'
232 run: |
233 mkdir -p dist/npm-artifacts
234 npm pack --pack-destination dist/npm-artifacts
235
236 - name: Upload package artifact
237 if: steps.publish_check.outputs.published != 'true'
238 uses: actions/upload-artifact@v7
239 with:
240 name: npm-opencc
241 path: dist/npm-artifacts/*.tgz
242 if-no-files-found: error
243
244 - name: Determine npm dist-tag
245 id: npm_metadata
246 shell: bash
247 run: |
248 version="$(node -p "require('./package.json').version")"
249 tag="$(node -e "const version = process.argv[1]; const match = version.match(/-([0-9A-Za-z-]+)(?:[.+]|$)/); console.log(match ? match[1] : 'latest');" "$version")"
250 echo "version=$version" >> "$GITHUB_OUTPUT"
251 echo "tag=$tag" >> "$GITHUB_OUTPUT"
252 echo "Publishing opencc@$version with npm dist-tag '$tag'"
253
254 - name: Publish opencc
255 if: steps.publish_check.outputs.published != 'true'
256 env:
257 DRY_RUN: ${{ inputs.dry_run }}
258 NPM_TAG: ${{ steps.npm_metadata.outputs.tag }}
259 OPENCC_NPM_PUBLISH_DRY_RUN: ${{ inputs.dry_run }}
260 run: |
261 package="opencc"
262 version="${{ steps.npm_metadata.outputs.version }}"
263 if [ "$(npm view "$package@$version" version 2>/dev/null || true)" = "$version" ]; then
264 echo "$package@$version is already published; skipping."
265 exit 0
266 fi
267 publish_args=(--provenance --loglevel verbose)
268 if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "$DRY_RUN" = "true" ]; then
269 publish_args+=(--dry-run)
270 fi
271 npm publish --tag "$NPM_TAG" "${publish_args[@]}"
272
273 - name: Collect npm debug logs
274 if: ${{ failure() && github.event_name == 'workflow_dispatch' && inputs.upload_npm_debug_logs }}
275 shell: bash
276 run: |
277 cache_dir="$(npm config get cache)"
278 mkdir -p npm-debug-logs
279 if [ -d "$cache_dir/_logs" ]; then
280 for log in "$cache_dir/_logs"/*-debug-*.log; do
281 [ -f "$log" ] || continue
282 sed -E \
283 -e 's#(//[^[:space:]]+:_authToken=).+#\1[REDACTED]#g' \
284 -e 's/(Authorization: Bearer )[A-Za-z0-9._~+\/=-]+/\1[REDACTED]/g' \
285 -e 's/(npm_[A-Za-z0-9]{20,})/[REDACTED]/g' \
286 "$log" > "npm-debug-logs/$(basename "$log")"
287 done
288 fi
289
290 - name: Upload npm debug logs
291 if: ${{ failure() && github.event_name == 'workflow_dispatch' && inputs.upload_npm_debug_logs }}
292 uses: actions/upload-artifact@v7
293 with:
294 name: npm-debug-logs-opencc
295 path: npm-debug-logs/*.log
296 if-no-files-found: warn