Skip to content

Commit

Permalink
tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dlachaume committed Mar 5, 2024
1 parent 4dbb219 commit eaf5bd2
Showing 1 changed file with 65 additions and 29 deletions.
94 changes: 65 additions & 29 deletions .github/workflows/actions/publish-npm-package/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,70 @@ inputs:
runs:
using: "composite"
steps:
# 0 - check input.tag: fail if not latest or next
# 1 - if dry-run: echo "dry_run=' --dry-run'" >> $GITHUB_OUTPUT
# 2 - add: echo "local_version='$LOCAL_VERSION'" >> $GITHUB_OUTPUT
# 3 - in the "should_deploy" if:
# - if not deployed + input.tag is 'next': publish
# - if not deployed + input.tag is 'latest' + next doesnt exist: publish
# - if not deployed + input.tag is 'latest' + next exist: promote
# - if already deployed: nothing to do
#
# Note: dry-run can't be executed for promote (because npm dist-tag doesn't support it)

# input.tag | next | latest
# Publish on Npm |
# Nothing | deploy next | deploy latest
# version / next | --- | dist-tag add latest / dist-tag rm next
# version / latest | ??? | ---
# version / other | Crash or nothing

- name: Check npm latest version
id: check_version
shell: bash
run: |
echo "Check crate latest published version for '${{ inputs.package}}' package"
LATEST_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.${{ inputs.tag }} 2> /dev/null || true)
LOCAL_VERSION=$(cargo metadata --quiet --no-deps | jq -r '.packages[] | select(.name=="${{ inputs.package}}") | .version')
NEXT_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.next 2> /dev/null || true)
LATEST_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.latest 2> /dev/null || true)
# TODO: Should we check that the version is upper to the remote one
echo "Latest crate.io version: $LATEST_REMOTE_VERSION"
echo "Next crate.io version: $NEXT_REMOTE_VERSION"
echo "Local version: $LOCAL_VERSION"
if [ "$LOCAL_VERSION" != "$LATEST_REMOTE_VERSION" ]; then
echo "Local version is newer than remote version: we will publish to npm registry"
echo "should_deploy=true" >> $GITHUB_OUTPUT
else
echo "Local version and remote version are the same: no need to publish to npm registry"
echo "should_deploy=false" >> $GITHUB_OUTPUT
if [ ${{ inputs.tag }} == 'latest'; then
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
echo "Local version and remote version are the same: no need to publish to npm registry"
DEPLOY_MODE='none'
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
DEPLOY_MODE='promote'
else
DEPLOY_MODE='publish'
fi
else # input.tag == 'next'
if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then
# A latest already published: no need to tag with next
echo "Local version and remote version are the same: no need to publish to npm registry"
DEPLOY_MODE='none'
elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then
echo "Local version and remote version are the same: no need to publish to npm registry"
DEPLOY_MODE='none'
else
DEPLOY_MODE='publish'
fi
fi
echo "deploy_mode=$DEPLOY_MODE" >> $GITHUB_OUTPUT
- name: Build package
shell: bash
run: |
Expand All @@ -58,40 +104,30 @@ runs:
echo "List '@${{ inputs.scope }}/${{ inputs.package }}' package"
ls -al ${{ inputs.package }}/pkg
- name: Test publish package
if: inputs.dry_run == 'true' && steps.check_version.outputs.should_deploy == 'true'
shell: bash
env:
NPM_TOKEN: ${{ inputs.api_token }}
run: |
echo "test publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
npm whoami
cd ${{ inputs.package }}/pkg
npm publish --tag ${{ inputs.tag }} --access ${{ inputs.access }} --dry-run
- name: Publish package new version
if: inputs.dry_run == 'false' && steps.check_version.outputs.should_deploy == 'true' && inputs.tag != 'latest'
if: steps.check_version.outputs.deploy_mode == 'publish'
shell: bash
env:
NPM_TOKEN: ${{ inputs.api_token }}
run: |
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
npm whoami
# npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
# npm whoami
cd ${{ inputs.package }}/pkg
npm publish --tag ${{ inputs.tag }} --access ${{ inputs.access }}
if [ ${{ inputs.dry_run }} == 'true' ]; then
dry_run_option = '--dry-run'
fi
echo "npm publish --tag ${{ inputs.tag }} --access ${{ inputs.access }} {{ dry_run_option }}"
- name: Promote package distribution tag to 'latest'
if: inputs.dry_run == 'false' && steps.check_version.outputs.should_deploy == 'true' && inputs.tag == 'latest'
if: inputs.dry_run == 'false' && steps.check_version.outputs.deploy_mode == 'promote'
shell: bash
env:
NPM_TOKEN: ${{ inputs.api_token }}
run: |
echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package"
npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
npm whoami
# npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
# npm whoami
cd ${{ inputs.package }}/pkg
PACKAGE_VERSION=$(npm pkg get version | tr -d '"')
npm dist-tag add @${{ inputs.scope }}/${{ inputs.package }}@$PACKAGE_VERSION latest
npm dist-tag rm @${{ inputs.scope }}/${{ inputs.package }}@$PACKAGE_VERSION next
echo "npm dist-tag add @${{ inputs.scope }}/${{ inputs.package }}@{{ steps.check_version.outputs.package_version }} latest"
echo "npm dist-tag rm @${{ inputs.scope }}/${{ inputs.package }}@{{ steps.check_version.outputs.package_version }} next"

0 comments on commit eaf5bd2

Please sign in to comment.