From ac6b1bd0b4684c02066f8511ae3037abc15c599f Mon Sep 17 00:00:00 2001
From: Kyle Harding <kyle@balena.io>
Date: Mon, 11 Nov 2024 16:27:41 -0500
Subject: [PATCH] Generate datestamps during docker bake and not on merge

This more accurately represents when the image was published,
and not when the bake files were last updated.

Change-type: minor
Signed-off-by: Kyle Harding <kyle@balena.io>
---
 scripts/blueprints/workflows/os-arch.yaml     |  2 ++
 scripts/blueprints/workflows/os-device.yaml   |  2 ++
 scripts/blueprints/workflows/stack-arch.yaml  |  2 ++
 .../blueprints/workflows/stack-device.yaml    |  2 ++
 scripts/generate-bakefiles.js                 | 21 ++++++++++---------
 5 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/scripts/blueprints/workflows/os-arch.yaml b/scripts/blueprints/workflows/os-arch.yaml
index dbd483107ce0..031bf198689c 100644
--- a/scripts/blueprints/workflows/os-arch.yaml
+++ b/scripts/blueprints/workflows/os-arch.yaml
@@ -95,6 +95,8 @@ output:
               registry: docker.io
               username: ${{ secrets.BALENAIMAGES_USER }}
               password: ${{ secrets.BALENAIMAGES_TOKEN }}
+          - name: Set the DATESTAMP variable
+            run: echo "DATESTAMP=$(date +%Y%m%d)" >> "${GITHUB_ENV}"
           # https://github.com/docker/bake-action
           - name: Docker bake
             continue-on-error: false
diff --git a/scripts/blueprints/workflows/os-device.yaml b/scripts/blueprints/workflows/os-device.yaml
index d955a188c8ca..4ea609b56ec9 100644
--- a/scripts/blueprints/workflows/os-device.yaml
+++ b/scripts/blueprints/workflows/os-device.yaml
@@ -95,6 +95,8 @@ output:
               registry: docker.io
               username: ${{ secrets.BALENAIMAGES_USER }}
               password: ${{ secrets.BALENAIMAGES_TOKEN }}
+          - name: Set the DATESTAMP variable
+            run: echo "DATESTAMP=$(date +%Y%m%d)" >> "${GITHUB_ENV}"
           # https://github.com/docker/bake-action
           - name: Docker bake
             continue-on-error: false
diff --git a/scripts/blueprints/workflows/stack-arch.yaml b/scripts/blueprints/workflows/stack-arch.yaml
index e86f111f3f27..009935147f9f 100644
--- a/scripts/blueprints/workflows/stack-arch.yaml
+++ b/scripts/blueprints/workflows/stack-arch.yaml
@@ -96,6 +96,8 @@ output:
               registry: docker.io
               username: ${{ secrets.BALENAIMAGES_USER }}
               password: ${{ secrets.BALENAIMAGES_TOKEN }}
+          - name: Set the DATESTAMP variable
+            run: echo "DATESTAMP=$(date +%Y%m%d)" >> "${GITHUB_ENV}"
           # https://github.com/docker/bake-action
           - name: Docker bake
             continue-on-error: false
diff --git a/scripts/blueprints/workflows/stack-device.yaml b/scripts/blueprints/workflows/stack-device.yaml
index 3fa5d70e2d0d..ef68cdaf030c 100644
--- a/scripts/blueprints/workflows/stack-device.yaml
+++ b/scripts/blueprints/workflows/stack-device.yaml
@@ -96,6 +96,8 @@ output:
               registry: docker.io
               username: ${{ secrets.BALENAIMAGES_USER }}
               password: ${{ secrets.BALENAIMAGES_TOKEN }}
+          - name: Set the DATESTAMP variable
+            run: echo "DATESTAMP=$(date +%Y%m%d)" >> "${GITHUB_ENV}"
           # https://github.com/docker/bake-action
           - name: Docker bake
             continue-on-error: false
diff --git a/scripts/generate-bakefiles.js b/scripts/generate-bakefiles.js
index 9f6361e127b2..d3a51c172174 100644
--- a/scripts/generate-bakefiles.js
+++ b/scripts/generate-bakefiles.js
@@ -32,14 +32,6 @@ const balena = getSdk({
 	dataDirectory: false,
 });
 
-function yyyymmdd() {
-	const now = new Date();
-	const y = now.getFullYear();
-	const m = now.getMonth() + 1;
-	const d = now.getDate();
-	return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;
-}
-
 function getVersionAliases(version, latestVersion, generateAllAliases = true) {
 	// version can be a full version x.y.z or x.y or just x (only for latest version if generateAllAliases is false).
 	const result = [version];
@@ -150,6 +142,11 @@ function generateBakeFile(context, tags) {
 				cache_from: tags,
 			},
 		},
+		variable: {
+			DATESTAMP: {
+				default: '$DATESTAMP',
+			},
+		},
 	};
 
 	const destination = path.join(DEST_DIR, context.imageName + '.json');
@@ -196,7 +193,11 @@ async function generateOsArchLibrary(context) {
 	}
 
 	let repo = [NAMESPACE, context.imageName].join('/');
-	const tags = generateCombinations([osVersions, variants, [yyyymmdd(), null]]);
+	const tags = generateCombinations([
+		osVersions,
+		variants,
+		['${DATESTAMP}', null],
+	]);
 
 	// loop over all tags and append the repo as a prefix
 	let repoTags = tags.map((tag) => {
@@ -257,7 +258,7 @@ async function generateStackLibrary(context) {
 		stackVersions,
 		osVersions,
 		variants,
-		[yyyymmdd(), null],
+		['${DATESTAMP}', null],
 	]);
 
 	// loop over all tags and append the repo as a prefix