From 98d81c6d6ede04844e8d444145358b92effa8a5c Mon Sep 17 00:00:00 2001 From: Christoph Szymanski Date: Fri, 1 Dec 2017 17:30:52 +0100 Subject: [PATCH 01/10] Create .travis.yml --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..c2bdaecb0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +language: groovy +sudo: false From 5d5a82929c3c6f65def884e93efe79629f752836 Mon Sep 17 00:00:00 2001 From: Oliver Feldmann Date: Fri, 24 Nov 2017 12:33:26 +0100 Subject: [PATCH 02/10] centralPipelineLoad step A more convenient way to checkout the project sources. The idea is to have a Jenkinsfile in the payload repository that only loads the shared library and then runs this step. This step in turn loads a Jenkinsfile from another repository. The repo url, branch, Jenkinsfile path and credentials ID can be provided to the step. The Jenkinsfile is first checked out to a temporary folder, before it is loaded. The payload repo must include a Jenkinsfile to be loaded with this approach. --- vars/centralPipelineLoad.groovy | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 vars/centralPipelineLoad.groovy diff --git a/vars/centralPipelineLoad.groovy b/vars/centralPipelineLoad.groovy new file mode 100644 index 000000000..029773ce3 --- /dev/null +++ b/vars/centralPipelineLoad.groovy @@ -0,0 +1,45 @@ +import com.sap.piper.Utils + +/** + * centralPipelineLoad + * Load a central pipeline. + * + */ +def call(Map parameters = [:]) { + + handlePipelineStepErrors (stepName: 'centralPipelineLoad', stepParameters: parameters) { + + def utils = new Utils() + + // The coordinates of the central pipeline script + def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null) + def branch = utils.getMandatoryParameter(parameters, 'branch', 'master') + def path = utils.getMandatoryParameter(parameters, 'jenkinsfilePath', 'Jenkinsfile') + + // In case access to the repository containing the central 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', '') + + // temporary folder used for storing the central jenkins file locally. + def temporaryPipelineFolder = "pipeline-${UUID.randomUUID().toString()}" + + deleteDir() + dir(temporaryPipelineFolder) { + + checkout([$class: 'GitSCM', branches: [[name: branch]], + doGenerateSubmoduleConfigurations: false, + extensions: [[$class: 'SparseCheckoutPaths', + sparseCheckoutPaths: [[path: path]] + ]], + submoduleCfg: [], + userRemoteConfigs: [[credentialsId: credentialsId, + url: repo + ]] + ]) + } + + load "${temporaryPipelineFolder}/${path}" + } +} From a6df3b3053f0ab223e2d18c484afc218a3c46518 Mon Sep 17 00:00:00 2001 From: Oliver Feldmann Date: Fri, 24 Nov 2017 13:59:40 +0100 Subject: [PATCH 03/10] 'centralPipelineLoad' unit tests Mocking with LesFurets. Check that everything is at the right place. 1. Test with only the mandatory parameter 'repoUrl'. 1. Test setting all available parameters. 1. Test with no parameters throws Exception for missing mandatory parameter 'repoUrl'. --- test/groovy/CentralPipelineLoadTest.groovy | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test/groovy/CentralPipelineLoadTest.groovy diff --git a/test/groovy/CentralPipelineLoadTest.groovy b/test/groovy/CentralPipelineLoadTest.groovy new file mode 100644 index 000000000..7b4d92158 --- /dev/null +++ b/test/groovy/CentralPipelineLoadTest.groovy @@ -0,0 +1,125 @@ +import hudson.AbortException +import org.junit.rules.TemporaryFolder +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.ExpectedException + +class CentralPipelineLoadTest extends PiperTestBase { + + @Rule + public ExpectedException thrown = new ExpectedException().none() + + def pipelinePath + def checkoutParameters = [:] + def load + + @Before + void setUp() { + + super.setUp() + + pipelinePath = null + checkoutParameters.clear() + load = null + + helper.registerAllowedMethod('deleteDir', [], null) + helper.registerAllowedMethod("dir", [String, Closure], { + dirName, c -> + pipelinePath = dirName + c() + }) + helper.registerAllowedMethod('checkout', [Map], { m -> + checkoutParameters.branch = m.branches[0].name + checkoutParameters.repoUrl = m.userRemoteConfigs[0].url + checkoutParameters.credentialsId = m.userRemoteConfigs[0].credentialsId + checkoutParameters.path = m.extensions[0].sparseCheckoutPaths[0].path + }) + helper.registerAllowedMethod('load', [String], { s -> load = s }) + + } + + + @Test + void straightForwardTest() { + + withPipeline(defaultPipeline()).execute() + assert load == "${pipelinePath}/Jenkinsfile" + assert checkoutParameters.branch == 'master' + assert checkoutParameters.repoUrl == "https://test.com/myRepo.git" + assert checkoutParameters.credentialsId == '' + assert checkoutParameters.path == 'Jenkinsfile' + + } + + @Test + void parameterizeTest() { + + withPipeline(parameterizePipeline()).execute() + assert load == "${pipelinePath}/path/to/Jenkinsfile" + assert checkoutParameters.branch == 'feature' + 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") + + withPipeline(noRepoUrlPipeline()).execute() + + } + + + private defaultPipeline() { + return """ + @Library('piper-library-os') + + execute() { + + node() { + centralPipelineLoad repoUrl: "https://test.com/myRepo.git" + } + + } + + return this + """ + } + + private parameterizePipeline() { + return """ + @Library('piper-library-os') + + execute() { + + node() { + centralPipelineLoad repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', jenkinsfilePath: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' + } + + } + + return this + """ + } + + private noRepoUrlPipeline() { + return """ + @Library('piper-library-os') + + execute() { + + node() { + centralPipelineLoad() + } + + } + + return this + """ + } +} From b4d17fa929525bc1dafcd2443f847aa214a3fb82 Mon Sep 17 00:00:00 2001 From: Oliver Feldmann Date: Fri, 24 Nov 2017 14:49:20 +0100 Subject: [PATCH 04/10] Documentation for 'centralPipelineLoad' --- .../docs/steps/centralPipelineLoad.md | 41 +++++++++++++++++++ documentation/mkdocs.yml | 1 + 2 files changed, 42 insertions(+) create mode 100644 documentation/docs/steps/centralPipelineLoad.md diff --git a/documentation/docs/steps/centralPipelineLoad.md b/documentation/docs/steps/centralPipelineLoad.md new file mode 100644 index 000000000..73b5518f6 --- /dev/null +++ b/documentation/docs/steps/centralPipelineLoad.md @@ -0,0 +1,41 @@ +# centralPipelineLoad + +## Description +Loads a pipeline from a git repository. The idea is to set up a pipeline job in Jenkins that loads a minimal pipeline, which in turn loads the shared library and then uses this step to load the actual pipeline. + +## Prerequisites + +none + +## Parameters + +| parameter | mandatory | default | possible values | +| -------------------|-----------|-----------------|-----------------| +| `repoUrl` | yes | | | +| `branch` | no | 'master' | | +| `jenkinsfilePath` | no | 'Jenkinsfile' | | +| `credentialsId` | no | An empty String | | + +* `repoUrl` The url to the git repository of the pipeline to be loaded. +* `branch` The branch of the git repository from which the pipeline should be checked out. +* `jenkinsfilePath` The path to the Jenkinsfile, inside the repository, to be loaded. +* `credentialsId` The Jenkins credentials containing user and password needed to access a private git repository. + +## Return value + +none + +## Side effects + +The Jenkinsfile is checked out to a temporary folder in the Jenkins workspace. This folder starts with 'pipeline-' followed by a random UUID. + +## Exceptions + +* `Exception` + * If `repoUrl` is not provided. + +## Example + +```groovy +centralPipelineLoad repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', jenkinsfilePath: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' +``` diff --git a/documentation/mkdocs.yml b/documentation/mkdocs.yml index aeafed2de..714723ba8 100644 --- a/documentation/mkdocs.yml +++ b/documentation/mkdocs.yml @@ -4,6 +4,7 @@ pages: - 'Library steps': - commonPipelineEnvironment: steps/commonPipelineEnvironment.md - handlePipelineStepErrors: steps/handlePipelineStepErrors.md + - centralPipelineLoad: steps/centralPipelineLoad.md - toolValidate: steps/toolValidate.md - mtaBuild: steps/mtaBuild.md - neoDeploy: steps/neoDeploy.md From 13139da01e50935541ac3b06d595247bcd46c3b1 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 28 Nov 2017 09:34:54 +0100 Subject: [PATCH 05/10] Use a dedicated node() instead of a temporary directory. --- documentation/docs/steps/centralPipelineLoad.md | 2 +- test/groovy/CentralPipelineLoadTest.groovy | 9 ++------- vars/centralPipelineLoad.groovy | 11 ++++------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/documentation/docs/steps/centralPipelineLoad.md b/documentation/docs/steps/centralPipelineLoad.md index 73b5518f6..4cb1d4899 100644 --- a/documentation/docs/steps/centralPipelineLoad.md +++ b/documentation/docs/steps/centralPipelineLoad.md @@ -27,7 +27,7 @@ none ## Side effects -The Jenkinsfile is checked out to a temporary folder in the Jenkins workspace. This folder starts with 'pipeline-' followed by a random UUID. +none ## Exceptions diff --git a/test/groovy/CentralPipelineLoadTest.groovy b/test/groovy/CentralPipelineLoadTest.groovy index 7b4d92158..bf1ed31a4 100644 --- a/test/groovy/CentralPipelineLoadTest.groovy +++ b/test/groovy/CentralPipelineLoadTest.groovy @@ -24,11 +24,6 @@ class CentralPipelineLoadTest extends PiperTestBase { load = null helper.registerAllowedMethod('deleteDir', [], null) - helper.registerAllowedMethod("dir", [String, Closure], { - dirName, c -> - pipelinePath = dirName - c() - }) helper.registerAllowedMethod('checkout', [Map], { m -> checkoutParameters.branch = m.branches[0].name checkoutParameters.repoUrl = m.userRemoteConfigs[0].url @@ -44,7 +39,7 @@ class CentralPipelineLoadTest extends PiperTestBase { void straightForwardTest() { withPipeline(defaultPipeline()).execute() - assert load == "${pipelinePath}/Jenkinsfile" + assert load == "Jenkinsfile" assert checkoutParameters.branch == 'master' assert checkoutParameters.repoUrl == "https://test.com/myRepo.git" assert checkoutParameters.credentialsId == '' @@ -56,7 +51,7 @@ class CentralPipelineLoadTest extends PiperTestBase { void parameterizeTest() { withPipeline(parameterizePipeline()).execute() - assert load == "${pipelinePath}/path/to/Jenkinsfile" + assert load == "path/to/Jenkinsfile" assert checkoutParameters.branch == 'feature' assert checkoutParameters.repoUrl == "https://test.com/anotherRepo.git" assert checkoutParameters.credentialsId == 'abcd1234' diff --git a/vars/centralPipelineLoad.groovy b/vars/centralPipelineLoad.groovy index 029773ce3..a139ea08b 100644 --- a/vars/centralPipelineLoad.groovy +++ b/vars/centralPipelineLoad.groovy @@ -22,11 +22,8 @@ def call(Map parameters = [:]) { // credentials needs to be configured in Jenkins accordingly. def credentialsId = utils.getMandatoryParameter(parameters, 'credentialsId', '') - // temporary folder used for storing the central jenkins file locally. - def temporaryPipelineFolder = "pipeline-${UUID.randomUUID().toString()}" - - deleteDir() - dir(temporaryPipelineFolder) { + node() { + deleteDir() checkout([$class: 'GitSCM', branches: [[name: branch]], doGenerateSubmoduleConfigurations: false, @@ -38,8 +35,8 @@ def call(Map parameters = [:]) { url: repo ]] ]) - } - load "${temporaryPipelineFolder}/${path}" + load path + } } } From 67630d829bbb014b892ab0a65221270d47950bf3 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 29 Nov 2017 08:45:48 +0100 Subject: [PATCH 06/10] change naming of parameter holding the path to the pipeline script. --- documentation/docs/steps/centralPipelineLoad.md | 6 +++--- test/groovy/CentralPipelineLoadTest.groovy | 2 +- vars/centralPipelineLoad.groovy | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/documentation/docs/steps/centralPipelineLoad.md b/documentation/docs/steps/centralPipelineLoad.md index 4cb1d4899..57b7407a9 100644 --- a/documentation/docs/steps/centralPipelineLoad.md +++ b/documentation/docs/steps/centralPipelineLoad.md @@ -13,12 +13,12 @@ none | -------------------|-----------|-----------------|-----------------| | `repoUrl` | yes | | | | `branch` | no | 'master' | | -| `jenkinsfilePath` | no | 'Jenkinsfile' | | +| `path` | no | 'Jenkinsfile' | | | `credentialsId` | no | An empty String | | * `repoUrl` The url to the git repository of the pipeline to be loaded. * `branch` The branch of the git repository from which the pipeline should be checked out. -* `jenkinsfilePath` The path to the Jenkinsfile, inside the repository, to be loaded. +* `path` The path to the Jenkinsfile, inside the repository, to be loaded. * `credentialsId` The Jenkins credentials containing user and password needed to access a private git repository. ## Return value @@ -37,5 +37,5 @@ none ## Example ```groovy -centralPipelineLoad repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', jenkinsfilePath: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' +centralPipelineLoad repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', path: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' ``` diff --git a/test/groovy/CentralPipelineLoadTest.groovy b/test/groovy/CentralPipelineLoadTest.groovy index bf1ed31a4..382e0565f 100644 --- a/test/groovy/CentralPipelineLoadTest.groovy +++ b/test/groovy/CentralPipelineLoadTest.groovy @@ -93,7 +93,7 @@ class CentralPipelineLoadTest extends PiperTestBase { execute() { node() { - centralPipelineLoad repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', jenkinsfilePath: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' + centralPipelineLoad repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' } } diff --git a/vars/centralPipelineLoad.groovy b/vars/centralPipelineLoad.groovy index a139ea08b..bdad47f83 100644 --- a/vars/centralPipelineLoad.groovy +++ b/vars/centralPipelineLoad.groovy @@ -14,7 +14,7 @@ def call(Map parameters = [:]) { // The coordinates of the central pipeline script def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null) def branch = utils.getMandatoryParameter(parameters, 'branch', 'master') - def path = utils.getMandatoryParameter(parameters, 'jenkinsfilePath', 'Jenkinsfile') + def path = utils.getMandatoryParameter(parameters, 'path', 'Jenkinsfile') // In case access to the repository containing the central pipeline // script is restricted the credentialsId of the credentials used for From bbe799fd192c16a5f267a864e6972a04d4f4e794 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 29 Nov 2017 12:26:47 +0100 Subject: [PATCH 07/10] Rename centralPipelineLoad step to externalPipelineExecute --- ...ntralPipelineLoad.md => externalPipelineExecute.md} | 4 ++-- documentation/mkdocs.yml | 2 +- ...dTest.groovy => ExternalPipelineExecuteTest.groovy} | 8 ++++---- ...elineLoad.groovy => externalPipelineExecute.groovy} | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) rename documentation/docs/steps/{centralPipelineLoad.md => externalPipelineExecute.md} (86%) rename test/groovy/{CentralPipelineLoadTest.groovy => ExternalPipelineExecuteTest.groovy} (88%) rename vars/{centralPipelineLoad.groovy => externalPipelineExecute.groovy} (80%) diff --git a/documentation/docs/steps/centralPipelineLoad.md b/documentation/docs/steps/externalPipelineExecute.md similarity index 86% rename from documentation/docs/steps/centralPipelineLoad.md rename to documentation/docs/steps/externalPipelineExecute.md index 57b7407a9..d2a4e4493 100644 --- a/documentation/docs/steps/centralPipelineLoad.md +++ b/documentation/docs/steps/externalPipelineExecute.md @@ -1,4 +1,4 @@ -# centralPipelineLoad +# externalPipelineExecute ## Description Loads a pipeline from a git repository. The idea is to set up a pipeline job in Jenkins that loads a minimal pipeline, which in turn loads the shared library and then uses this step to load the actual pipeline. @@ -37,5 +37,5 @@ none ## Example ```groovy -centralPipelineLoad repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', path: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' +externalPipelineExecute repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', path: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' ``` diff --git a/documentation/mkdocs.yml b/documentation/mkdocs.yml index 714723ba8..5556d6ed5 100644 --- a/documentation/mkdocs.yml +++ b/documentation/mkdocs.yml @@ -4,7 +4,7 @@ pages: - 'Library steps': - commonPipelineEnvironment: steps/commonPipelineEnvironment.md - handlePipelineStepErrors: steps/handlePipelineStepErrors.md - - centralPipelineLoad: steps/centralPipelineLoad.md + - externalPipelineExecute: steps/externalPipelineExecute.md - toolValidate: steps/toolValidate.md - mtaBuild: steps/mtaBuild.md - neoDeploy: steps/neoDeploy.md diff --git a/test/groovy/CentralPipelineLoadTest.groovy b/test/groovy/ExternalPipelineExecuteTest.groovy similarity index 88% rename from test/groovy/CentralPipelineLoadTest.groovy rename to test/groovy/ExternalPipelineExecuteTest.groovy index 382e0565f..639c40104 100644 --- a/test/groovy/CentralPipelineLoadTest.groovy +++ b/test/groovy/ExternalPipelineExecuteTest.groovy @@ -5,7 +5,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException -class CentralPipelineLoadTest extends PiperTestBase { +class ExternalPipelineExecuteTest extends PiperTestBase { @Rule public ExpectedException thrown = new ExpectedException().none() @@ -77,7 +77,7 @@ class CentralPipelineLoadTest extends PiperTestBase { execute() { node() { - centralPipelineLoad repoUrl: "https://test.com/myRepo.git" + externalPipelineExecute repoUrl: "https://test.com/myRepo.git" } } @@ -93,7 +93,7 @@ class CentralPipelineLoadTest extends PiperTestBase { execute() { node() { - centralPipelineLoad repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' + externalPipelineExecute repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' } } @@ -109,7 +109,7 @@ class CentralPipelineLoadTest extends PiperTestBase { execute() { node() { - centralPipelineLoad() + externalPipelineExecute() } } diff --git a/vars/centralPipelineLoad.groovy b/vars/externalPipelineExecute.groovy similarity index 80% rename from vars/centralPipelineLoad.groovy rename to vars/externalPipelineExecute.groovy index bdad47f83..45706f3d5 100644 --- a/vars/centralPipelineLoad.groovy +++ b/vars/externalPipelineExecute.groovy @@ -1,22 +1,22 @@ import com.sap.piper.Utils /** - * centralPipelineLoad - * Load a central pipeline. + * externalPipelineExecute + * Load and executes a pipeline from another git repository. * */ def call(Map parameters = [:]) { - handlePipelineStepErrors (stepName: 'centralPipelineLoad', stepParameters: parameters) { + handlePipelineStepErrors (stepName: 'externalPipelineExecute', stepParameters: parameters) { def utils = new Utils() - // The coordinates of the central pipeline script + // The coordinates of the pipeline script def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null) def branch = utils.getMandatoryParameter(parameters, 'branch', 'master') def path = utils.getMandatoryParameter(parameters, 'path', 'Jenkinsfile') - // In case access to the repository containing the central pipeline + // 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. From 88ac3ae43e9135f9afbd639b76f77a996fb9dba4 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 1 Dec 2017 10:51:11 +0100 Subject: [PATCH 08/10] Rename externalPipelineExecute to pipelineExecute --- .../{externalPipelineExecute.md => pipelineExecute.md} | 4 ++-- documentation/mkdocs.yml | 2 +- ...elineExecuteTest.groovy => PipelineExecuteTest.groovy} | 8 ++++---- ...ernalPipelineExecute.groovy => pipelineExecute.groovy} | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) rename documentation/docs/steps/{externalPipelineExecute.md => pipelineExecute.md} (86%) rename test/groovy/{ExternalPipelineExecuteTest.groovy => PipelineExecuteTest.groovy} (88%) rename vars/{externalPipelineExecute.groovy => pipelineExecute.groovy} (92%) diff --git a/documentation/docs/steps/externalPipelineExecute.md b/documentation/docs/steps/pipelineExecute.md similarity index 86% rename from documentation/docs/steps/externalPipelineExecute.md rename to documentation/docs/steps/pipelineExecute.md index d2a4e4493..4859f4809 100644 --- a/documentation/docs/steps/externalPipelineExecute.md +++ b/documentation/docs/steps/pipelineExecute.md @@ -1,4 +1,4 @@ -# externalPipelineExecute +# pipelineExecute ## Description Loads a pipeline from a git repository. The idea is to set up a pipeline job in Jenkins that loads a minimal pipeline, which in turn loads the shared library and then uses this step to load the actual pipeline. @@ -37,5 +37,5 @@ none ## Example ```groovy -externalPipelineExecute repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', path: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' +pipelineExecute repoUrl: "https://github.com/MyOrg/MyPipelineRepo.git", branch: 'feature1', path: 'path/to/Jenkinsfile', credentialsId: 'MY_REPO_CREDENTIALS' ``` diff --git a/documentation/mkdocs.yml b/documentation/mkdocs.yml index 5556d6ed5..f94ac36a9 100644 --- a/documentation/mkdocs.yml +++ b/documentation/mkdocs.yml @@ -4,7 +4,7 @@ pages: - 'Library steps': - commonPipelineEnvironment: steps/commonPipelineEnvironment.md - handlePipelineStepErrors: steps/handlePipelineStepErrors.md - - externalPipelineExecute: steps/externalPipelineExecute.md + - pipelineExecute: steps/pipelineExecute.md - toolValidate: steps/toolValidate.md - mtaBuild: steps/mtaBuild.md - neoDeploy: steps/neoDeploy.md diff --git a/test/groovy/ExternalPipelineExecuteTest.groovy b/test/groovy/PipelineExecuteTest.groovy similarity index 88% rename from test/groovy/ExternalPipelineExecuteTest.groovy rename to test/groovy/PipelineExecuteTest.groovy index 639c40104..4452c9373 100644 --- a/test/groovy/ExternalPipelineExecuteTest.groovy +++ b/test/groovy/PipelineExecuteTest.groovy @@ -5,7 +5,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException -class ExternalPipelineExecuteTest extends PiperTestBase { +class PipelineExecuteTest extends PiperTestBase { @Rule public ExpectedException thrown = new ExpectedException().none() @@ -77,7 +77,7 @@ class ExternalPipelineExecuteTest extends PiperTestBase { execute() { node() { - externalPipelineExecute repoUrl: "https://test.com/myRepo.git" + pipelineExecute repoUrl: "https://test.com/myRepo.git" } } @@ -93,7 +93,7 @@ class ExternalPipelineExecuteTest extends PiperTestBase { execute() { node() { - externalPipelineExecute repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' + pipelineExecute repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234' } } @@ -109,7 +109,7 @@ class ExternalPipelineExecuteTest extends PiperTestBase { execute() { node() { - externalPipelineExecute() + pipelineExecute() } } diff --git a/vars/externalPipelineExecute.groovy b/vars/pipelineExecute.groovy similarity index 92% rename from vars/externalPipelineExecute.groovy rename to vars/pipelineExecute.groovy index 45706f3d5..f710f7b8e 100644 --- a/vars/externalPipelineExecute.groovy +++ b/vars/pipelineExecute.groovy @@ -1,13 +1,13 @@ import com.sap.piper.Utils /** - * externalPipelineExecute + * pipelineExecute * Load and executes a pipeline from another git repository. * */ def call(Map parameters = [:]) { - handlePipelineStepErrors (stepName: 'externalPipelineExecute', stepParameters: parameters) { + handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters) { def utils = new Utils() From 15a7e8e3f8c73c6e3ac75c8f842adc04b1831e8f Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 1 Dec 2017 15:34:25 +0100 Subject: [PATCH 09/10] Explain the use case for pipelineExecute --- documentation/docs/steps/pipelineExecute.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/documentation/docs/steps/pipelineExecute.md b/documentation/docs/steps/pipelineExecute.md index 4859f4809..7e6067c9a 100644 --- a/documentation/docs/steps/pipelineExecute.md +++ b/documentation/docs/steps/pipelineExecute.md @@ -3,6 +3,10 @@ ## Description Loads a pipeline from a git repository. The idea is to set up a pipeline job in Jenkins that loads a minimal pipeline, which in turn loads the shared library and then uses this step to load the actual pipeline. +A centrally maintained pipeline script (Jenkinsfile) can be re-used by +several projects using `pipelineExecute` as outlined in the example +below. + ## Prerequisites none From 36837ad3b87d759ae9cf8894683d7f0d46ceb933 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 1 Dec 2017 17:00:30 +0100 Subject: [PATCH 10/10] Prevent pointing to pipelineExecute in case of an failure inside foreign pipeline In case there is an error in another pipeline step inside a foreign pipeline we got nevertheless an error explaining there is an issue inside the pipeline load step. --- vars/pipelineExecute.groovy | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/vars/pipelineExecute.groovy b/vars/pipelineExecute.groovy index f710f7b8e..f0209bd32 100644 --- a/vars/pipelineExecute.groovy +++ b/vars/pipelineExecute.groovy @@ -7,22 +7,26 @@ import com.sap.piper.Utils */ def call(Map parameters = [:]) { - handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters) { + node() { - def utils = new Utils() + def path - // The coordinates of the pipeline script - def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null) - def branch = utils.getMandatoryParameter(parameters, 'branch', 'master') - def path = utils.getMandatoryParameter(parameters, 'path', 'Jenkinsfile') + handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters) { - // 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', '') + def utils = new Utils() + + // 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', '') - node() { deleteDir() checkout([$class: 'GitSCM', branches: [[name: branch]], @@ -36,7 +40,7 @@ def call(Map parameters = [:]) { ]] ]) - load path } + load path } }