diff --git a/kash.sh b/kash.sh index 11b5075..1838c8e 100644 --- a/kash.sh +++ b/kash.sh @@ -517,6 +517,39 @@ get_json_value() { yq --output-format=yaml ".$JSON_FIELD" "$JSON_SRC" } +# Extract version major from a version string. +# Expected args: +# 1. the version string +get_semver_major() { + local VERSION="$1" + local VERSION_REGEX="^([0-9]+)(\.[0-9]+)?(\.[0-9]+)?$" + if [[ "$VERSION" =~ $VERSION_REGEX ]]; then + printf "%s" "${BASH_REMATCH[1]}" + fi +} + +# Extract version minor from a version string. +# Expected args: +# 1. the version string +get_semver_minor() { + local VERSION="$1" + local VERSION_REGEX="^[0-9]+\.([0-9]+)(\.[0-9]+)?$" + if [[ "$VERSION" =~ $VERSION_REGEX ]]; then + printf "%s" "${BASH_REMATCH[1]}" + fi +} + +# Extract version minor from a version string. +# Expected args: +# 1. the version string +get_semver_patch() { + local VERSION="$1" + local VERSION_REGEX="^[0-9]+\.[0-9]+\.([0-9]+)$" + if [[ "$VERSION" =~ $VERSION_REGEX ]]; then + printf "%s" "${BASH_REMATCH[1]}" + fi +} + ### Git ### @@ -1112,6 +1145,11 @@ setup_app_workspace() { local DEVELOPMENT_DIR="$WORKSPACE_DIR/development" git_shallow_clone "$DEVELOPMENT_REPO_URL" "$DEVELOPMENT_DIR" + # NOTE: we don't reuse init_app_infos here since init_app_infos + # fetch app version from package.json (it requires a cloned repo) + # Here we might be setting up workspace for a specific + # branch / tag that's not currently checked out. + # fetch app name and ref (tag or branch) required local APP_NAME APP_NAME=$(yq --output-format=yaml '.name' "$REPO_DIR/package.json") @@ -1168,6 +1206,10 @@ init_app_infos() { APP_NAME=$(yq --output-format=yaml '.name' "$REPO_ROOT/package.json") local APP_VERSION APP_VERSION=$(yq --output-format=yaml '.version' "$REPO_ROOT/package.json") + local APP_VERSION_MAJOR + local APP_VERSION_MINOR + APP_VERSION_MAJOR=$(get_semver_major "$APP_VERSION") + APP_VERSION_MINOR=$(get_semver_minor "$APP_VERSION") local GIT_TAG GIT_TAG=$(get_git_tag "$REPO_ROOT") @@ -1182,9 +1224,12 @@ init_app_infos() { local KLI_FILE="$KLI_BASE/$APP_NAME/$APP_FLAVOR/$APP_NAME" case "$APP_FLAVOR" in - prod | test) + prod) KLI_FILE="$KLI_FILE-$APP_VERSION" ;; + test) + KLI_FILE="$KLI_FILE-$APP_VERSION_MAJOR.$APP_VERSION_MINOR" + ;; *) ;; esac diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 08e7fb9..c9bfba3 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -51,11 +51,23 @@ curl -OLsS "https://raw.githubusercontent.com/kalisio/krawler/master/package.jso [ "$(get_json_value "$TMP_DIR/utils/package.json" 'name')" != "@kalisio/krawler" ] && exit 1 cd ~- +[ "$(get_semver_major "1" )" != "1" ] && exit 1 +[ "$(get_semver_major "1.5" )" != "1" ] && exit 1 +[ "$(get_semver_major "1.5.90" )" != "1" ] && exit 1 +[ "$(get_semver_major "abcd" )" != "" ] && exit 1 + +[ "$(get_semver_minor "10.4" )" != "4" ] && exit 1 +[ "$(get_semver_minor "10.5.0" )" != "5" ] && exit 1 +[ "$(get_semver_minor "abcd" )" != "" ] && exit 1 + +[ "$(get_semver_patch "10.4.59" )" != "59" ] && exit 1 +[ "$(get_semver_patch "abcd" )" != "" ] && exit 1 + ## Git helpers case "$CI_ID" in github) - [ "$(get_git_branch "$ROOT_DIR" )" != "$GITHUB_REF_NAME" ] && exit 1 + [ "''$(get_git_branch "$ROOT_DIR" )" != "$GITHUB_REF_NAME" ] && exit 1 ;; gitlab) [ "$(get_git_branch "$ROOT_DIR" )" != "$CI_COMMIT_REF_NAME" ] && exit 1