From f5b19a79e464683e6d735be4c6c6fbd2c2c6d66c Mon Sep 17 00:00:00 2001 From: Daniel Kurzynski Date: Mon, 5 Oct 2020 19:34:43 +0200 Subject: [PATCH] Infer project name (#2119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stephan Aßmus --- src/com/sap/piper/Utils.groovy | 3 ++- vars/piperPipelineStageInit.groovy | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/com/sap/piper/Utils.groovy b/src/com/sap/piper/Utils.groovy index d348bea08..727e5dc59 100644 --- a/src/com/sap/piper/Utils.groovy +++ b/src/com/sap/piper/Utils.groovy @@ -30,7 +30,8 @@ def stashList(script, List stashes) { def exclude = stash.excludes if (stash?.merge == true) { - String lockName = "${script.commonPipelineEnvironment.configuration.stashFiles}/${stash.name}" + String lockingResourceGroup = script.commonPipelineEnvironment.projectName?:env.JOB_NAME + String lockName = "${lockingResourceGroup}/${stash.name}" lock(lockName) { unstash stash.name echo "Stash content: ${name} (include: ${include}, exclude: ${exclude})" diff --git a/vars/piperPipelineStageInit.groovy b/vars/piperPipelineStageInit.groovy index 03d56df90..39c9d20e6 100644 --- a/vars/piperPipelineStageInit.groovy +++ b/vars/piperPipelineStageInit.groovy @@ -28,6 +28,10 @@ import static com.sap.piper.Prerequisites.checkScript * If this is set to true, it is not required to provide the `buildTool` parameter in the `general` section of the pipeline configuration. */ 'inferBuildTool', + /** + * Enables automatic inference from the build descriptor in case projectName is not configured. + */ + 'inferProjectName', /** * Toggle for initialization of the stash settings for Cloud SDK Pipeline. * If this is set to true, the stashSettings parameter is **not** configurable. @@ -41,6 +45,10 @@ import static com.sap.piper.Prerequisites.checkScript * Defines the main branch for your pipeline. **Typically this is the `master` branch, which does not need to be set explicitly.** Only change this in exceptional cases */ 'productiveBranch', + /** + * Name of the project, e.g. used for the name of lockable resources. + */ + 'projectName', /** * Defines the library resource containing stage/step initialization settings. Those define conditions when certain steps/stages will be activated. **Caution: changing the default will break the standard behavior of the pipeline - thus only relevant when including `Init` stage into custom pipelines!** */ @@ -122,6 +130,12 @@ void call(Map parameters = [:]) { String buildTool = checkBuildTool(config) + script.commonPipelineEnvironment.projectName = config.projectName + + if (!script.commonPipelineEnvironment.projectName && config.inferProjectName) { + script.commonPipelineEnvironment.projectName = inferProjectName(script, buildTool) + } + if (Boolean.valueOf(env.ON_K8S) && config.containerMapResource) { ContainerMap.instance.initFromResource(script, config.containerMapResource, buildTool) } @@ -193,6 +207,22 @@ void call(Map parameters = [:]) { } } +private String inferProjectName(Script script, String buildTool) { + switch (buildTool) { + case 'maven': + def pom = script.readMavenPom file: 'pom.xml' + return "${pom.groupId}-${pom.artifactId}" + case 'npm': + Map packageJson = script.readJSON file: 'package.json' + return packageJson.name + case 'mta': + Map mta = script.readYaml file: 'mta.yaml' + return mta.ID + } + + script.error "Cannot infer projectName. Project buildTool was none of the expected ones 'mta', 'maven', or 'npm'." +} + private String checkBuildTool(config) { def buildDescriptorPattern = '' String buildTool = config.buildTool