From 285c643d9597f028323295a2126aed54ec6723de Mon Sep 17 00:00:00 2001
From: Dimitrios Giannopoulos <dimit.giannopoulos@upnet.gr>
Date: Wed, 11 Sep 2024 08:15:28 +0000
Subject: [PATCH] feat: sanitize version naming

---
 ci-templates/default.yml | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/ci-templates/default.yml b/ci-templates/default.yml
index e49b634..c231446 100644
--- a/ci-templates/default.yml
+++ b/ci-templates/default.yml
@@ -7,11 +7,23 @@ stages:
 
 .default:
   before_script:
+    - set -e
     - |
       if [ "$CI_COMMIT_REF_PROTECTED" = true ] && [ -n "$CI_COMMIT_TAG" ]; then
         export APP_VERSION=$CI_COMMIT_TAG
       elif [ "$CI_COMMIT_REF_NAME" = "develop" ]; then
         export APP_VERSION="develop"
       else
-        export APP_VERSION=$CI_COMMIT_REF_NAME
+        # Replace '/' with '-' and check for any invalid characters
+        APP_VERSION=$(echo "$CI_COMMIT_REF_NAME" | sed 's|/|-|g')
+        
+        # Check for invalid characters (only allow lowercase letters, digits, '_', '.', '-')
+        if echo "$APP_VERSION" | grep -q '[^a-zA-Z0-9._-]'; then
+          echo "Error: Branch name contains invalid characters for Docker tags. Only a-z, 0-9, '_', '.', and '-' are allowed."
+          exit 1
+        fi
+
+        # Convert to lowercase
+        export APP_VERSION=$(echo "$APP_VERSION" | tr 'A-Z' 'a-z')
       fi
+    - echo "APP_VERSION set to $APP_VERSION"
-- 
GitLab