From eaf5bd26fbf4a22a46c537f4f90021382b455555 Mon Sep 17 00:00:00 2001 From: Damien LACHAUME / PALO-IT Date: Tue, 5 Mar 2024 11:56:25 +0100 Subject: [PATCH] tmp commit --- .../actions/publish-npm-package/action.yml | 94 +++++++++++++------ 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/.github/workflows/actions/publish-npm-package/action.yml b/.github/workflows/actions/publish-npm-package/action.yml index 08563fcee25..24c6c4a4d09 100644 --- a/.github/workflows/actions/publish-npm-package/action.yml +++ b/.github/workflows/actions/publish-npm-package/action.yml @@ -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: | @@ -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"