From d3aaf7c19212400ac325ee8888af88e061e2e9b9 Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@orange.com>
Date: Wed, 12 May 2021 11:21:28 +0200
Subject: [PATCH] feat: integrate semantic-release info to helm-package job

---
 README.md                    | 10 ++++++++++
 kicker.json                  |  5 +++++
 templates/gitlab-ci-helm.yml | 21 +++++++++++----------
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index d9cd68e..0629323 100644
--- a/README.md
+++ b/README.md
@@ -235,6 +235,16 @@ This job [packages your chart into an archive](https://helm.sh/docs/helm/helm_pa
 | --------------------- | ---------------------------------------- | ----------------- |
 | `HELM_PACKAGE_ARGS`   | The Helm [command with options](https://helm.sh/docs/helm/helm_package/) to perform the packaging (_without dynamic arguments such as the chart path_)   | `package --dependency-update` |
 
+| `HELM_SEMREL_RELEASE_DISABLED`   | Set to disable usage of semrel release info for helm package  | _none_ (enabled) |
+
+#### `semantic-release` integration
+
+If you activate the [`semantic-release-info` job from the `semantic-release` template](https://gitlab.com/Orange-OpenSource/tbc/semantic-release/#semantic-release-info-job), the `mvn-release` job will automatically use the generated next version info for both application version (`--app-version`) and chart version (`--version`).
+
+If no next version info is generated by semantic-release, the package will be created either, but without versionning info.
+
+Note: You can disable the `semantic-release` integration (as it's the `helm-package`job that will perform the release) with the `HELM_SEMREL_RELEASE_DISABLED` variable.
+
 ### `helm-publish` job
 
 This job uses the following variables:
diff --git a/kicker.json b/kicker.json
index ba02f1c..6f171dd 100644
--- a/kicker.json
+++ b/kicker.json
@@ -140,6 +140,11 @@
           "description": "The Helm [command with options](https://helm.sh/docs/helm/helm_package/) to perform the packaging (_without dynamic arguments such as the chart path_)",
           "default": "package --dependency-update",
           "advanced": true
+        },
+        {
+          "name": "HELM_SEMREL_RELEASE_DISABLED",
+          "description": "Disable semantic-release integration",
+          "advanced": true
         }
       ]
     },
diff --git a/templates/gitlab-ci-helm.yml b/templates/gitlab-ci-helm.yml
index 839106c..b85eef6 100644
--- a/templates/gitlab-ci-helm.yml
+++ b/templates/gitlab-ci-helm.yml
@@ -282,11 +282,9 @@ stages:
       helm_namespace_opt="$helm_namespace_opt --namespace $namespace"
     fi
 
-    if [ -f "$CI_PROJECT_DIR/.package_name.txt" ]; then
-      package=$(cat "$CI_PROJECT_DIR/.package_name.txt")
-    elif [ -n "$HELM_DEPLOY_CHART" ]; then
-      package="$HELM_DEPLOY_CHART"
-    else
+    package=$(ls -1 ./helm_packages/*.tgz 2>/dev/null || echo "")
+    package=${package:-$HELM_DEPLOY_CHART}
+    if [ -z "${package}" ]; then
       log_error "No Chart to deploy! Please use \\e[32m\$HELM_DEPLOY_CHART\\e[0m to deploy a chart from a repository"
       log_error "Or check the provided variables to package your own chart!"
       exit 1
@@ -557,9 +555,13 @@ helm-package:
     - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
     - add_helm_repositories
   script:
-    - output=$(helm $HELM_PACKAGE_ARGS $HELM_CHART_DIR --destination $CI_PROJECT_DIR) && echo $output
-    - mv "$(echo $output | grep -oe "[^ ]*$")" $CI_PROJECT_DIR/
-    - echo $output | grep -oe "[^/]*$" > $CI_PROJECT_DIR/.package_name.txt
+    - |
+      if [[ "$SEMREL_INFO_ON" ]] && [[ "$SEMREL_INFO_NEXT_VERSION" ]] && [[ -z "$HELM_SEMREL_RELEASE_DISABLED" ]]
+      then
+        log_info "semantic-release info is activated, using computed next version for release: \\e[1;94m${SEMREL_INFO_NEXT_VERSION}\\e[0m"
+        helm_version_opts="--app-version ${SEMREL_INFO_NEXT_VERSION} --version ${SEMREL_INFO_NEXT_VERSION}"
+      fi
+    - helm $HELM_PACKAGE_ARGS ${TRACE+--debug} $helm_version_opts $HELM_CHART_DIR --destination helm_packages
   rules:
     - if: $CI_MERGE_REQUEST_ID
       when: never
@@ -568,8 +570,7 @@ helm-package:
   artifacts:
     expire_in: 1 week
     paths:
-      - $CI_PROJECT_DIR/*.tgz
-      - $CI_PROJECT_DIR/.package_name.txt
+      - helm_packages/
 
 # ==================================================
 # Stage: publish
-- 
GitLab