From ce60dde95b241691d18aecd0873841d0c289172f Mon Sep 17 00:00:00 2001
From: Lorenzo Pagliai <lorenzo.pagliai@seco.com>
Date: Wed, 8 Feb 2023 14:16:57 +0100
Subject: [PATCH] Include release stage in the tag workflow

* Add intel_apl support inside main .yml file
* Include changelog.md file inside the notify (the file is both uploaded
to Azure and accessible via the Teams notification)
* Insert release stage allowing to create a GitLab release when a tag is
created containing all the links to access built artifacts and changelog
---
 manifest-pipeline-yocto.yml | 171 ++++++++++++++++++++++--------------
 notification.json           |   7 ++
 2 files changed, 110 insertions(+), 68 deletions(-)

diff --git a/manifest-pipeline-yocto.yml b/manifest-pipeline-yocto.yml
index b1596f1..27fa237 100644
--- a/manifest-pipeline-yocto.yml
+++ b/manifest-pipeline-yocto.yml
@@ -9,6 +9,7 @@ include:
     - local: boards/.c20.yml
     - local: boards/.c31.yml
     - local: boards/.tanaro.yml
+    - local: boards/.intel_apl.yml
     
 variables:
   # The id of the gitlab project used in the rules section to not run pipelines in
@@ -43,6 +44,7 @@ stages:
   - deploy
   - changelog
   - notify
+  - release
 
 workflow:
   rules:
@@ -155,7 +157,7 @@ retrigger:
   extends:
     - .infrastructure
     - .full_build_pipeline
-  rules:
+  rules: &build_rule
     - if: $CI_PIPELINE_SOURCE == "pipeline"
       when: "manual"
     - if: $CI_PIPELINE_SOURCE == "schedule"
@@ -226,29 +228,13 @@ retrigger:
         fi
 
   artifacts:
+     expire_in: 4 weeks
      paths:
       - $BUILD_DIRECTORY/$IMAGES_PATH/$MACHINE/
       - pathname.txt
       - Job_report_$BOARD.txt
       - Job_message_$BOARD.txt
   allow_failure: true 
-
-#############################
-####### CHANGE LOG ##########
-#############################
-
-changelog:
-  stage: changelog
-  extends: .infrastructure
-  script: .gitlab-ci/scripts/changelog_generator.py
-              --token=${GITBOT_TOKEN}
-              --branch=${MASTER_BRANCH}
-              > changelog.md
-  artifacts:
-    expire_in: 4 weeks
-    paths:
-      - "changelog.md"
-
 #############################
 ########## DEPLOY ###########
 #############################
@@ -264,8 +250,8 @@ changelog:
  timeout: 20 m
  tags:
    - azure_deploy
- rules:
-    # Trigger deploy jobs only on a schedule basis (for weekly release)
+ rules: &schedule_tag_rule
+    # Trigger deploy jobs only on a schedule basis (for weekly release) or on tag
     - if: $CI_PIPELINE_SOURCE == "schedule" 
     - if: $CI_COMMIT_TAG
  script:
@@ -514,6 +500,24 @@ changelog:
     - Job_message_$BOARD.txt
     - Link_report_$BOARD.txt
 
+#############################
+####### CHANGE LOG ##########
+#############################
+
+changelog:
+  stage: changelog
+  extends: .infrastructure
+  rules:
+    - *build_rule
+  script: .gitlab-ci/scripts/changelog_generator.py
+              --token=${GITBOT_TOKEN}
+              --branch=${MASTER_BRANCH}
+              > changelog.md
+  artifacts:
+    expire_in: 4 weeks
+    paths:
+      - "changelog.md"
+
 #############################
 ########## NOTIFY ###########
 #############################
@@ -527,77 +531,108 @@ notify:
     - send_notify
   allow_failure: true
   rules:
-    # Trigger deploy jobs only on a schedule basis (for weekly release)
-    - if: $CI_PIPELINE_SOURCE == "schedule"
-    - if: $CI_COMMIT_TAG
+    - *schedule_tag_rule
   script:
      - |      
        echo -e "The job results for all boards are listed below (also available by clicking on the **Job Report** button)." >> Job_message.txt
        echo -e "The paths to all packages can be accessed by clicking on the **Link Report** button." >> Job_message.txt
+       echo -e "The changelog can be accessed by clicking on the **Changelog** button." >> Job_message.txt
        echo -e "*We kindly ask developers to check job results so that in case of failures a fix to eventual bugs can be promptly made.*" >> Job_message.txt
 
-       cat Job_message.txt Job_message_*.txt > Job_message_ARM_imx_$(date +%Y%m%d).txt
-       cat Job_report_*.txt > Job_report_ARM_imx_$(date +%Y%m%d).txt
-       cat Link_report_*.txt > Link_report_ARM_imx_$(date +%Y%m%d).txt
+       if [ -n "$CI_COMMIT_TAG" ]; then
+         tag=${CI_COMMIT_TAG}
+       else
+         tag=$(date +%Y%m%d)
+       fi
 
-       sed -i -E 's/\"//g' Link_report_ARM_imx_$(date +%Y%m%d).txt
+       cat Job_message.txt Job_message_*.txt > Job_message_edgehog_${tag}.txt
+       cat Job_report_*.txt > Job_report_edgehog_${tag}.txt
+       cat Link_report_*.txt > Link_report_edgehog_${tag}.txt
+       cat changelog.md > Changelog_edgehog_${tag}.md
+
+       sed -i -E 's/\"//g' Link_report_edgehog_${tag}.txt
 
        az storage blob upload --account-name $AZURE_STORAGE_ACCOUNT \
                               --sas-token $AZURE_STORAGE_SAS_TOKEN \
                               --container-name $AZURE_CONTAINER_NAME \
-                              --file Job_report_ARM_imx_$(date +%Y%m%d).txt \
-                              --name Job_report_ARM_imx_$(date +%Y%m%d).txt \
+                              --file Job_report_edgehog_${tag}.txt \
+                              --name Build_reports/Job_report_edgehog_${tag}.txt \
                               --overwrite
        
        az storage blob upload --account-name $AZURE_STORAGE_ACCOUNT \
                               --sas-token $AZURE_STORAGE_SAS_TOKEN \
                               --container-name $AZURE_CONTAINER_NAME \
-                              --file Link_report_ARM_imx_$(date +%Y%m%d).txt \
-                              --name Link_report_ARM_imx_$(date +%Y%m%d).txt \
+                              --file Link_report_edgehog_${tag}.txt \
+                              --name Build_reports/Link_report_edgehog_${tag}.txt \
+                              --overwrite
+
+       az storage blob upload --account-name $AZURE_STORAGE_ACCOUNT \
+                              --sas-token $AZURE_STORAGE_SAS_TOKEN \
+                              --container-name $AZURE_CONTAINER_NAME \
+                              --file Changelog_edgehog_${tag}.md \
+                              --name Build_reports/Changelog_edgehog_${tag}.md \
                               --overwrite
 
        cp .gitlab-ci/notification.json .
-       job_report="$(az storage blob url --container-name $AZURE_CONTAINER_NAME --name Job_report_ARM_imx_$(date +%Y%m%d).txt)"
+       job_report="$(az storage blob url --container-name $AZURE_CONTAINER_NAME --name Build_reports/Job_report_edgehog_${tag}.txt)"
        job_report="$(echo $job_report | sed -E 's/\s.*$//')"
        jq --arg job_var $job_report '.potentialAction[0].targets[0].uri = $job_var' notification.json > notification_1.json
 
-       link_report="$(az storage blob url --container-name $AZURE_CONTAINER_NAME --name Link_report_ARM_imx_$(date +%Y%m%d).txt)"
+       link_report="$(az storage blob url --container-name $AZURE_CONTAINER_NAME --name Build_reports/Link_report_edgehog_${tag}.txt)"
        link_report="$(echo $link_report | sed -E 's/\s.*$//')"
        jq --arg link_var $link_report '.potentialAction[1].targets[0].uri = $link_var' notification_1.json > notification_2.json
-       sed -i -E 's/\\"//g' notification_2.json
-
-       jq --arg text "$(cat Job_message_ARM_imx_$(date +%Y%m%d).txt)" '.text = $text' notification_2.json > notification_3.json
-       sed -i -E 's/\\n/ \\n\\n /g' notification_3.json
-
-       curl -X POST -H "Content-Type: application/json" --data @notification_3.json $WEBHOOK
-
-  # For some reason we cannot use a variable for the timeout, the CI linter reports
-  # 'jobs:build:merge_request:timeout config should be a duration' then.
-  # Hence we use GitLab's 'reference' syntax instead.
-  # timeout: !reference [variables, BUILD_TIMEOUT]
-  # script:
-  #   - cd ${CI_PROJECT_DIR}
-  #   # Get pipeline for merge request
-  #   - MR_PIPELINE=$(.gitlab-ci/scripts/get_pipelines.py
-  #       --gitlab-url=${CI_SERVER_URL}
-  #       --token=${GITBOT_TOKEN}
-  #       --project=${CI_PROJECT_PATH}
-  #       --commit=${CI_COMMIT_SHA}
-  #       --ref=^${MASTER_BRANCH} || true | head -1)
-  #   # If pipeline exists, mirror its result
-  #   - if [ ! -z "${MR_PIPELINE}" ]; then
-  #       .gitlab-ci/scripts/mirror_pipeline_result.py
-  #         --gitlab-url=${CI_SERVER_URL}
-  #         --token=${GITBOT_TOKEN}
-  #         --project=${CI_PROJECT_PATH}
-  #         --pipeline=${MR_PIPELINE}
-  #   # If no pipeline found, trigger a new one on the master
-  #   - else
-  #       .gitlab-ci/scripts/trigger_pipeline.py
-  #         --gitlab-url=${CI_SERVER_URL}
-  #         --token=${GITBOT_TOKEN}
-  #         --project=${CI_PROJECT_PATH}
-  #         --ref=${MASTER_BRANCH}
-  #   - fi
+
+       changelog_report="$(az storage blob url --container-name $AZURE_CONTAINER_NAME --name Build_reports/Changelog_edgehog_${tag}.txt)"
+       changelog_report="$(echo $changelog_report | sed -E 's/\s.*$//')"
+       jq --arg changelog_var $changelog_report '.potentialAction[2].targets[0].uri = $changelog_var' notification_2.json > notification_3.json
+       sed -i -E 's/\\"//g' notification_3.json
+
+       jq --arg text "$(cat Job_message_edgehog_${tag}.txt)" '.text = $text' notification_3.json > notification_4.json
+       sed -i -E 's/\\n/ \\n\\n /g' notification_4.json
+
+       curl -X POST -H "Content-Type: application/json" --data @notification_4.json $WEBHOOK
+
+       echo "${changelog_report#?}" > changelog_link.txt
+  
+  artifacts:
+    expire_in: 4 weeks
+    paths:
+      - changelog_link.txt
+
+#############################
+######### RELEASE ###########
+#############################
+
+release:
+  # Caution, as of 2021-02-02 these assets links require a login, see:
+  # https://gitlab.com/gitlab-org/gitlab/-/issues/299384
+  stage: release
+  image: registry.gitlab.com/gitlab-org/release-cli:latest
+  rules:
+    - if: $CI_COMMIT_TAG
+  script:
+    -  |
+        rm -rf release.sh
+        echo "#!/bin/bash" >> release.sh 
+        echo "" >> release.sh
+        command='release-cli create --name Release-'$CI_COMMIT_TAG' --tag-name '$CI_COMMIT_TAG' --description "changelog.md" --assets-link='"'"'['
+
+        while IFS= read -r line || [ -n "$line" ]; do
+              # Reading each line
+              if [[ ${line} == *https* ]]; then
+                url=$line
+                name=$(echo ${line} | sed 's/^.*seco_edgehog/seco_edgehog/')
+                command=${command}'{"name": "'${name}'", "url":"'${url}'"}, '
+              fi
+        done < Link_report.txt
+
+        url="$(cat changelog_link.txt)"
+        command=${command}'{"name": "changelog", "url":"'${url}'"}, '
+        command="${command%??}"
+        command=${command}']'"'"''
+        echo $command >> release.sh
+        cat release.sh
+        chmod +x release.sh
+        sh release.sh
 
 
diff --git a/notification.json b/notification.json
index 4ddb4f1..5cd4726 100644
--- a/notification.json
+++ b/notification.json
@@ -18,6 +18,13 @@
       "targets": [
         { "os": "default", "uri": "https://portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2Fe62f6e9e-3fe0-4f8b-a040-c32b0aeff5b9%2FresourceGroups%2FSECO_RD_Cloud_Services%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fsecostorage/path/secosoftware-public/etag/%220x8D98FAAF7268BD1%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container" }
       ]
+    },
+    {
+      "@type": "OpenUri",
+      "name": "Changelog",
+      "targets": [
+        { "os": "default", "uri": "https://portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2Fe62f6e9e-3fe0-4f8b-a040-c32b0aeff5b9%2FresourceGroups%2FSECO_RD_Cloud_Services%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fsecostorage/path/secosoftware-public/etag/%220x8D98FAAF7268BD1%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container" }
+      ]
     }
   ]
 }
-- 
GitLab