fix versioning

This commit is contained in:
Pavel Murygin 2021-05-25 11:46:09 +03:00
parent 433ce73acc
commit 73c68481c3

View File

@ -5,8 +5,8 @@ on:
# uncomment to release only on tags starting with 'v' # uncomment to release only on tags starting with 'v'
# tags: # tags:
# - "v*" # - "v*"
#branches: branches:
# - "main" - "main"
workflow_dispatch: workflow_dispatch:
@ -39,33 +39,19 @@ jobs:
- name: Get version from npm and increment - name: Get version from npm and increment
run: | run: |
cd vscode-highlight cd vscode-highlight
# install semver and add it to PATH
yarn global add semver
PATH="$(yarn global bin):$PATH" PATH="$(yarn global bin):$PATH"
yarn global add semver
# sanitize branch name so it can be used as a semver suffix (replace [^0-9a-zA-Z-] with hyphen) # take npm version and increment it
SANITIZED_BRANCH="$(echo -n "${{ env.BRANCH_NAME }}" | tr -C '[:alnum:]-' -)"
# get package name from package.json
PKG_NAME="$(cat package.json | jq -r .name)" PKG_NAME="$(cat package.json | jq -r .name)"
NPM_VERSION="$(yarn info --silent "$PKG_NAME" version || true)"
# take all versions from npm and replace single quotes with double quotes NEXT_NPM_VERSION="$(semver --increment patch "$NPM_VERSION")"
NPM_VERSIONS=$(yarn info --silent "$PKG_NAME" versions 2>/dev/null | tr \' \")
# take only versions that contain branch name
NPM_VERSIONS_FILTERED=$(echo $NPM_VERSIONS | jq -r ".[] | select(contains(\"$SANITIZED_BRANCH\"))")
# flatten into a single line
NPM_VERSIONS_FLATTENED=$(echo $NPM_VERSIONS_FILTERED | awk '{print}' ORS=' ')
# sort versions according to semver, take highest (last)
LAST_NPM_VERSION="$(semver -p $(echo $NPM_VERSIONS_FLATTENED) | tail -n1 || true)"
# increment prerelease part of the version
PRERELEASE_NPM_VERSION="$(semver --increment prerelease --preid "$SANITIZED_BRANCH" "${LAST_NPM_VERSION}" || true)"
# take local version # take local version
LOCAL_VERSION="$(cat package.json | jq -r .version)" LOCAL_VERSION="$(cat package.json | jq -r .version)"
# set prerelease part on local version
LOCAL_PRERELEASE_VERSION="$(semver --increment prerelease --preid "$SANITIZED_BRANCH" "${LOCAL_VERSION}-0")" # added '-0' here to avoid semver erroneously increment patch octet. Any suffix works, '-0' is chosen deliberately.
# take the highest version # take maximum of the local and NEXT_NPM versions
MAX_VERSION="$(semver "$LOCAL_PRERELEASE_VERSION" "$PRERELEASE_NPM_VERSION" | tail -n1)" MAX_VERSION="$(semver "$LOCAL_VERSION" "$NEXT_NPM_VERSION" "0.0.0" | tail -n1)"
# save info to env # save info to env
echo "FINAL_VERSION=$MAX_VERSION" | tee -a $GITHUB_ENV echo "FINAL_VERSION=$MAX_VERSION" | tee -a $GITHUB_ENV