From 9b69280a41644f127e47c812b64dd1426ff78eb9 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tuxtimo@gmail.com>
Date: Thu, 10 Apr 2025 11:34:35 +0200
Subject: [PATCH] Change default for `version` input to match component version

This change set is a workaround for
https://gitlab.com/gitlab-org/gitlab/-/issues/438275.
It implements an additional release step that "hard codes" the component
version to release in the component templates. This is ugly, but hides
complexity from the component user.

This change set maybe a breaking change to component users that rely on
the default being `latest`. For example, a config like:

    include:
      - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@~latest
        inputs:
          opentofu_version: <OPENTOFU_VERSION>

would break, because the component `version` value is no longer
`latest`, but no refers to the actual version from of `~latest`.
Since, using `~latest` is "discouraged" anyways, we'll still introduce
this breaking change. Users of `~latest` should just specify `version:
latest` in their `inputs`.

Changelog: changed
---
 .gitlab/README.md.template              | 36 -------------------------
 .gitlab/release-notes.md.template       |  4 ---
 .gitlab/scripts/release.sh              |  7 +++++
 .gitlab/scripts/update-self-versions.sh | 29 ++++++++++++++++++++
 README.md                               | 36 -------------------------
 5 files changed, 36 insertions(+), 76 deletions(-)
 create mode 100755 .gitlab/scripts/update-self-versions.sh

diff --git a/.gitlab/README.md.template b/.gitlab/README.md.template
index 5c9e3a1..4c3d68c 100644
--- a/.gitlab/README.md.template
+++ b/.gitlab/README.md.template
@@ -29,10 +29,6 @@ together with an OpenTofu version.
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [validate, test, build, deploy, cleanup]
@@ -43,10 +39,6 @@ stages: [validate, test, build, deploy, cleanup]
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [validate, build, deploy]
@@ -55,10 +47,6 @@ stages: [validate, build, deploy]
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       trigger_in_child_pipeline: true
 ```
@@ -70,10 +58,6 @@ A concrete example may look like this:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@0.10.0
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: 0.10.0 # component version
       opentofu_version: 1.6.1
 
 stages: [validate, test, build, deploy, cleanup]
@@ -100,10 +84,6 @@ Or import all jobs as hidden templates ready to be extended:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/job-templates@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [...]
@@ -188,10 +168,6 @@ The following snippet will auto-encrypt your state with a passphrase coming from
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       auto_encryption: true
       auto_encryption_passphrase: $PASSPHRASE
@@ -220,10 +196,6 @@ An example setup may look like this:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       enable_id_tokens: true
 
@@ -298,10 +270,6 @@ and compose your own pipeline, for example, to just run the `fmt` job you can do
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/fmt@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION>
       opentofu_version: 1.6.1
       root_dir: tofu/
 ```
@@ -315,10 +283,6 @@ you want to extend the jobs:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/job-templates@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION>
       opentofu_version: 1.6.1
 
 plan:
diff --git a/.gitlab/release-notes.md.template b/.gitlab/release-notes.md.template
index aa5790c..3cb628d 100644
--- a/.gitlab/release-notes.md.template
+++ b/.gitlab/release-notes.md.template
@@ -19,10 +19,6 @@ You can use the OpenTofu CI/CD component from the CI/CD catalog using:
 include:
   - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/full-pipeline@$CI_COMMIT_TAG
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: $CI_COMMIT_TAG # component version
       opentofu_version: $LATEST_OPENTOFU_VERSION
 
 stages: [validate, test, build, deploy, cleanup]
diff --git a/.gitlab/scripts/release.sh b/.gitlab/scripts/release.sh
index d6ed49b..d4ec83b 100755
--- a/.gitlab/scripts/release.sh
+++ b/.gitlab/scripts/release.sh
@@ -26,6 +26,13 @@ echo "${version}" | "${script_dir}/check-semantic-version.sh"
 
 echo "Starting release process for ${version} ..."
 
+echo "Updating default value for version input to match ${version} ..."
+
+"${script_dir}/update-self-version.sh" "${version}"
+
+git commit templates/*.yml -m "Update version input default for ${version} release"
+git push origin main
+
 echo "Determing last stable version ..."
 last_stable_version_sha="$(git tag | grep -E '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$' | sort --version-sort --reverse | head -n1 | xargs git rev-list -n1)"
 echo "Using commit '${last_stable_version_sha}' to start the changelog from ..."
diff --git a/.gitlab/scripts/update-self-versions.sh b/.gitlab/scripts/update-self-versions.sh
new file mode 100755
index 0000000..595b446
--- /dev/null
+++ b/.gitlab/scripts/update-self-versions.sh
@@ -0,0 +1,29 @@
+#!/usr/bin/env sh
+
+set -e
+
+script_dir="$(dirname "$0")"
+project_dir="$script_dir/../.."
+
+VERSION="$1"
+if [ -z "$VERSION" ]; then
+  echo "Error: version must be passed as first argument" >&2
+  exit 1
+fi
+
+echo "Updating template files ..."
+
+templates="templates/*.yml"
+templates_exclude="templates/delete-state.yml templates/module-release.yml templates/__internal_id_tokens_base_job.yml"
+
+for relative_template_file in $templates; do
+  if echo "$templates_exclude" | grep -q "$relative_template_file"; then continue; fi
+  template_file="$project_dir/$relative_template_file"
+  tmp_template_file=$(mktemp)
+  echo "Updating $template_file ... "
+  yq eval-all '
+    select(document_index == 0).spec.inputs.version.default
+     = "'"$VERSION"'"
+    ' "$template_file" > "$tmp_template_file"
+  diff -Bw "$template_file" "$tmp_template_file" | patch "$template_file" -
+done
diff --git a/README.md b/README.md
index 4f490c5..c5d7dba 100644
--- a/README.md
+++ b/README.md
@@ -31,10 +31,6 @@ together with an OpenTofu version.
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [validate, test, build, deploy, cleanup]
@@ -45,10 +41,6 @@ stages: [validate, test, build, deploy, cleanup]
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [validate, build, deploy]
@@ -57,10 +49,6 @@ stages: [validate, build, deploy]
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       trigger_in_child_pipeline: true
 ```
@@ -72,10 +60,6 @@ A concrete example may look like this:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@0.10.0
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: 0.10.0 # component version
       opentofu_version: 1.6.1
 
 stages: [validate, test, build, deploy, cleanup]
@@ -102,10 +86,6 @@ Or import all jobs as hidden templates ready to be extended:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/job-templates@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
 
 stages: [...]
@@ -190,10 +170,6 @@ The following snippet will auto-encrypt your state with a passphrase coming from
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       auto_encryption: true
       auto_encryption_passphrase: $PASSPHRASE
@@ -222,10 +198,6 @@ An example setup may look like this:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION> # component version
       opentofu_version: <OPENTOFU_VERSION>
       enable_id_tokens: true
 
@@ -300,10 +272,6 @@ and compose your own pipeline, for example, to just run the `fmt` job you can do
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/fmt@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION>
       opentofu_version: 1.6.1
       root_dir: tofu/
 ```
@@ -317,10 +285,6 @@ you want to extend the jobs:
 include:
   - component: $CI_SERVER_FQDN/components/opentofu/job-templates@<VERSION>
     inputs:
-      # The version must currently be specified explicitly as an input,
-      # to find the correctly associated images. # This can be removed
-      # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved.
-      version: <VERSION>
       opentofu_version: 1.6.1
 
 plan:
-- 
GitLab