Created
December 29, 2020 11:46
-
-
Save folex/99f39455f07c64b00b74d3ec7eaf9adb to your computer and use it in GitHub Desktop.
Increment semver version as prerelease in Github Actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yarn global add semver | |
PATH="$(yarn global bin):$PATH" | |
# take npm version and increment it | |
PKG_NAME="$(cat package.json | jq -r .name)" | |
# sanitize branch name so it can be used as a semver suffix (replace [^0-9a-zA-Z-] with hyphen) | |
SANITIZED_BRANCH="$(echo -n "${{ env.BRANCH_NAME }}" | tr -C '[:alnum:]-' -)" | |
# take all versions from npm and replace single quotes with double quotes | |
NPM_VERSIONS=$(yarn info --silent fluence versions | 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)" | |
# increment prerelease part of the version | |
PRERELEASE_NPM_VERSION="$(semver --increment prerelease --preid "$SANITIZED_BRANCH" "$LAST_NPM_VERSION")" | |
# take local 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")" | |
# take the highest version | |
MAX_VERSION="$(semver "$LOCAL_PRERELEASE_VERSION" "$PRERELEASE_NPM_VERSION" | tail -n1)" | |
# save info to env | |
echo "FINAL_VERSION=$MAX_VERSION" | tee -a $GITHUB_ENV | |
echo "PKG_NAME=$PKG_NAME" | tee -a $GITHUB_ENV" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment