From 229a4dec77dd503ca76f4a31121749b0d7d177e0 Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@seco.com>
Date: Fri, 8 Sep 2023 07:59:08 +0200
Subject: [PATCH] Define MACHINE variable on job level

Instead of passing MACHINE from stage to stage or loading it from
testdata.json, use the original value from the Jinja2 loop and set it
directly for each job where it is used.
---
 build-pipeline-yocto.yml.jinja2 |  3 +++
 build-pipeline.yml              | 13 +++++--------
 scripts/package_release.py      | 19 ++++++++++---------
 3 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/build-pipeline-yocto.yml.jinja2 b/build-pipeline-yocto.yml.jinja2
index dd8ef24b..253a6ba1 100644
--- a/build-pipeline-yocto.yml.jinja2
+++ b/build-pipeline-yocto.yml.jinja2
@@ -134,6 +134,7 @@ platformtest:{{ machine }}:
 package-{{ machine }}:
   extends: .package
   variables:
+    MACHINE: {{ machine }}
     PACKAGE_TYPE: image
     ASSOCIATED_BUILD_JOB: build-{{ machine }}
   needs:
@@ -174,6 +175,8 @@ deploy-{{ machine }}:
 # --------------------------------------------------------------------------------------
 generate-alphaplan-data-{{ machine }}:
   extends: .generate_alphaplan_data
+  variables:
+    MACHINE: {{ machine }}
   needs:
     - deploy-{{ machine }}
     - build-version
diff --git a/build-pipeline.yml b/build-pipeline.yml
index 4b17f40a..53494970 100644
--- a/build-pipeline.yml
+++ b/build-pipeline.yml
@@ -331,8 +331,6 @@ workflow:
   artifacts:
     paths:
       - release/**/**/*
-    reports:
-      dotenv: package.env
   timeout: 90m
   before_script:
     # We do this manually since we do not use GitLab's default artifact downloader
@@ -347,11 +345,13 @@ workflow:
         --doc-dir=.
         --output-dir=release/${RELEASE_NAME}
         --release-version=${RELEASE_VERSION}
+        --machine=${MACHINE}
     - elif [[ "${PACKAGE_TYPE}" == "sdk" ]]; then
     - .gitlab-ci/scripts/package_release.py
         --sdk-dir="${SDK_PATH}"
         --output-dir=release/${RELEASE_NAME}
         --release-version=${RELEASE_VERSION}
+        --machine=${MACHINE}
     - fi
   cache:
     - key: ${CI_PIPELINE_ID}-${ASSOCIATED_BUILD_JOB}
@@ -361,7 +361,6 @@ workflow:
       policy: push
       paths:
         - release
-        - package.env
 
 # --------------------------------------------------------------------------------------
 # Stage: deploy
@@ -371,10 +370,9 @@ workflow:
     - when: manual
       allow_failure: true
   before_script:
-    # We do this manually since we do not use GitLab's default artifact downloader
-    - source package.env
-    # Save dotenv data for next stage
-    - echo "MACHINE=${MACHINE}" >> deploy.env
+    # Save MACHINE for confluence stage. This variable cannot be passed directly on the
+    # job definition level because the confluence stage is machine-independent.
+    - echo "MACHINE=${MACHINE}" > deploy.env
     # Expand eventual nested variables contained within the DEPLOY_* variables
     # FIXME: For now we need a double 'eval' here due to a GitLab bug:
     # https://gitlab.com/gitlab-org/gitlab/-/issues/273409
@@ -495,7 +493,6 @@ workflow:
   rules:
     - if: $ALPHAPLAN_STAGE == "true"
   script:
-    # MACHINE is available from deploy.env
     # RELEASE_NAME is available from build-version.env
     - .gitlab-ci/scripts/generate_alphaplan_fwr_file.py
         --machine="${MACHINE}"
diff --git a/scripts/package_release.py b/scripts/package_release.py
index 1b2b950a..a131d904 100755
--- a/scripts/package_release.py
+++ b/scripts/package_release.py
@@ -92,6 +92,12 @@ def main():
         dest="release_version",
         required=True,
     )
+    parser.add_argument(
+        "--machine",
+        help="""Machine""",
+        dest="machine",
+        required=True,
+    )
     args, _ = parser.parse_known_args()
 
     # Get bitbake variables from testdata.json file
@@ -118,7 +124,6 @@ def main():
     with open(testdata_files[0], "r", encoding="utf-8") as f:
         buildvars = json.load(f)
 
-    machine = buildvars["MACHINE"]
     sdkname = buildvars["TOOLCHAIN_OUTPUTNAME"]
     image_artifacts = buildvars["DISTRO_IMAGES"].split()
     artifacts = buildvars["DISTRO_RELEASE_ARTEFACTS"].split()
@@ -176,7 +181,7 @@ def main():
 
         # Generate metadata file
         generate_metadata(
-            machine,
+            args.machine,
             args.release_version,
             artifacts,
             sdkname,
@@ -185,23 +190,19 @@ def main():
         artifacts.append(os.path.join(args.images_dir, "metainfo.json"))
 
         # Copy files
-        copy_files(artifacts, os.path.join(args.output_dir, machine))
+        copy_files(artifacts, os.path.join(args.output_dir, args.machine))
 
     # Package SDK
     if args.sdk_dir is not None:
         sdkfiles = glob.glob(os.path.join(args.sdk_dir, f"{sdkname}*"))
 
         # Generate MD5 sums file
-        sdk_md5sums_file = os.path.join(machine, "sdk", "md5sums.txt")
+        sdk_md5sums_file = os.path.join(args.machine, "sdk", "md5sums.txt")
         generate_md5sums_file(sdkfiles, sdk_md5sums_file)
         sdkfiles.append(sdk_md5sums_file)
 
         # Copy files
-        copy_files(sdkfiles, os.path.join(args.output_dir, machine, "sdk"))
-
-    # Store pathes and other stuff in environment variable file
-    with open("package.env", "w", encoding="utf-8") as env_file:
-        env_file.write(f"MACHINE={machine}\n")
+        copy_files(sdkfiles, os.path.join(args.output_dir, args.machine, "sdk"))
 
 
 if __name__ == "__main__":
-- 
GitLab