From 3fab262ef171fb7c6714762b7e54ce00da9c3ba2 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Fri, 19 Jan 2024 09:18:36 +0100
Subject: [PATCH] Note built images in release notes

---
 .gitlab-ci.yml              | 40 +++++++++++++++++++++++++++++++------
 .gitlab/release-template.md |  6 ++++++
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6ff1924..2bc6fe9 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -105,10 +105,15 @@ gitlab-opentofu-image:deploy:with-opentofu-version:
     # We use a dash `-` here, instead of the `+`.
     # This may be problematic, because it indicates a semver prerelease.
     RELEASE_SEMVER: "${CI_COMMIT_TAG}-opentofu${OPENTOFU_VERSION}"
+    RELEASE_IMAGE: "$RELEASE_IMAGE_NAME:$RELEASE_SEMVER"
   before_script:
     - crane auth login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
   script:
-    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE_NAME:$RELEASE_SEMVER"
+    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE"
+    - echo "- $RELEASE_IMAGE" > image$CI_JOB_ID.txt
+  artifacts:
+    paths:
+      - 'image*.txt'
   rules:
     - if: $CI_COMMIT_TAG
 
@@ -121,10 +126,15 @@ gitlab-opentofu-image:deploy:latest-with-opentofu-version:
   variables:
     RELEASE_IMAGE_NAME: "$CI_REGISTRY_IMAGE/gitlab-opentofu"
     RELEASE_SEMVER: "latest-opentofu${OPENTOFU_VERSION}"
+    RELEASE_IMAGE: "$RELEASE_IMAGE_NAME:$RELEASE_SEMVER"
   before_script:
     - crane auth login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
   script:
-    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE_NAME:$RELEASE_SEMVER"
+    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE"
+    - echo "- $RELEASE_IMAGE" > image$CI_JOB_ID.txt
+  artifacts:
+    paths:
+      - 'image*.txt'
   rules:
     - if: $CI_COMMIT_TAG
 
@@ -136,10 +146,15 @@ gitlab-opentofu-image:deploy:latest:
   variables:
     OPENTOFU_VERSION: $LATEST_OPENTOFU_VERSION
     RELEASE_IMAGE_NAME: "$CI_REGISTRY_IMAGE/gitlab-opentofu"
+    RELEASE_IMAGE: "$RELEASE_IMAGE_NAME:$RELEASE_IMAGE_TAG"
   before_script:
     - crane auth login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
   script:
-    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE_NAME:$RELEASE_IMAGE_TAG"
+    - crane copy "$GITLAB_OPENTOFU_IMAGE_NAME" "$RELEASE_IMAGE"
+    - echo "- $RELEASE_IMAGE" > image$CI_JOB_ID.txt
+  artifacts:
+    paths:
+      - 'image*.txt'
   rules:
     - if: $CI_COMMIT_TAG
   parallel:
@@ -151,18 +166,31 @@ gitlab-opentofu-image:deploy:latest:
 
 # If the pipeline is for a new tag with a semantic version, and all previous jobs succeed,
 # create the release.
-create-release:
+.create-release:
   stage: release
   image: registry.gitlab.com/gitlab-org/release-cli:latest
-  rules:
-    - if: $CI_COMMIT_TAG =~ /\d+/
   before_script:
     - apk add --update yq envsubst
   script: 
     - echo "Creating release $CI_COMMIT_TAG"
     - AVAILABLE_OPENTOFU_VERSIONS=$(yq -r '.spec.inputs.opentofu_version.options | filter((. | test("\$.*")) == false) | .[] | "- [`" + . + "`](https://github.com/opentofu/opentofu/releases/tag/v" + . + ")"' templates/full-pipeline.yml)
     - export AVAILABLE_OPENTOFU_VERSIONS
+    - AVAILABLE_IMAGES=$(cat image*.txt)
     - cat .gitlab/release-template.md | envsubst > release-notes.md 
+
+create-release:dry-run:
+  extends: .create-release
+  script:
+    - !reference [.create-release, script]
+    - cat release-notes.md
+  artifacts:
+    paths:
+      - release-notes.md
+
+create-release:
+  extends: .create-release
+  rules:
+    - if: $CI_COMMIT_TAG =~ /\d+/
   release:
     tag_name: $CI_COMMIT_TAG
     description: './release-notes.md'
diff --git a/.gitlab/release-template.md b/.gitlab/release-template.md
index df8c42f..46b7867 100644
--- a/.gitlab/release-template.md
+++ b/.gitlab/release-template.md
@@ -24,3 +24,9 @@ See the [README.md](README.md) for more usage examples.
 This release is available with the following OpenTofu versions:
 
 $AVAILABLE_OPENTOFU_VERSIONS
+
+## Available GitLab OpenTofu images
+
+This release deployed the following `gitlab-opentofu` images:
+
+$AVAILABE_IMAGES
-- 
GitLab