From c0d51df73f35705f839afffb85415c4b6eed7115 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABtan=20Montury?=
 <10528250-pytgaen@users.noreply.gitlab.com>
Date: Sun, 29 Sep 2024 09:41:49 +0000
Subject: [PATCH] fix: bats libs not downloaded behind a proxy

Fixed by installing classic wget

co-authored-by: Pierre Smeyers <pierre.smeyers@gmail.com>
---
 templates/gitlab-ci-bash.yml | 39 ++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/templates/gitlab-ci-bash.yml b/templates/gitlab-ci-bash.yml
index 0666e45..c9716e7 100644
--- a/templates/gitlab-ci-bash.yml
+++ b/templates/gitlab-ci-bash.yml
@@ -314,6 +314,28 @@ stages:
     done
   }
 
+  function maybe_install_packages() {
+    if command -v apt-get > /dev/null
+    then
+      # Debian
+      if ! dpkg --status "$@" > /dev/null
+      then
+        apt-get update
+        apt-get install --no-install-recommends --yes --quiet "$@"
+      fi
+    elif command -v apk > /dev/null
+    then
+      # Alpine
+      if ! apk info --installed "$@" > /dev/null
+      then
+        apk add --no-cache "$@"
+      fi
+    else
+      log_error "... didn't find any supported package manager to install $*"
+      exit 1
+    fi
+  }
+
   function install_bats_libs() {
     export BATS_LIBRARIES_DIR=/opt/bats/libexec
 
@@ -322,6 +344,13 @@ stages:
       return
     fi
 
+    if [[ "$https_proxy" || "$HTTPS_PROXY" ]]
+    then
+      # BusyBox wget doesn't support proxies and TLS/SSL at the same time: need to use offical full-featured wget instead
+
+      maybe_install_packages wget
+    fi
+
     # install Bats libraries
     for lib in $BASH_BATS_LIBRARIES
     do
@@ -333,7 +362,7 @@ stages:
       target=$(mktemp)
 
       # 1: download
-      log_info " ... download"
+      log_info " ... download with wget"
       wget -O "$target" "$lib_url"
 
       # 2: unzip
@@ -349,9 +378,11 @@ stages:
       fi
     done
 
-    # debug log
-    log_info " ... DONE"
-    ls -lart "$BATS_LIBRARIES_DIR"
+    if [ -n "${TRACE}" ]; then
+      # debug log
+      log_info " ... DONE"
+      ls -lart "$BATS_LIBRARIES_DIR"
+    fi
   }
 
   unscope_variables
-- 
GitLab