From 85d09af40ce3b1b6005236ca60e85cbd01814964 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Thu, 18 Jan 2024 18:15:12 +0100
Subject: [PATCH] Avoid nested inputs

---
 .gitlab-ci.yml                                 |  3 ++-
 Makefile                                       |  4 +++-
 README.md                                      |  1 -
 backports/OpenTofu/Base.gitlab-ci.yml          |  2 +-
 templates/full-pipeline.yml                    | 17 ++++++++++-------
 tests/integration-tests/Defaults.gitlab-ci.yml |  4 +++-
 tests/integration.gitlab-ci.yml                |  3 ++-
 7 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e3397b8..6de90a5 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -34,7 +34,8 @@ variables:
   # OpenTofu image build variables:
   PLATFORMS: linux/amd64,linux/arm64
   BASE_IMAGE: "alpine:3.19.0"
-  GITLAB_OPENTOFU_IMAGE_NAME: "$CI_REGISTRY_IMAGE/internal/gitlab-opentofu-$OPENTOFU_VERSION:$CI_COMMIT_SHA"
+  GITLAB_OPENTOFU_IMAGE_BASE: "$CI_REGISTRY_IMAGE/internal"
+  GITLAB_OPENTOFU_IMAGE_NAME: "$GITLAB_OPENTOFU_IMAGE_BASE/gitlab-opentofu:$CI_COMMIT_SHA-opentofu$OPENTOFU_VERSION"
 
 gitlab-opentofu-image:build:
   extends: .opentofu-versions
diff --git a/Makefile b/Makefile
index ac8327a..bd7973e 100644
--- a/Makefile
+++ b/Makefile
@@ -30,7 +30,9 @@ backports:
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_build \]\]/build/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_deploy \]\]/deploy/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_cleanup \]\]/cleanup/'
-	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.gitlab_opentofu_image \]\]/$$GITLAB_OPENTOFU_IMAGE/'
+	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs._image_registry_base \]\]/$$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/'
+	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.version \]\]/$$GITLAB_OPENTOFU_VERSION/'
+	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.opentofu_version \]\]/$$OPENTOFU_VERSION/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.root_dir \]\]/$$TF_ROOT/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.state_name \]\]/$$TF_STATE_NAME/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.auto_apply \]\]/$$TF_AUTO_APPLY/'
diff --git a/README.md b/README.md
index 260767c..a58044b 100644
--- a/README.md
+++ b/README.md
@@ -77,7 +77,6 @@ stages: [validate, test, build, deploy, cleanup]
 | `stage_cleanup` | `cleanup` | Defines the cleanup stage. This stage includes the `destroy` and `delete-state` jobs. |
 | `version` | `latest` | Version of this component. Has to be the same as the one in the component include entry. |
 | `opentofu_version` | `1.6.0` | OpenTofu version that should be used. Must be one of `1.6.0`, `1.6.0-rc1`. |
-| `gitlab_opentofu_image` | `$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]` | Tag of the gitlab-opentofu image. |
 | `root_dir` | `${CI_PROJECT_DIR}` | Root directory for the OpenTofu project. |
 | `state_name` | `default` | Remote OpenTofu state name. |
 | `auto_apply` | `false` | Whether the apply job is manual or automatically run. |
diff --git a/backports/OpenTofu/Base.gitlab-ci.yml b/backports/OpenTofu/Base.gitlab-ci.yml
index 081da91..1989cd9 100644
--- a/backports/OpenTofu/Base.gitlab-ci.yml
+++ b/backports/OpenTofu/Base.gitlab-ci.yml
@@ -26,7 +26,7 @@ variables:
 
 .opentofu:default:
   image:
-    name: $GITLAB_OPENTOFU_IMAGE
+    name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
 
   cache:
     key: "$TF_ROOT"
diff --git a/templates/full-pipeline.yml b/templates/full-pipeline.yml
index d243a26..fe22c05 100644
--- a/templates/full-pipeline.yml
+++ b/templates/full-pipeline.yml
@@ -29,12 +29,15 @@ spec:
       description: 'OpenTofu version that should be used.'
 
     # Images
-    gitlab_opentofu_image:
-      # FIXME: This should reference the component tag that is used.
-      #        Currently, blocked by https://gitlab.com/gitlab-org/gitlab/-/issues/438275
-      # default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.opentofu_version ]]'
-      default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
-      description: 'Tag of the gitlab-opentofu image.'
+    _image_registry_base:
+      default: '$CI_REGISTRY/components/opentofu'
+    # FIXME: not yet possible because of https://gitlab.com/gitlab-org/gitlab/-/issues/438722
+    # gitlab_opentofu_image:
+    #   # FIXME: This should reference the component tag that is used.
+    #   #        Currently, blocked by https://gitlab.com/gitlab-org/gitlab/-/issues/438275
+    #   # default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.opentofu_version ]]'
+    #   default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
+    #   description: 'Tag of the gitlab-opentofu image.'
     
     # Configuration
     root_dir: 
@@ -60,7 +63,7 @@ spec:
 
 .default:
   image:
-    name: $[[ inputs.gitlab_opentofu_image ]]
+    name: '$[[ inputs._image_registry_base ]]/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
 
   cache:
     key: "$[[ inputs.root_dir ]]"
diff --git a/tests/integration-tests/Defaults.gitlab-ci.yml b/tests/integration-tests/Defaults.gitlab-ci.yml
index 484b313..92da5eb 100644
--- a/tests/integration-tests/Defaults.gitlab-ci.yml
+++ b/tests/integration-tests/Defaults.gitlab-ci.yml
@@ -1,7 +1,9 @@
 include:
   - component: gitlab.com/$CI_PROJECT_PATH/full-pipeline@$CI_COMMIT_SHA
     inputs:
-      gitlab_opentofu_image: $GITLAB_OPENTOFU_IMAGE_NAME
+      _image_registry_base: $GITLAB_OPENTOFU_IMAGE_BASE
+      version: $CI_COMMIT_SHA
+      opentofu_version: $OPENTOFU_VERSION
       root_dir: $TF_ROOT
       state_name: $TF_STATE_NAME
 
diff --git a/tests/integration.gitlab-ci.yml b/tests/integration.gitlab-ci.yml
index 1f87139..3f10624 100644
--- a/tests/integration.gitlab-ci.yml
+++ b/tests/integration.gitlab-ci.yml
@@ -17,8 +17,9 @@ component:
 backport-templates:
   stage: test-integration
   variables:
+    GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE: $GITLAB_OPENTOFU_IMAGE_BASE
+    GITLAB_OPENTOFU_VERSION: $CI_COMMIT_SHA
     OPENTOFU_VERSION: $LATEST_OPENTOFU_VERSION
-    GITLAB_OPENTOFU_IMAGE: $GITLAB_OPENTOFU_IMAGE_NAME
     TF_STATE_NAME: ci-integration-backports-$CI_PIPELINE_IID-$CI_NODE_INDEX
     TF_ROOT: tests/terraform
   trigger:
-- 
GitLab