Skip to content

Commit 2ad7d5e

Browse files
authored
Merge pull request #2753 from input-output-hk/djo/2745/ci-enhance-npm-publish
ci: enhance npm publish workflow
2 parents e72890c + 110e896 commit 2ad7d5e

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

.github/workflows/actions/publish-npm-package/action.yml

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ inputs:
55
dry_run:
66
description: Dry run will not publish to npm, just test it.
77
required: true
8-
package:
9-
description: npm package name.
10-
required: true
11-
scope:
12-
description: npm package scope (must not include '@' prefix).
8+
package_dir:
9+
description: directory containing the package.json file.
1310
required: true
1411
tag:
1512
description: npm package tag.
@@ -26,40 +23,53 @@ inputs:
2623
runs:
2724
using: "composite"
2825
steps:
26+
- uses: actions/setup-node@v6
27+
with:
28+
node-version: "lts/*"
29+
package-manager-cache: "false"
30+
# Note: setting up the registry allows auth to be passed via env.NODE_AUTH_TOKEN
31+
registry-url: https://registry.npmjs.org
32+
33+
- name: Prepare
34+
id: prepare
35+
shell: bash
36+
run: |
37+
echo "package_name=$(jq -r ".name" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT
38+
echo "package_local_version=$(jq -r ".version" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT
39+
2940
- name: Check npm latest version
3041
id: check_version
3142
shell: bash
3243
run: |
33-
echo "Check crate latest published version for '${{ inputs.package }}' package"
44+
echo "Check npmjs.com latest published version for '${{ steps.prepare.outputs.package_name }}' package"
3445
3546
if [ "${{ inputs.tag }}" != "latest" -a "${{ inputs.tag }}" != "next" ]; then
3647
echo "Tag '${{ inputs.tag }}' is not valid. It should be one of 'latest' or 'next'"
3748
exit 1
3849
fi
3950
40-
LOCAL_VERSION=$(cat ${{ inputs.package }}/package.json | jq -r '.version')
41-
NEXT_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.next 2> /dev/null || true)
42-
LATEST_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.latest 2> /dev/null || true)
51+
NEXT_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.next 2> /dev/null || true)
52+
LATEST_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.latest 2> /dev/null || true)
4353
44-
echo "Latest crate.io version: '$LATEST_REMOTE_VERSION'"
45-
echo "Next crate.io version: '$NEXT_REMOTE_VERSION'"
46-
echo "Local version: '$LOCAL_VERSION'"
54+
echo "Latest npmjs.com version: '$LATEST_REMOTE_VERSION'"
55+
echo "Next npmjs.com version: '$NEXT_REMOTE_VERSION'"
56+
echo "Local version: '${{ steps.prepare.outputs.package_local_version }}'"
4757
4858
if [ "${{ inputs.tag }}" == "latest" ]; then
49-
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
59+
if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then
5060
echo "Local version and remote version are the same: no need to publish to npm registry"
5161
DEPLOY_MODE='none'
52-
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
62+
elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then
5363
DEPLOY_MODE='promote'
5464
else
5565
DEPLOY_MODE='publish'
5666
fi
5767
else # input.tag == 'next'
58-
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
68+
if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then
5969
# A latest already published: no need to tag with next
6070
echo "Local version and remote version are the same: no need to publish to npm registry"
6171
DEPLOY_MODE='none'
62-
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
72+
elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then
6373
echo "Local version and remote version are the same: no need to publish to npm registry"
6474
DEPLOY_MODE='none'
6575
else
@@ -70,43 +80,43 @@ runs:
7080
echo "Deploy mode: '$DEPLOY_MODE'"
7181
echo "Dry run: '${{ inputs.dry_run }}'"
7282
echo "deploy_mode=$DEPLOY_MODE" >> $GITHUB_OUTPUT
73-
echo "package_version=$LOCAL_VERSION" >> $GITHUB_OUTPUT
7483
7584
- name: Build package
7685
shell: bash
77-
working-directory: ${{ inputs.package }}
86+
working-directory: ${{ inputs.package_dir }}
7887
env:
79-
WASM_PACK_ARGS: --release --scope ${{ inputs.scope }}
88+
WASM_PACK_ARGS: --release
8089
run: |
81-
echo "Build '@${{ inputs.scope }}/${{ inputs.package }}' package"
90+
echo "::group::Build '${{ steps.prepare.outputs.package_name }}' package"
8291
make build
92+
echo "::endgroup::"
8393
8494
- name: Prepare publish
8595
shell: bash
8696
run: |
87-
cp ./LICENSE ${{ inputs.package }}
88-
cp -f ${{ inputs.package }}/npm/README.md ${{ inputs.package }}/
97+
cp ./LICENSE ${{ inputs.package_dir }}
98+
cp -f ${{ inputs.package_dir }}/npm/README.md ${{ inputs.package_dir }}/
8999
90100
- name: List package
91101
shell: bash
92102
run: |
93-
echo "List '@${{ inputs.scope }}/${{ inputs.package }}' package"
94-
ls -al -R ${{ inputs.package }}/dist
103+
echo "List '${{ steps.prepare.outputs.package_name }}' package"
104+
ls -al -R ${{ inputs.package_dir }}/dist
95105
96106
- name: Publish package new version
97107
if: steps.check_version.outputs.deploy_mode == 'publish'
98108
shell: bash
109+
working-directory: ${{ inputs.package_dir }}
99110
env:
100-
NPM_TOKEN: ${{ inputs.api_token }}
111+
NODE_AUTH_TOKEN: ${{ inputs.api_token }}
101112
run: |
102-
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
103-
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
104-
if [ -z "${NPM_TOKEN}" -a "${{ inputs.dry_run }}" == "true" ]; then
113+
echo "Publish '${{ steps.prepare.outputs.package_name }}' package"
114+
if [ -z "${NODE_AUTH_TOKEN}" -a "${{ inputs.dry_run }}" == "true" ]; then
105115
echo "Warning: An NPM access token is required for authentication and has not been provided."
106116
else
107117
npm whoami
108118
fi
109-
cd ${{ inputs.package }}/
119+
110120
if [ "${{ inputs.dry_run }}" == "false" ]; then
111121
dry_run_option=""
112122
else
@@ -115,14 +125,18 @@ runs:
115125
npm publish --tag ${{ inputs.tag }} --access ${{ inputs.access }} $dry_run_option
116126
117127
- name: Promote package distribution tag to 'latest'
118-
if: inputs.dry_run == 'false' && steps.check_version.outputs.deploy_mode == 'promote'
128+
if: steps.check_version.outputs.deploy_mode == 'promote'
119129
shell: bash
130+
working-directory: ${{ inputs.package_dir }}
120131
env:
121-
NPM_TOKEN: ${{ inputs.api_token }}
132+
NODE_AUTH_TOKEN: ${{ inputs.api_token }}
122133
run: |
123-
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
124-
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
125-
npm whoami
126-
cd ${{ inputs.package }}/
127-
npm dist-tag add @${{ inputs.scope }}/${{ inputs.package }}@${{ steps.check_version.outputs.package_version }} latest
128-
npm dist-tag rm @${{ inputs.scope }}/${{ inputs.package }}@${{ steps.check_version.outputs.package_version }} next
134+
echo "Promote '${{ steps.prepare.outputs.package_name }}' package version '${{ steps.prepare.outputs.package_local_version }}' from 'next' to 'latest'"
135+
if [ "${{ inputs.dry_run }}" == "false" ]; then
136+
npm whoami
137+
npm dist-tag add ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} latest
138+
npm dist-tag rm ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} next
139+
else
140+
echo "Dry run: npm dist-tag add ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} latest
141+
echo "Dry run: npm dist-tag rm ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} next
142+
fi

.github/workflows/ci.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ jobs:
136136
shell: bash
137137
working-directory: mithril-client-wasm
138138
env:
139-
WASM_PACK_ARGS: --release --scope mithril-dev
139+
WASM_PACK_ARGS: --release
140140
run: make build
141141

142142
- name: Prepare 'mithril-client-wasm' package
@@ -594,7 +594,6 @@ jobs:
594594
package: [mithril-client-wasm]
595595
include:
596596
- package: mithril-client-wasm
597-
scope: mithril-dev
598597
tag: latest
599598
access: public
600599
api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM
@@ -613,12 +612,11 @@ jobs:
613612
cargo-tools: wasm-pack
614613
github-token: ${{ secrets.GITHUB_TOKEN }}
615614

616-
- name: Publish package to npm
615+
- name: Publish package to npm (dry-run)
617616
uses: ./.github/workflows/actions/publish-npm-package
618617
with:
619618
dry_run: "true"
620-
package: ${{ matrix.package }}
621-
scope: ${{ matrix.scope }}
619+
package_dir: ${{ matrix.package }}
622620
access: ${{ matrix.access }}
623621
api_token: ${{ secrets[matrix.api_token_secret_name] }}
624622

.github/workflows/manual-publish-npm.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jobs:
3939
package: [mithril-client-wasm]
4040
include:
4141
- package: mithril-client-wasm
42-
scope: mithril-dev
4342
tag: latest
4443
access: public
4544
api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM
@@ -65,8 +64,7 @@ jobs:
6564
uses: ./.github/workflows/actions/publish-npm-package
6665
with:
6766
dry_run: ${{ inputs.dry_run }}
68-
package: ${{ matrix.package }}
69-
scope: ${{ matrix.scope }}
67+
package_dir: ${{ matrix.package }}
7068
tag: ${{ matrix.tag }}
7169
access: ${{ matrix.access }}
7270
api_token: ${{ secrets[matrix.api_token_secret_name] }}

.github/workflows/pre-release.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,14 @@ jobs:
316316
dry_run: "true"
317317
package: ${{ matrix.package }}
318318

319-
publish-wasm-test:
319+
publish-next-wasm-package:
320320
strategy:
321321
fail-fast: false
322322
max-parallel: 1
323323
matrix:
324324
package: [mithril-client-wasm]
325325
include:
326326
- package: mithril-client-wasm
327-
scope: mithril-dev
328327
tag: next
329328
access: public
330329
api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM
@@ -345,8 +344,7 @@ jobs:
345344
uses: ./.github/workflows/actions/publish-npm-package
346345
with:
347346
dry_run: "false"
348-
package: ${{ matrix.package }}
349-
scope: ${{ matrix.scope }}
347+
package_dir: ${{ matrix.package }}
350348
tag: next
351349
access: ${{ matrix.access }}
352350
api_token: ${{ secrets[matrix.api_token_secret_name] }}

.github/workflows/release.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,14 @@ jobs:
248248
package: ${{ matrix.package }}
249249
api_token: ${{ secrets[matrix.api_token_secret_name] }}
250250

251-
publish-wasm:
251+
promote-wasm-package-to-latest:
252252
strategy:
253253
fail-fast: false
254254
max-parallel: 1
255255
matrix:
256256
package: [mithril-client-wasm]
257257
include:
258258
- package: mithril-client-wasm
259-
scope: mithril-dev
260259
tag: latest
261260
access: public
262261
api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM
@@ -273,12 +272,11 @@ jobs:
273272
cargo-tools: wasm-pack
274273
github-token: ${{ secrets.GITHUB_TOKEN }}
275274

276-
- name: Publish package to npm
275+
- name: Promote npm package
277276
uses: ./.github/workflows/actions/publish-npm-package
278277
with:
279278
dry_run: "false"
280-
package: ${{ matrix.package }}
281-
scope: ${{ matrix.scope }}
279+
package_dir: ${{ matrix.package }}
282280
tag: latest
283281
access: ${{ matrix.access }}
284282
api_token: ${{ secrets[matrix.api_token_secret_name] }}

mithril-client-wasm/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
"types": "dist/node/mithril_client_wasm.d.ts",
2626
"scripts": {
2727
"build": "npm run build:node && npm run build:web && npm run build:bundler",
28-
"build:bundler": "wasm-pack build --target bundler --out-dir dist/bundler $WASM_PACK_ARGS",
28+
"build:bundler": "wasm-pack build --target bundler --out-dir dist/bundler --scope mithril-dev $WASM_PACK_ARGS",
2929
"postbuild:bundler": "rm dist/bundler/.gitignore",
30-
"build:node": "wasm-pack build --target nodejs --out-dir dist/node $WASM_PACK_ARGS",
30+
"build:node": "wasm-pack build --target nodejs --out-dir dist/node --scope mithril-dev $WASM_PACK_ARGS",
3131
"postbuild:node": "rm dist/node/.gitignore",
32-
"build:web": "wasm-pack build --target web --out-dir dist/web $WASM_PACK_ARGS",
32+
"build:web": "wasm-pack build --target web --out-dir dist/web --scope mithril-dev $WASM_PACK_ARGS",
3333
"postbuild:web": "rm dist/web/.gitignore"
3434
}
3535
}

0 commit comments

Comments
 (0)