Skip to content

Commit a7f79fa

Browse files
committed
ci(publish-npm): clarify that the package input is the package_dir
- rename input `package` to `package_dir`, this is mostly a semantic change since it was already used as such - extract the package name from the `package.json` file (this have the additionnal advantage of retrieving the scope since it's included in the name) - use `working-directory: ${{ inputs.package_dir }}` instead of doing manual `cd` where applicable - fix console prints that were talking about `crates.io` which is incorrect since everything is done against `npmjs.com` in this workflow
1 parent e72890c commit a7f79fa

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +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.
8+
package_dir:
9+
description: directory containing the package.json file.
1010
required: true
1111
scope:
1212
description: npm package scope (must not include '@' prefix).
@@ -26,40 +26,46 @@ inputs:
2626
runs:
2727
using: "composite"
2828
steps:
29+
- name: Prepare
30+
id: prepare
31+
shell: bash
32+
run: |
33+
echo "package_name=$(jq -r ".name" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT
34+
echo "package_local_version=$(jq -r ".version" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT
35+
2936
- name: Check npm latest version
3037
id: check_version
3138
shell: bash
3239
run: |
33-
echo "Check crate latest published version for '${{ inputs.package }}' package"
40+
echo "Check npmjs.com latest published version for '${{ steps.prepare.outputs.package_name }}' package"
3441
3542
if [ "${{ inputs.tag }}" != "latest" -a "${{ inputs.tag }}" != "next" ]; then
3643
echo "Tag '${{ inputs.tag }}' is not valid. It should be one of 'latest' or 'next'"
3744
exit 1
3845
fi
3946
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)
47+
NEXT_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.next 2> /dev/null || true)
48+
LATEST_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.latest 2> /dev/null || true)
4349
44-
echo "Latest crate.io version: '$LATEST_REMOTE_VERSION'"
45-
echo "Next crate.io version: '$NEXT_REMOTE_VERSION'"
46-
echo "Local version: '$LOCAL_VERSION'"
50+
echo "Latest npmjs.com version: '$LATEST_REMOTE_VERSION'"
51+
echo "Next npmjs.com version: '$NEXT_REMOTE_VERSION'"
52+
echo "Local version: '${{ steps.prepare.outputs.package_local_version }}'"
4753
4854
if [ "${{ inputs.tag }}" == "latest" ]; then
49-
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
55+
if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then
5056
echo "Local version and remote version are the same: no need to publish to npm registry"
5157
DEPLOY_MODE='none'
52-
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
58+
elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then
5359
DEPLOY_MODE='promote'
5460
else
5561
DEPLOY_MODE='publish'
5662
fi
5763
else # input.tag == 'next'
58-
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
64+
if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then
5965
# A latest already published: no need to tag with next
6066
echo "Local version and remote version are the same: no need to publish to npm registry"
6167
DEPLOY_MODE='none'
62-
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
68+
elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then
6369
echo "Local version and remote version are the same: no need to publish to npm registry"
6470
DEPLOY_MODE='none'
6571
else
@@ -70,43 +76,43 @@ runs:
7076
echo "Deploy mode: '$DEPLOY_MODE'"
7177
echo "Dry run: '${{ inputs.dry_run }}'"
7278
echo "deploy_mode=$DEPLOY_MODE" >> $GITHUB_OUTPUT
73-
echo "package_version=$LOCAL_VERSION" >> $GITHUB_OUTPUT
7479
7580
- name: Build package
7681
shell: bash
77-
working-directory: ${{ inputs.package }}
82+
working-directory: ${{ inputs.package_dir }}
7883
env:
7984
WASM_PACK_ARGS: --release --scope ${{ inputs.scope }}
8085
run: |
81-
echo "Build '@${{ inputs.scope }}/${{ inputs.package }}' package"
86+
echo "Build '${{ steps.prepare.outputs.package_name }}' package"
8287
make build
8388
8489
- name: Prepare publish
8590
shell: bash
8691
run: |
87-
cp ./LICENSE ${{ inputs.package }}
88-
cp -f ${{ inputs.package }}/npm/README.md ${{ inputs.package }}/
92+
cp ./LICENSE ${{ inputs.package_dir }}
93+
cp -f ${{ inputs.package_dir }}/npm/README.md ${{ inputs.package_dir }}/
8994
9095
- name: List package
9196
shell: bash
9297
run: |
93-
echo "List '@${{ inputs.scope }}/${{ inputs.package }}' package"
94-
ls -al -R ${{ inputs.package }}/dist
98+
echo "List '${{ steps.prepare.outputs.package_name }}' package"
99+
ls -al -R ${{ inputs.package_dir }}/dist
95100
96101
- name: Publish package new version
97102
if: steps.check_version.outputs.deploy_mode == 'publish'
98103
shell: bash
104+
working-directory: ${{ inputs.package_dir }}
99105
env:
100106
NPM_TOKEN: ${{ inputs.api_token }}
101107
run: |
102-
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
108+
echo "Publish '${{ steps.prepare.outputs.package_name }}' package"
103109
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
104110
if [ -z "${NPM_TOKEN}" -a "${{ inputs.dry_run }}" == "true" ]; then
105111
echo "Warning: An NPM access token is required for authentication and has not been provided."
106112
else
107113
npm whoami
108114
fi
109-
cd ${{ inputs.package }}/
115+
110116
if [ "${{ inputs.dry_run }}" == "false" ]; then
111117
dry_run_option=""
112118
else
@@ -117,12 +123,12 @@ runs:
117123
- name: Promote package distribution tag to 'latest'
118124
if: inputs.dry_run == 'false' && steps.check_version.outputs.deploy_mode == 'promote'
119125
shell: bash
126+
working-directory: ${{ inputs.package_dir }}
120127
env:
121128
NPM_TOKEN: ${{ inputs.api_token }}
122129
run: |
123-
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
130+
echo "Promote '${{ steps.prepare.outputs.package_name }}' package version '${{ steps.prepare.outputs.package_local_version }}' from 'next' to 'latest'"
124131
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
125132
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
133+
npm dist-tag add ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} latest
134+
npm dist-tag rm ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} next

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ jobs:
617617
uses: ./.github/workflows/actions/publish-npm-package
618618
with:
619619
dry_run: "true"
620-
package: ${{ matrix.package }}
620+
package_dir: ${{ matrix.package }}
621621
scope: ${{ matrix.scope }}
622622
access: ${{ matrix.access }}
623623
api_token: ${{ secrets[matrix.api_token_secret_name] }}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
uses: ./.github/workflows/actions/publish-npm-package
6666
with:
6767
dry_run: ${{ inputs.dry_run }}
68-
package: ${{ matrix.package }}
68+
package_dir: ${{ matrix.package }}
6969
scope: ${{ matrix.scope }}
7070
tag: ${{ matrix.tag }}
7171
access: ${{ matrix.access }}

.github/workflows/pre-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ jobs:
345345
uses: ./.github/workflows/actions/publish-npm-package
346346
with:
347347
dry_run: "false"
348-
package: ${{ matrix.package }}
348+
package_dir: ${{ matrix.package }}
349349
scope: ${{ matrix.scope }}
350350
tag: next
351351
access: ${{ matrix.access }}

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ jobs:
277277
uses: ./.github/workflows/actions/publish-npm-package
278278
with:
279279
dry_run: "false"
280-
package: ${{ matrix.package }}
280+
package_dir: ${{ matrix.package }}
281281
scope: ${{ matrix.scope }}
282282
tag: latest
283283
access: ${{ matrix.access }}

0 commit comments

Comments
 (0)