Skip to content

Commit

Permalink
Enhance workflow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnie57 committed Oct 22, 2024
1 parent 73ace66 commit bffc63c
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions .github/workflows/publish-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
needs: [Timestamp]
runs-on: ubuntu-latest
outputs:
branch_name: ${{ steps.extract_branch_name.outputs.branch_name }}
branch_name: ${{ steps.extract_branch_name.outputs.BRANCH_NAME }}
steps:
- name: Extract branch name
id: extract_branch_name
Expand All @@ -46,29 +46,7 @@ jobs:
else
echo "BRANCH_NAME=${GITHUB_HEAD_REF}" >> $GITHUB_OUTPUT
fi
# Fetch the latest version from NPM
fetch_latest_version:
needs: [Timestamp]
runs-on: ubuntu-latest
outputs:
core_sdk_latest_version: ${{ steps.get_latest_version.outputs.CORE_SDK_LATEST_VERSION }}
react_sdk_latest_version: ${{ steps.get_latest_version.outputs.REACT_SDK_LATEST_VERSION }}
steps:
- name: Get latest package version
id: get_latest_version
run: |
CORE_SDK_LATEST_VERSION=$(npm view @story-protocol/core-sdk version --silent)
REACT_SDK_LATEST_VERSION=$(npm view @story-protocol/react-sdk version --silent || true)
if [ -z "$REACT_SDK_LATEST_VERSION" ]; then
echo "@story-protocol/react-sdk package not found on NPMJS"
REACT_SDK_LATEST_VERSION="Not_Published"
fi
echo "Latest version of @story-protocol/core-sdk on NPMJS is $CORE_SDK_LATEST_VERSION"
echo "CORE_SDK_LATEST_VERSION=$CORE_SDK_LATEST_VERSION" >> $GITHUB_OUTPUT
echo "Latest version of @story-protocol/react-sdk on NPMJS is $REACT_SDK_LATEST_VERSION"
echo "REACT_SDK_LATEST_VERSION=$REACT_SDK_LATEST_VERSION" >> $GITHUB_OUTPUT
check_if_version_is_valid_for_branch:
needs: [extract_branch_name, print_version_to_publish]
runs-on: ubuntu-latest
Expand All @@ -91,6 +69,45 @@ jobs:
fi
fi
# Fetch the latest version from NPM
fetch_latest_version:
needs: [Timestamp, extract_branch_name]
runs-on: ubuntu-latest
outputs:
core_sdk_latest_version: ${{ steps.get_latest_version.outputs.CORE_SDK_LATEST_VERSION }}
react_sdk_latest_version: ${{ steps.get_latest_version.outputs.REACT_SDK_LATEST_VERSION }}
steps:
- name: Get latest package version
id: get_latest_version
run: |
get_latest_version() {
prefix=$1
package_name=$2
variable_key=$3
echo "$variable_key"
all_versions=$(npm view "$package_name" versions --json)
if [[ "$all_versions" == "["* ]]; then
filtered_versions=$(echo "$all_versions" | jq -r "[.[] | select(startswith(\"$prefix\"))]")
echo "filtered_versions1: $filtered_versions"
else
filtered_versions=$(echo "[$all_versions]" | jq -r "[.[] | select(startswith(\"$prefix\"))]")
echo "filtered_versions2: $filtered_versions"
fi
latest_version=$(echo "$filtered_versions" | jq -r '.[-1]')
if [ "$latest_version" == "null" ]; then
latest_version="Not_Published"
fi
echo "Latest version of $package_name on NPMJS with prefix $prefix is $latest_version"
echo "$variable_key=$latest_version" >> $GITHUB_OUTPUT
}
BRANCH_NAME="${{ needs.extract_branch_name.outputs.branch_name }}"
if [[ "$BRANCH_NAME" == "v1.1" || "$BRANCH_NAME" == "dev_v1.1" ]]; then
get_latest_version "1.1" "bonni-sdk" "CORE_SDK_LATEST_VERSION"
get_latest_version "1.1" "bonnie-react-sdk" "REACT_SDK_LATEST_VERSION"
elif [[ "$BRANCH_NAME" == "main" || "$BRANCH_NAME" == "dev" ]]; then
get_latest_version "1.2" "bonni-sdk" "CORE_SDK_LATEST_VERSION"
get_latest_version "1.2" "bonnie-react-sdk" "REACT_SDK_LATEST_VERSION"
fi
# Fail the PR if the version to be published is the same as the latest version on NPM
fail_if_version_is_same:
needs: [print_version_to_publish, fetch_latest_version]
Expand Down

0 comments on commit bffc63c

Please sign in to comment.