You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Skip checkout flag (#2594)
* Skip checkout flag Defaults to null==false We need the option to provide an scmInfo object by the user * Check skipCheckout is Boolean * Require scmInfo map in case checkout is skipped
This commit is contained in:
		| @@ -8,6 +8,7 @@ import org.junit.rules.ExpectedException | ||||
| import org.junit.rules.RuleChain | ||||
| import util.* | ||||
|  | ||||
| import static org.hamcrest.Matchers.hasItem | ||||
| import static org.hamcrest.Matchers.hasItems | ||||
| import static org.hamcrest.Matchers.hasKey | ||||
| import static org.hamcrest.Matchers.is | ||||
| @@ -228,4 +229,49 @@ class PiperPipelineStageInitTest extends BasePiperTest { | ||||
|         assertThat(stepParams.setupCommonPipelineEnvironment?.customDefaultsFromFiles, is(['my-custom-default-file.yml'])) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void "Parameter skipCheckout skips the checkout call"() { | ||||
|         jsr.step.piperPipelineStageInit( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             buildTool: 'maven', | ||||
|             stashSettings: 'com.sap.piper/pipeline/stashSettings.yml', | ||||
|             skipCheckout: true, | ||||
|             scmInfo: ["dummyScmKey":"dummyScmKey"] | ||||
|         ) | ||||
|  | ||||
|         assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'artifactPrepareVersion', 'pipelineStashFilesBeforeBuild')) | ||||
|         assertThat(stepsCalled, not(hasItem('checkout'))) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void "Try to skip checkout with parameter skipCheckout not boolean throws error"() { | ||||
|         thrown.expectMessage('[piperPipelineStageInit] Parameter skipCheckout has to be of type boolean. Instead got \'java.lang.String\'') | ||||
|  | ||||
|         jsr.step.piperPipelineStageInit( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             buildTool: 'maven', | ||||
|             stashSettings: 'com.sap.piper/pipeline/stashSettings.yml', | ||||
|             skipCheckout: "false" | ||||
|         ) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void "Try to skip checkout without scmInfo parameter throws error"() { | ||||
|         thrown.expectMessage('[piperPipelineStageInit] Need am scmInfo map retrieved from a checkout. ' + | ||||
|             'If you want to skip the checkout the scm info needs to be provided by you with parameter scmInfo, ' + | ||||
|             'for example as follows:\n' + | ||||
|             '  def scmInfo = checkout scm\n' + | ||||
|             '  piperPipelineStageInit script:this, skipCheckout: true, scmInfo: scmInfo') | ||||
|  | ||||
|         jsr.step.piperPipelineStageInit( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             buildTool: 'maven', | ||||
|             stashSettings: 'com.sap.piper/pipeline/stashSettings.yml', | ||||
|             skipCheckout: true | ||||
|         ) | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -69,6 +69,16 @@ import static com.sap.piper.Prerequisites.checkScript | ||||
|      * Example: `[$class: 'GitSCM', branches: [[name: <branch_to_be_cloned>]], userRemoteConfigs: [[credentialsId: <credential_to_access_repository>, url: <repository_url>]]]`. | ||||
|      */ | ||||
|     'checkoutMap', | ||||
|     /** | ||||
|      * The map returned from a Jenkins git checkout. Used to set the git information in the | ||||
|      * common pipeline environment. | ||||
|      */ | ||||
|     'scmInfo', | ||||
|     /** | ||||
|      * Optional skip of checkout if checkout was done before this step already. | ||||
|      * @possibleValues `true`, `false` | ||||
|      */ | ||||
|     'skipCheckout', | ||||
|     /** | ||||
|      * Optional path to the pipeline configuration file defining project specific settings. | ||||
|      */ | ||||
| @@ -106,7 +116,21 @@ void call(Map parameters = [:]) { | ||||
|     def stageName = StageNameProvider.instance.getStageName(script, parameters, this) | ||||
|  | ||||
|     piperStageWrapper (script: script, stageName: stageName, stashContent: [], ordinal: 1, telemetryDisabled: true) { | ||||
|         def scmInfo = checkout(parameters.checkoutMap ?: scm) | ||||
|         def skipCheckout = parameters.skipCheckout | ||||
|         if (skipCheckout != null && !(skipCheckout instanceof Boolean)) { | ||||
|             error "[${STEP_NAME}] Parameter skipCheckout has to be of type boolean. Instead got '${skipCheckout.class.getName()}'" | ||||
|         } | ||||
|         def scmInfo = parameters.scmInfo | ||||
|         if (skipCheckout && !scmInfo) { | ||||
|             error "[${STEP_NAME}] Need am scmInfo map retrieved from a checkout. " + | ||||
|                 "If you want to skip the checkout the scm info needs to be provided by you with parameter scmInfo, " + | ||||
|                 "for example as follows:\n" + | ||||
|                 "  def scmInfo = checkout scm\n" + | ||||
|                 "  piperPipelineStageInit script:this, skipCheckout: true, scmInfo: scmInfo" | ||||
|         } | ||||
|         if (!skipCheckout) { | ||||
|             scmInfo = checkout(parameters.checkoutMap ?: scm) | ||||
|         } | ||||
|  | ||||
|         setupCommonPipelineEnvironment(script: script, customDefaults: parameters.customDefaults, scmInfo: scmInfo, | ||||
|             configFile: parameters.configFile, customDefaultsFromFiles: parameters.customDefaultsFromFiles) | ||||
| @@ -139,7 +163,7 @@ void call(Map parameters = [:]) { | ||||
|             ContainerMap.instance.initFromResource(script, config.containerMapResource, buildTool) | ||||
|         } | ||||
|  | ||||
|         initStashConfiguration(script, config.stashSettings, config.verbose?: false) | ||||
|         initStashConfiguration(script, config.stashSettings, config.verbose ?: false) | ||||
|  | ||||
|         if (config.verbose) { | ||||
|             echo "piper-lib-os  configuration: ${script.commonPipelineEnvironment.configuration}" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user