From cf089c2c485a2e64b9331b7ea4a91a2c8820d01f Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Thu, 28 Nov 2024 11:52:00 +0100 Subject: [PATCH] OZG-6923 add commitId to feature branch image tag/helm version --- Jenkinsfile | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index ce7f7286..ee1b86a8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,8 +10,8 @@ pipeline { SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/ FAILED_STAGE = "" SH_SUCCESS_STATUS_CODE = 0 - IMAGE_TAG = generateImageTag() - HELM_CHART_VERSION = generateHelmChartVersion() + IMAGE_TAG = buildVersionName() + HELM_CHART_VERSION = buildVersionName() BUILD_PROFILE = getBuildProfile() } @@ -109,7 +109,7 @@ pipeline { stage ('Deploy SBOM to DependencyTrack') { steps { script { - IMAGE_TAG = generateImageTag() + IMAGE_TAG = buildVersionName() configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) { withCredentials([string(credentialsId: 'dependency-track-api-key', variable: 'API_KEY')]) { @@ -141,7 +141,7 @@ pipeline { steps { script { FAILED_STAGE=env.STAGE_NAME - HELM_CHART_VERSION = generateHelmChartVersion() + HELM_CHART_VERSION = buildVersionName() dir('src/main/helm') { sh "helm lint -f ../../test/helm-linter-values.yaml" @@ -224,8 +224,22 @@ String getHelmRepository(){ return 'ozg-base-apps-snapshot'; } -String generateImageTag() { - return "${env.BRANCH_NAME}-${getPomVersion()}" +String validateBranchName(branchName) { + int maxLength = 30 + if (branchName.length() > maxLength) { + String originalBranchName = branchName + branchName = branchName.substring(0, maxLength) + echo "WARNING: Branch name '${originalBranchName}' exceeded ${maxLength} characters. " + + "It has been truncated to '${branchName}' for deployment purposes." + } + return branchName +} + +String buildVersionName() { + if (isReleaseBranch()) { + return getPomVersion() + } + return "${getPomVersion()}-${validateBranchName(env.BRANCH_NAME)}-${env.GIT_COMMIT.take(7)}".replaceAll("_", "-") } String getPomVersion() { @@ -347,16 +361,3 @@ Boolean isMasterBranch() { Boolean isReleaseBranch() { return env.BRANCH_NAME == 'release' } - -String generateHelmChartVersion() { - def chartVersion = getPomVersion() - - if (isMasterBranch()) { - chartVersion += "-${env.GIT_COMMIT.take(7)}" - } - else if (!isReleaseBranch()) { - chartVersion += "-${env.BRANCH_NAME}" - } - - return chartVersion.replaceAll("_", "-").take(63 - "administration-".length()) -} -- GitLab