From c4c47a781b92b5f5927c592814c9173a8c09f859 Mon Sep 17 00:00:00 2001
From: mptr <5887228-mptr@users.noreply.gitlab.com>
Date: Tue, 18 Jun 2024 14:40:59 +0000
Subject: [PATCH] fix: send init stdout to stderr

---
 src/gitlab-tofu.sh     | 6 +++++-
 templates/validate.yml | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh
index 0f8b02b..116af65 100644
--- a/src/gitlab-tofu.sh
+++ b/src/gitlab-tofu.sh
@@ -148,6 +148,9 @@ terraform_authenticate_private_registry() {
 # to calling the wrapper `terraform` commands.
 TF_IMPLICIT_INIT=${TF_IMPLICIT_INIT:-true}
 
+# Allows users to continue the actual command in case init failed
+TF_IGNORE_INIT_ERRORS=${TF_IGNORE_INIT_ERRORS:-false}
+
 terraform_init() {
   # If TF_INIT_NO_RECONFIGURE is not set to 'true',
   # a `-reconfigure` flag is added to the `terraform init` command.
@@ -157,7 +160,8 @@ terraform_init() {
 
   # We want to allow word splitting here for TF_INIT_FLAGS
   # shellcheck disable=SC2086
-  tofu "${TF_CHDIR_OPT}" init "${@}" -input=false ${tf_init_reconfigure_flag} ${TF_INIT_FLAGS}
+  tofu "${TF_CHDIR_OPT}" init "${@}" -input=false ${tf_init_reconfigure_flag} ${TF_INIT_FLAGS} \
+    1>&2 || $TF_IGNORE_INIT_ERRORS
 }
 
 # If this script is executed and not sourced, a terraform command is ran.
diff --git a/templates/validate.yml b/templates/validate.yml
index 3be97a1..02578ed 100644
--- a/templates/validate.yml
+++ b/templates/validate.yml
@@ -69,6 +69,7 @@ spec:
     __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
     TF_STATE_NAME: $[[ inputs.state_name ]]
+    TF_IGNORE_INIT_ERRORS: 'true' # Tofu can report errors which might be the reason init failed.
   image:
     name: '$[[ inputs.image_registry_base ]]/$[[ inputs.image_name ]]:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
   script:
-- 
GitLab