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 
			
		
		
		
	dockerExecute: handle stashContent (#332)
* handle stashed content * add utils
This commit is contained in:
		
				
					committed by
					
						 Oliver Nocon
						Oliver Nocon
					
				
			
			
				
	
			
			
			
						parent
						
							5ce3adf401
						
					
				
				
					commit
					aa5ad1c0d1
				
			| @@ -137,7 +137,10 @@ steps: | ||||
|     mtaDeployPlugin: | ||||
|       dockerImage: 's4sdk/docker-cf-cli' | ||||
|       dockerWorkspace: '/home/piper' | ||||
|   dockerExecute: | ||||
|     stashContent: [] | ||||
|   dockerExecuteOnKubernetes: | ||||
|     stashContent: [] | ||||
|     stashIncludes: | ||||
|       workspace: '**/*.*' | ||||
|     stashExcludes: | ||||
|   | ||||
| @@ -87,7 +87,9 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest { | ||||
|  | ||||
|     @Test | ||||
|     void testRunOnPodNoContainerMapOnlyDockerImage() throws Exception { | ||||
|         jsr.step.dockerExecuteOnKubernetes(script: nullScript, | ||||
|         jsr.step.dockerExecuteOnKubernetes( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             dockerImage: 'maven:3.5-jdk-8-alpine', | ||||
|             dockerOptions: '-it', | ||||
|             dockerVolumeBind: ['my_vol': '/my_vol'], | ||||
| @@ -139,7 +141,9 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest { | ||||
|     @Test | ||||
|     void testDockerExecuteOnKubernetesWithCustomJnlpWithDockerImage() throws Exception { | ||||
|         nullScript.commonPipelineEnvironment.configuration = ['general': ['jenkinsKubernetes': ['jnlpAgent': 'myJnalpAgent']]] | ||||
|         jsr.step.dockerExecuteOnKubernetes(script: nullScript, | ||||
|         jsr.step.dockerExecuteOnKubernetes( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             dockerImage: 'maven:3.5-jdk-8-alpine') { | ||||
|             bodyExecuted = true | ||||
|         } | ||||
| @@ -195,7 +199,9 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest { | ||||
|     @Test | ||||
|     void testDockerExecuteOnKubernetesEmptyContainerMapNoDockerImage() throws Exception { | ||||
|         exception.expect(IllegalArgumentException.class); | ||||
|             jsr.step.dockerExecuteOnKubernetes(script: nullScript, | ||||
|             jsr.step.dockerExecuteOnKubernetes( | ||||
|                 script: nullScript, | ||||
|                 juStabUtils: utils, | ||||
|                 containerMap: [:], | ||||
|                 dockerEnvVars: ['customEnvKey': 'customEnvValue']) { | ||||
|                 container(name: 'jnlp') { | ||||
| @@ -214,6 +220,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest { | ||||
|         }) | ||||
|         jsr.step.dockerExecuteOnKubernetes( | ||||
|             script: nullScript, | ||||
|             juStabUtils: utils, | ||||
|             containerCommands: ['selenium/standalone-chrome': ''], | ||||
|             containerEnvVars: [ | ||||
|                 'selenium/standalone-chrome': ['customEnvKey': 'customEnvValue'] | ||||
|   | ||||
| @@ -1,7 +1,10 @@ | ||||
| import com.cloudbees.groovy.cps.NonCPS | ||||
|  | ||||
| import com.sap.piper.ConfigurationHelper | ||||
| import com.sap.piper.k8s.ContainerMap | ||||
| import com.sap.piper.JenkinsUtils | ||||
| import com.sap.piper.Utils | ||||
| import com.sap.piper.k8s.ContainerMap | ||||
|  | ||||
| import groovy.transform.Field | ||||
|  | ||||
| @Field def STEP_NAME = 'dockerExecute' | ||||
| @@ -22,7 +25,8 @@ import groovy.transform.Field | ||||
|     'sidecarName', | ||||
|     'sidecarOptions', | ||||
|     'sidecarWorkspace', | ||||
|     'sidecarVolumeBind' | ||||
|     'sidecarVolumeBind', | ||||
|     'stashContent' | ||||
| ] | ||||
| @Field Set STEP_CONFIG_KEYS = PARAMETER_KEYS | ||||
|  | ||||
| @@ -31,6 +35,7 @@ void call(Map parameters = [:], body) { | ||||
|         final script = parameters.script | ||||
|         if (script == null) | ||||
|             script = [commonPipelineEnvironment: commonPipelineEnvironment] | ||||
|         def utils = parameters?.juStabUtils ?: new Utils() | ||||
|         Map config = ConfigurationHelper.newInstance(this) | ||||
|             .loadStepDefaults() | ||||
|             .mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS) | ||||
| @@ -51,7 +56,8 @@ void call(Map parameters = [:], body) { | ||||
|                         script: script, | ||||
|                         dockerImage: config.dockerImage, | ||||
|                         dockerEnvVars: config.dockerEnvVars, | ||||
|                         dockerWorkspace: config.dockerWorkspace | ||||
|                         dockerWorkspace: config.dockerWorkspace, | ||||
|                         stashContent: config.stashContent | ||||
|                     ){ | ||||
|                         echo "[INFO][${STEP_NAME}] Executing inside a Kubernetes Pod" | ||||
|                         body() | ||||
| @@ -64,7 +70,8 @@ void call(Map parameters = [:], body) { | ||||
|                         containerMap: [:], | ||||
|                         containerName: config.dockerName, | ||||
|                         containerPortMappings: [:], | ||||
|                         containerWorkspaces: [:] | ||||
|                         containerWorkspaces: [:], | ||||
|                         stashContent: config.stashContent | ||||
|                     ] | ||||
|                     paramMap.containerCommands[config.sidecarImage] = '' | ||||
|  | ||||
| @@ -104,6 +111,7 @@ void call(Map parameters = [:], body) { | ||||
|                 executeInsideDocker = false | ||||
|             } | ||||
|             if (executeInsideDocker && config.dockerImage) { | ||||
|                 utils.unstashAll(config.stashContent) | ||||
|                 def image = docker.image(config.dockerImage) | ||||
|                 image.pull() | ||||
|                 if (!config.sidecarImage) { | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| import com.sap.piper.ConfigurationHelper | ||||
| import com.sap.piper.JenkinsUtils | ||||
| import com.sap.piper.Utils | ||||
| import com.sap.piper.k8s.SystemEnv | ||||
| import groovy.transform.Field | ||||
| import hudson.AbortException | ||||
| @@ -16,7 +17,8 @@ import hudson.AbortException | ||||
|     'containerWorkspaces', //specify workspace (=home directory of user) per container. If not provided dockerWorkspace will be used. If empty, home directory will not be set. | ||||
|     'dockerImage', | ||||
|     'dockerWorkspace', | ||||
|     'dockerEnvVars' | ||||
|     'dockerEnvVars', | ||||
|     'stashContent' | ||||
| ] | ||||
| @Field Set STEP_CONFIG_KEYS = PARAMETER_KEYS.plus(['stashIncludes', 'stashExcludes']) | ||||
|  | ||||
| @@ -28,6 +30,7 @@ void call(Map parameters = [:], body) { | ||||
|         final script = parameters.script | ||||
|         if (script == null) | ||||
|             script = [commonPipelineEnvironment: commonPipelineEnvironment] | ||||
|         def utils = parameters?.juStabUtils ?: new Utils() | ||||
|  | ||||
|         ConfigurationHelper configHelper = ConfigurationHelper.newInstance(this) | ||||
|             .loadStepDefaults() | ||||
| @@ -43,7 +46,7 @@ void call(Map parameters = [:], body) { | ||||
|             config.containerName = 'container-exec' | ||||
|             config.containerMap = ["${config.get('dockerImage')}": config.containerName] | ||||
|         } | ||||
|         executeOnPod(config, body) | ||||
|         executeOnPod(config, utils, body) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -53,7 +56,7 @@ def getOptions(config) { | ||||
|             containers: getContainerList(config)] | ||||
| } | ||||
|  | ||||
| void executeOnPod(Map config, Closure body) { | ||||
| void executeOnPod(Map config, utils, Closure body) { | ||||
|     /* | ||||
|      * There could be exceptions thrown by | ||||
|         - The podTemplate | ||||
| @@ -65,14 +68,15 @@ void executeOnPod(Map config, Closure body) { | ||||
|      * In case third case, we need to create the 'container' stash to bring the modified content back to the host. | ||||
|      */ | ||||
|     try { | ||||
|         if (config.containerName) | ||||
|             stashWorkspace(config, 'workspace') | ||||
|         if (config.containerName && config.stashContent.isEmpty()){ | ||||
|             config.stashContent.add(stashWorkspace(config, 'workspace')) | ||||
|         } | ||||
|         podTemplate(getOptions(config)) { | ||||
|             node(config.uniqueId) { | ||||
|                 if (config.containerName) { | ||||
|                     container(name: config.containerName){ | ||||
|                         try { | ||||
|                             unstashWorkspace(config, 'workspace') | ||||
|                             utils.unstashAll(config.stashContent) | ||||
|                             body() | ||||
|                         } finally { | ||||
|                             stashWorkspace(config, 'container') | ||||
| @@ -89,18 +93,21 @@ void executeOnPod(Map config, Closure body) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| private void stashWorkspace(config, prefix) { | ||||
| private String stashWorkspace(config, prefix) { | ||||
|     def stashName = "${prefix}-${config.uniqueId}" | ||||
|     try { | ||||
|         // Every dockerImage used in the dockerExecuteOnKubernetes should have user id 1000 | ||||
|         sh "chown -R 1000:1000 ." | ||||
|         stash( | ||||
|             name: "${prefix}-${config.uniqueId}", | ||||
|             name: stashName, | ||||
|             include: config.stashIncludes.workspace, | ||||
|             exclude: config.stashExcludes.excludes | ||||
|         ) | ||||
|         return stashName | ||||
|     } catch (AbortException | IOException e) { | ||||
|         echo "${e.getMessage()}" | ||||
|     } | ||||
|     return null | ||||
| } | ||||
|  | ||||
| private void unstashWorkspace(config, prefix) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user