From fcd186e84c613eb7e002f2bee20d76d43ab939f9 Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Tue, 27 Dec 2022 16:01:40 +0100
Subject: [PATCH] refactor: manage Helm config and cache with env

---
 templates/gitlab-ci-helm.yml | 38 ++++++++++++++----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/templates/gitlab-ci-helm.yml b/templates/gitlab-ci-helm.yml
index e8a91cf..dca943c 100644
--- a/templates/gitlab-ci-helm.yml
+++ b/templates/gitlab-ci-helm.yml
@@ -325,10 +325,6 @@ stages:
     done
   }
 
-  function get_helm_opts() {
-    echo "${TRACE+--debug} --registry-config $CI_PROJECT_DIR/.config/helm/registry.json --repository-cache $CI_PROJECT_DIR/.cache/helm/repository --repository-config $CI_PROJECT_DIR/.config/helm/repositories.yaml"
-  }
-
   function setup_kubeconfig() {
     if [ -n "$1" ]; then
       export KUBECONFIG="$CI_PROJECT_DIR/.kubeconfig"
@@ -345,10 +341,6 @@ stages:
     # Use cacheable folders
     mkdir -p "$CI_PROJECT_DIR/.config/helm/"
     mkdir -p "$CI_PROJECT_DIR/.cache/helm/repository/"
-    ln -s "$CI_PROJECT_DIR/.cache" ~/.cache
-    ln -s "$CI_PROJECT_DIR/.config" ~/.config
-
-    helm_opts=$(get_helm_opts)
 
     # Install helm repositories
     for repo in $HELM_REPOS
@@ -367,7 +359,7 @@ stages:
           log_info "--- login to OCI-registry \\e[32m${repo_name}\\e[0m: \\e[33;1m${registry_host}\\e[0m"
           export HELM_EXPERIMENTAL_OCI=1
           # shellcheck disable=SC2086
-          echo "$repo_password" | helm $helm_opts registry login "$registry_host" --username "$repo_user" --password-stdin
+          echo "$repo_password" | helm ${TRACE+--debug} registry login "$registry_host" --username "$repo_user" --password-stdin
         else
           log_warn "--- OCI-registry \\e[32m${repo_name}\\e[0m (\\e[33;1m${repo_url}\\e[0m) defined, but no credentials found (\$HELM_REPO_${repo_name_ssc}_USER/\$HELM_REPO_${repo_name_ssc}_PASSWORD)"
         fi
@@ -376,17 +368,17 @@ stages:
         then
           log_info "--- add repository \\e[32m${repo_name}\\e[0m: \\e[33;1m${repo_url}\\e[0m (with user/password auth)"
           # shellcheck disable=SC2086
-          echo "$repo_password" | helm $helm_opts repo add "$repo_name" "$repo_url" --username "$repo_user" --password-stdin
+          echo "$repo_password" | helm ${TRACE+--debug} repo add "$repo_name" "$repo_url" --username "$repo_user" --password-stdin
         else
           log_info "--- add repository \\e[32m${repo_name}\\e[0m: \\e[33;1m${repo_url}\\e[0m (unauthenticated)"
           # shellcheck disable=SC2086
-          helm $helm_opts repo add "$repo_name" "$repo_url"
+          helm ${TRACE+--debug} repo add "$repo_name" "$repo_url"
         fi
       fi
     done
 
     # shellcheck disable=SC2086
-    helm $helm_opts repo update
+    helm ${TRACE+--debug} repo update
   }
 
   function awkenvsubst() {
@@ -438,7 +430,7 @@ stages:
       log_info "--- \\e[32mpre-deploy hook\\e[0m (\\e[33;1m${prescript}\\e[0m) not found: skip"
     fi
 
-    helm_opts=$(get_helm_opts)
+    helm_opts=${TRACE+--debug}
     
     helm_opts="$helm_opts --set ${HELM_ENV_VALUE_NAME}=$environment_type"
     helm_opts="$helm_opts --set ${HELM_HOSTNAME_VALUE_NAME}=$hostname"
@@ -517,7 +509,7 @@ stages:
       log_info "--- \\e[32mpre-delete hook\\e[0m (\\e[33;1m${prescript}\\e[0m) not found: skip"
     fi
 
-    helm_opts=$(get_helm_opts)
+    helm_opts=${TRACE+--debug}
 
     if [ -f "$CI_PROJECT_DIR/.kubeconfig" ]; then
       log_info "--- using \\e[32mkubeconfig\\e[0m: \\e[33;1m$CI_PROJECT_DIR/.kubeconfig\\e[0m"
@@ -553,7 +545,7 @@ stages:
     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_opts)
+    helm_opts=${TRACE+--debug}
 
     if [ -f "$CI_PROJECT_DIR/.kubeconfig" ]; then
       log_info "--- using \\e[32mkubeconfig\\e[0m: \\e[33;1m$CI_PROJECT_DIR/.kubeconfig\\e[0m"
@@ -584,16 +576,13 @@ stages:
     fi
 
     add_helm_repositories
-    helm_opts=$(get_helm_opts)
 
     # helm package
     # shellcheck disable=SC2086
-    helm $helm_opts $HELM_PACKAGE_ARGS $helm_version_opts $HELM_CHART_DIR --destination helm_packages
+    helm ${TRACE+--debug} $HELM_PACKAGE_ARGS $helm_version_opts $HELM_CHART_DIR --destination helm_packages
   }
 
   function helm_publish() {
-    helm_opts=$(get_helm_opts)
-
     helm_package=$(ls -1 ./helm_packages/*.tgz 2>/dev/null || echo "")
     if [[ -z "$helm_package" ]]; then
       log_error "No package found to deploy"
@@ -633,17 +622,17 @@ stages:
       then
         registry_host=$(echo "$HELM_PUBLISH_URL" | awk -F[/:] '{print $4}')
         # shellcheck disable=SC2086
-        echo "$password" | helm $helm_opts registry login "$registry_host" --username "$username" --password-stdin
+        echo "$password" | helm ${TRACE+--debug} registry login "$registry_host" --username "$username" --password-stdin
         # enable OCI support prior to v3.8.0
         export HELM_EXPERIMENTAL_OCI=1
         # shellcheck disable=SC2086
-        helm $helm_opts push "$helm_package" "$HELM_PUBLISH_URL"
+        helm ${TRACE+--debug} push "$helm_package" "$HELM_PUBLISH_URL"
       else
         log_info "Installing cm-push plugin (version ${HELM_CM_PUSH_PLUGIN_VERSION:-latest})..."
         # shellcheck disable=SC2086
-        helm $helm_opts plugin install ${HELM_CM_PUSH_PLUGIN_VERSION:+--version "$HELM_CM_PUSH_PLUGIN_VERSION"} https://github.com/chartmuseum/helm-push || true
+        helm ${TRACE+--debug} plugin install ${HELM_CM_PUSH_PLUGIN_VERSION:+--version "$HELM_CM_PUSH_PLUGIN_VERSION"} https://github.com/chartmuseum/helm-push || true
         # shellcheck disable=SC2086
-        helm $helm_opts cm-push --username "$username" --password "$password" "$helm_package" "$HELM_PUBLISH_URL"
+        helm ${TRACE+--debug} cm-push --username "$username" --password "$password" "$helm_package" "$HELM_PUBLISH_URL"
       fi
       ;;
     post)
@@ -683,6 +672,9 @@ stages:
   services:
     - name: "$TBC_TRACKING_IMAGE"
       command: ["--service", "helm", "3.3.2" ]
+  variables:
+    HELM_CACHE_HOME: $CI_PROJECT_DIR/.cache/helm
+    HELM_CONFIG_HOME: $CI_PROJECT_DIR/.config/helm
   before_script:
     - *helm-scripts
     - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
-- 
GitLab