From c1e986040cd78867b4cf52d44b9b03296eb13ad8 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Wed, 31 Jan 2024 16:35:55 +0100
Subject: [PATCH] Fix cache:key with slashes with a workaround

---
 backports/OpenTofu/Base.latest.gitlab-ci.yml | 25 ++++++++++++++++----
 templates/apply.yml                          |  4 +++-
 templates/destroy.yml                        |  4 +++-
 templates/fmt.yml                            |  4 +++-
 templates/plan.yml                           |  4 +++-
 templates/validate.yml                       |  4 +++-
 6 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/backports/OpenTofu/Base.latest.gitlab-ci.yml b/backports/OpenTofu/Base.latest.gitlab-ci.yml
index 5600b23..81f4d7a 100644
--- a/backports/OpenTofu/Base.latest.gitlab-ci.yml
+++ b/backports/OpenTofu/Base.latest.gitlab-ci.yml
@@ -64,9 +64,12 @@ opentofu:use-component-instead-of-template:
   #allow_failure: true
   allow_failure: true
   cache:
-    key: "$TF_ROOT"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $TF_ROOT/.terraform/
+  variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$TF_ROOT"
   image:
     name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
   script:
@@ -80,9 +83,12 @@ opentofu:use-component-instead-of-template:
       when: never
     - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.
   cache:
-    key: "$TF_ROOT"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $TF_ROOT/.terraform/
+  variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$TF_ROOT"
   image:
     name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
   script:
@@ -112,9 +118,12 @@ opentofu:use-component-instead-of-template:
       when: never
     - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.
   cache:
-    key: "$TF_ROOT"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $TF_ROOT/.terraform/
+  variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$TF_ROOT"
   image:
     name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
   script:
@@ -132,9 +141,12 @@ opentofu:use-component-instead-of-template:
     - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
       when: manual
   cache:
-    key: "$TF_ROOT"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $TF_ROOT/.terraform/
+  variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$TF_ROOT"
   image:
     name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
   script:
@@ -150,9 +162,12 @@ opentofu:use-component-instead-of-template:
     - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$_TF_AUTO_DESTROY" == "true"'
     - when: manual
   cache:
-    key: "$TF_ROOT"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $TF_ROOT/.terraform/
+  variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$TF_ROOT"
   image:
     name: '$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/gitlab-opentofu:$GITLAB_OPENTOFU_VERSION-opentofu$OPENTOFU_VERSION'
   script:
diff --git a/templates/apply.yml b/templates/apply.yml
index cc1a6d5..8083b36 100644
--- a/templates/apply.yml
+++ b/templates/apply.yml
@@ -59,10 +59,12 @@ spec:
     - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
       when: manual
   cache:
-    key: "$[[ inputs.root_dir ]]"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $[[ inputs.root_dir ]]/.terraform/
   variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
     TF_STATE_NAME: $[[ inputs.state_name ]]
   image:
diff --git a/templates/destroy.yml b/templates/destroy.yml
index 267a0d8..a15e8f9 100644
--- a/templates/destroy.yml
+++ b/templates/destroy.yml
@@ -58,10 +58,12 @@ spec:
     - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$[[ inputs.auto_destroy ]]" == "true"'
     - when: manual
   cache:
-    key: "$[[ inputs.root_dir ]]"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $[[ inputs.root_dir ]]/.terraform/
   variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
     TF_STATE_NAME: $[[ inputs.state_name ]]
   image:
diff --git a/templates/fmt.yml b/templates/fmt.yml
index 730f1a8..857fd3b 100644
--- a/templates/fmt.yml
+++ b/templates/fmt.yml
@@ -59,10 +59,12 @@ spec:
   #allow_failure: $[[ inputs.allow_failure ]]
   allow_failure: true
   cache:
-    key: "$[[ inputs.root_dir ]]"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $[[ inputs.root_dir ]]/.terraform/
   variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
   image:
     name: '$[[ inputs.image_registry_base ]]/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
diff --git a/templates/plan.yml b/templates/plan.yml
index 6cc5b6c..5857577 100644
--- a/templates/plan.yml
+++ b/templates/plan.yml
@@ -69,10 +69,12 @@ spec:
       when: never
     - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.
   cache:
-    key: "$[[ inputs.root_dir ]]"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $[[ inputs.root_dir ]]/.terraform/
   variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
     TF_STATE_NAME: $[[ inputs.state_name ]]
   image:
diff --git a/templates/validate.yml b/templates/validate.yml
index 241ffec..e2b9d22 100644
--- a/templates/validate.yml
+++ b/templates/validate.yml
@@ -53,10 +53,12 @@ spec:
       when: never
     - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.
   cache:
-    key: "$[[ inputs.root_dir ]]"
+    key: "$__CACHE_KEY_HACK"
     paths:
       - $[[ inputs.root_dir ]]/.terraform/
   variables:
+    # FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
+    __CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
     TF_ROOT: $[[ inputs.root_dir ]]
     TF_STATE_NAME: $[[ inputs.state_name ]]
   image:
-- 
GitLab