diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index e3db017d8..cbfc0ad7c 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -348,6 +348,10 @@ steps: - 'opensourceConfiguration' verbose: false timeout: 0 + pipelineExecute: + branch: 'master' + path: 'Jenkinsfile' + credentialsId: '' pipelineRestartSteps: sendMail: true timeoutInSeconds: 900 diff --git a/src/com/sap/piper/Utils.groovy b/src/com/sap/piper/Utils.groovy index 8eec025ce..af3e42fda 100644 --- a/src/com/sap/piper/Utils.groovy +++ b/src/com/sap/piper/Utils.groovy @@ -7,19 +7,6 @@ import groovy.text.SimpleTemplateEngine import java.nio.charset.StandardCharsets import java.security.MessageDigest -@NonCPS -def getMandatoryParameter(Map map, paramName, defaultValue = null) { - - def paramValue = map[paramName] - - if (paramValue == null) - paramValue = defaultValue - - if (paramValue == null) - throw new Exception("ERROR - NO VALUE AVAILABLE FOR ${paramName}") - return paramValue - -} def stash(name, include = '**/*.*', exclude = '', useDefaultExcludes = true) { echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})" diff --git a/test/groovy/PipelineExecuteTest.groovy b/test/groovy/PipelineExecuteTest.groovy index 5af6463f0..8b8419f8a 100644 --- a/test/groovy/PipelineExecuteTest.groovy +++ b/test/groovy/PipelineExecuteTest.groovy @@ -11,6 +11,7 @@ import util.JenkinsReadYamlRule import util.JenkinsStepRule class PipelineExecuteTest extends BasePiperTest { + private ExpectedException thrown = new ExpectedException().none() private JenkinsStepRule stepRule = new JenkinsStepRule(this) @@ -27,9 +28,6 @@ class PipelineExecuteTest extends BasePiperTest { @Before void init() { - pipelinePath = null - checkoutParameters.clear() - load = null helper.registerAllowedMethod('deleteDir', [], null) helper.registerAllowedMethod('checkout', [Map], { m -> @@ -44,17 +42,19 @@ class PipelineExecuteTest extends BasePiperTest { @Test void straightForwardTest() { + stepRule.step.pipelineExecute(repoUrl: "https://test.com/myRepo.git") + assert load == "Jenkinsfile" assert checkoutParameters.branch == 'master' assert checkoutParameters.repoUrl == "https://test.com/myRepo.git" assert checkoutParameters.credentialsId == '' assert checkoutParameters.path == 'Jenkinsfile' - } @Test void parameterizeTest() { + stepRule.step.pipelineExecute(repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', @@ -65,11 +65,11 @@ class PipelineExecuteTest extends BasePiperTest { assert checkoutParameters.repoUrl == "https://test.com/anotherRepo.git" assert checkoutParameters.credentialsId == 'abcd1234' assert checkoutParameters.path == 'path/to/Jenkinsfile' - } @Test void noRepoUrlTest() { + thrown.expect(Exception) thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR repoUrl") diff --git a/test/groovy/com/sap/piper/UtilsTest.groovy b/test/groovy/com/sap/piper/UtilsTest.groovy index 3ce51dbf7..cf98f9156 100644 --- a/test/groovy/com/sap/piper/UtilsTest.groovy +++ b/test/groovy/com/sap/piper/UtilsTest.groovy @@ -41,29 +41,6 @@ class UtilsTest extends BasePiperTest { } - @Test - void noValueGetMandatoryParameterTest() { - - thrown.expect(Exception) - thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR test") - - utils.getMandatoryParameter(parameters, 'test', null) - } - - @Test - void defaultValueGetMandatoryParameterTest() { - - assert utils.getMandatoryParameter(parameters, 'test', 'default') == 'default' - } - - @Test - void valueGetmandatoryParameterTest() { - - parameters.put('test', 'value') - - assert utils.getMandatoryParameter(parameters, 'test', null) == 'value' - } - @Test void testGenerateSHA1() { def result = utils.generateSha1('ContinuousDelivery') diff --git a/vars/pipelineExecute.groovy b/vars/pipelineExecute.groovy index 706ff4c0c..f0ba5bf0c 100644 --- a/vars/pipelineExecute.groovy +++ b/vars/pipelineExecute.groovy @@ -1,5 +1,5 @@ import com.sap.piper.GenerateDocumentation -import com.sap.piper.Utils +import com.sap.piper.ConfigurationHelper import groovy.transform.Field @@ -17,7 +17,11 @@ import groovy.transform.Field 'branch', /** The path to the Jenkinsfile, inside the repository, to be loaded.*/ 'path', - /** The Jenkins credentials containing user and password needed to access a private git repository.*/ + /** + * The Jenkins credentials containing user and password needed to access a private git repository. + * In case access to the repository containing the pipeline script is restricted the credentialsId of the credentials used for + * accessing the repository needs to be provided. The corresponding credentials needs to be configured in Jenkins accordingly. + */ 'credentialsId' ] @@ -34,38 +38,34 @@ void call(Map parameters = [:]) { node() { - def path + Map config handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters, failOnError: true) { - def utils = new Utils() + ConfigurationHelper configHelper = ConfigurationHelper.newInstance(this) + .loadStepDefaults() + .mixin(parameters, PARAMETER_KEYS) + .withMandatoryProperty('repoUrl') + .withMandatoryProperty('branch') + .withMandatoryProperty('path') + .withMandatoryProperty('credentialsId') - // The coordinates of the pipeline script - def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null) - def branch = utils.getMandatoryParameter(parameters, 'branch', 'master') - - path = utils.getMandatoryParameter(parameters, 'path', 'Jenkinsfile') - - // In case access to the repository containing the pipeline - // script is restricted the credentialsId of the credentials used for - // accessing the repository needs to be provided below. The corresponding - // credentials needs to be configured in Jenkins accordingly. - def credentialsId = utils.getMandatoryParameter(parameters, 'credentialsId', '') + config = configHelper.use() deleteDir() - checkout([$class: 'GitSCM', branches: [[name: branch]], - doGenerateSubmoduleConfigurations: false, - extensions: [[$class: 'SparseCheckoutPaths', - sparseCheckoutPaths: [[path: path]] + checkout([$class: 'GitSCM', branches: [[name: config.branch]], + doGenerateSubmoduleConfigurations: false, + extensions: [[$class: 'SparseCheckoutPaths', + sparseCheckoutPaths: [[path: config.path]] ]], - submoduleCfg: [], - userRemoteConfigs: [[credentialsId: credentialsId, - url: repo + submoduleCfg: [], + userRemoteConfigs: [[credentialsId: config.credentialsId, + url: config.repoUrl ]] ]) } - load path + load config.path } }