From 12fd64493404dcf9c2a0ae12d3aa383a80732934 Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Sun, 11 Dec 2022 15:22:53 +0100
Subject: [PATCH] refactor: use $environment_type & $environment_name

Deprecated $appname and $env dynamic variables with $environment_name & $environment_type
With backward compatibility (legacy vars still usable)
---
 README.md                    |  6 ++--
 templates/gitlab-ci-helm.yml | 60 ++++++++++++++++++++++--------------
 2 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md
index 4c9d4b3..236eb07 100644
--- a/README.md
+++ b/README.md
@@ -152,8 +152,6 @@ Here are variables supported to configure review environments:
 | `HELM_REVIEW_ENVIRONMENT_SCHEME` | The review environment protocol scheme | `https` |
 | `HELM_REVIEW_ENVIRONMENT_DOMAIN` | The review environment domain | _none_ |
 
-Note: By default review `environment.url` will be built as `${HELM_REVIEW_ENVIRONMENT_SCHEME}://${$CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${HELM_REVIEW_ENVIRONMENT_DOMAIN}`
-
 #### Integration environment
 
 The integration environment is the environment associated to your integration branch (`develop` by default).
@@ -222,9 +220,9 @@ In order to be able to implement some **genericity** in your scripts and templat
 
 2.  you should use available environment variables:
   
-  * any [GitLab CI variable](https://docs.gitlab.com/ee/ci/variables/#predefined-environment-variables)
+  * any [GitLab CI variable](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)
     (ex: `${CI_ENVIRONMENT_URL}` to retrieve the actual environment exposed route)
-  * any [custom variable](https://docs.gitlab.com/ee/ci/variables/#custom-environment-variables)
+  * any [custom variable](https://docs.gitlab.com/ee/ci/variables/#add-a-cicd-variable-to-a-project)
     (ex: `${SECRET_TOKEN}` that you have set in your project CI/CD variables)
 
 > :warning: 
diff --git a/templates/gitlab-ci-helm.yml b/templates/gitlab-ci-helm.yml
index 97c8c34..745aee9 100644
--- a/templates/gitlab-ci-helm.yml
+++ b/templates/gitlab-ci-helm.yml
@@ -368,19 +368,28 @@ stages:
 
   # deploy application
   function deploy() {
-    export env=$1
-    export appname=$2
+    export environment_type=$1
+    export environment_name=$2
+    namespace=$3
+    values_files=$4
+
+    # backwards compatibility
+    export env=$environment_type
+    export appname=$environment_name
+
     # extract hostname from $CI_ENVIRONMENT_URL
     hostname=$(echo "$CI_ENVIRONMENT_URL" | awk -F[/:] '{print $4}')
     export hostname
 
-    namespace=$3
-    values_files=$4
+    log_info "--- \\e[32mdeploy\\e[0m"
+    log_info "--- \$namespace: \\e[33;1m${namespace}\\e[0m"
+    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
+    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
+    log_info "--- \$hostname: \\e[33;1m${hostname}\\e[0m"
 
-    log_info "--- \\e[32mdeploy\\e[0m (env: \\e[33;1m${env}\\e[0m)"
-    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
-    log_info "--- env: \\e[33;1m${env}\\e[0m"
-    log_info "--- hostname: \\e[33;1m${hostname}\\e[0m"
+    # unset any upstream deployment env & artifacts
+    rm -f helm.env
+    rm -f environment_url.txt
 
     helm_opts=$(get_helm_config_opt)
 
@@ -415,24 +424,28 @@ stages:
     fi
     log_info "--- using \\e[32mpackage\\e[0m: \\e[33;1m${package}\\e[0m"
 
-
     # shellcheck disable=SC2086
-    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $helm_values_opt --set "${HELM_ENV_VALUE_NAME}=$env,${HELM_HOSTNAME_VALUE_NAME}=$hostname" $HELM_DEPLOY_ARGS $appname $package
+    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $helm_values_opt --set "${HELM_ENV_VALUE_NAME}=$environment_type,${HELM_HOSTNAME_VALUE_NAME}=$hostname" $HELM_DEPLOY_ARGS $environment_name $package
 
     # finally persist environment url
     echo "$CI_ENVIRONMENT_URL" > environment_url.txt
-    echo -e "environment_type=$env\\nenvironment_name=$appname\\nenvironment_url=$CI_ENVIRONMENT_URL" > helm.env
+    echo -e "environment_type=$environment_type\\nenvironment_name=$environment_name\\nenvironment_url=$CI_ENVIRONMENT_URL" > helm.env
   }
 
   # delete application (and dependencies)
   function delete() {
-    export env=$1
-    export appname=$2
+    export environment_type=$1
+    export environment_name=$2
     namespace=$3
 
-    log_info "--- \\e[32mdelete\\e[0m (env: ${env})"
-    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
-    log_info "--- env: \\e[33;1m${env}\\e[0m"
+    # backwards compatibility
+    export env=$environment_type
+    export appname=$environment_name
+
+    log_info "--- \\e[32mdelete"
+    log_info "--- \$namespace: \\e[33;1m${namespace}\\e[0m"
+    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
+    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
 
     helm_opts=$(get_helm_config_opt)
 
@@ -447,18 +460,19 @@ stages:
     fi
 
     # shellcheck disable=SC2086
-    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $HELM_DELETE_ARGS $appname
+    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $HELM_DELETE_ARGS $environment_name
   }
 
   # test application (and dependencies)
   function test() {
-    export env=$1
-    export appname=$2
+    export environment_type=$1
+    export environment_name=$2
     namespace=$3
 
-    log_info "--- \\e[32mtest\\e[0m (env: ${env})"
-    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
-    log_info "--- env: \\e[33;1m${env}\\e[0m"
+    log_info "--- \\e[32mtest\\e[0m (env: ${environment_type})"
+    log_info "--- \$namespace: \\e[33;1m${namespace}\\e[0m"
+    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
+    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
 
     helm_opts=$(get_helm_config_opt)
 
@@ -473,7 +487,7 @@ stages:
     fi
 
     # shellcheck disable=SC2086
-    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $HELM_TEST_ARGS $appname
+    helm ${TRACE+--debug} $helm_opts $helm_namespace_opt $HELM_TEST_ARGS $environment_name
   }
 
   function maybe_install_curl() {
-- 
GitLab