From c89a644796c932544d9d9eed66c37db468bbf029 Mon Sep 17 00:00:00 2001
From: Bartek Wrona <wrona@syncad.com>
Date: Wed, 26 Jun 2024 18:27:26 +0200
Subject: [PATCH] dist-tag detection logic is based directly on informations
 provided by CI to eliminate branch detection ambiguities

---
 templates/npm_projects.gitlab-ci.yml | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/templates/npm_projects.gitlab-ci.yml b/templates/npm_projects.gitlab-ci.yml
index 56fe843..8ad8778 100644
--- a/templates/npm_projects.gitlab-ci.yml
+++ b/templates/npm_projects.gitlab-ci.yml
@@ -41,7 +41,17 @@ variables:
     NPM_REGISTRY_URL: "gitlab.syncad.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/"
 
   script:
-    - /home/emscripten/scripts/npm_build_package.sh "${SOURCE_DIR}" "${NPM_REGISTRY_URL}" "${NPM_PACKAGE_SCOPE}" "${NPM_PACKAGE_NAME}" "${DIST_DIR}"
+    - |
+      if [ "${CI_COMMIT_REF_PROTECTED}" == "true" ]; then
+        if [ -n "${CI_COMMIT_TAG}" ]; then
+          DIST_TAG="latest" # if package is built for protected tag, let's mark it as latest
+        else
+          DIST_TAG="stable" # otherwise, any build for protected branch will produce stable package
+        fi
+      else
+        DIST_TAG="dev"
+      fi
+      /home/emscripten/scripts/npm_build_package.sh "${SOURCE_DIR}" "${NPM_REGISTRY_URL}" "${NPM_PACKAGE_SCOPE}" "${NPM_PACKAGE_NAME}" "${DIST_DIR}" "${DIST_TAG}"
 
   artifacts:
     reports:
@@ -131,7 +141,7 @@ variables:
 
   script:
     # project version info must be regenerated accoring to correct target registry, package scope and name
-    - /home/emscripten/scripts/npm_generate_version.sh "${SOURCE_DIR}" "registry.npmjs.org/" "@hiveio" "${NPM_PACKAGE_NAME}"
+    - /home/emscripten/scripts/npm_generate_version.sh "${SOURCE_DIR}" "registry.npmjs.org/" "@hiveio" "${NPM_PACKAGE_NAME}" "latest"
     - echo -e "\e[0Ksection_start:$(date +%s):publishing[collapsed=false]\r\e[0KAttempting to publish a package to registry.npmjs.org..."
     - /home/emscripten/scripts/npm_publish.sh "${SOURCE_DIR}" "registry.npmjs.org/" "@hiveio" "${NPM_PUBLISH_TOKEN}"
     - echo -e "\e[0Ksection_end:$(date +%s):publishing\r\e[0KDone"
-- 
GitLab