diff --git a/test/groovy/CloudFoundryDeployTest.groovy b/test/groovy/CloudFoundryDeployTest.groovy index e45cb522c..0bbd37afb 100644 --- a/test/groovy/CloudFoundryDeployTest.groovy +++ b/test/groovy/CloudFoundryDeployTest.groovy @@ -471,7 +471,7 @@ class CloudFoundryDeployTest extends BasePiperTest { // asserts assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerImage', 's4sdk/docker-cf-cli')) assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerWorkspace', '/home/piper')) - assertThat(shellRule.shell, hasItem(containsString('cf login -u test_cf -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))) + assertThat(shellRule.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))) assertThat(shellRule.shell, hasItem(containsString('cf deploy target/test.mtar -f'))) assertThat(shellRule.shell, hasItem(containsString('cf logout'))) } @@ -491,7 +491,7 @@ class CloudFoundryDeployTest extends BasePiperTest { mtaPath: 'target/test.mtar' ]) - assertThat(shellRule.shell, hasItem(stringContainsInOrder(["cf login -u test_cf", 'cf bg-deploy', '-f', '--no-confirm']))) + assertThat(shellRule.shell, hasItem(stringContainsInOrder(["cf login -u \"test_cf\"", 'cf bg-deploy', '-f', '--no-confirm']))) } @Test diff --git a/vars/cloudFoundryDeploy.groovy b/vars/cloudFoundryDeploy.groovy index 6d31d3aca..5b5126e16 100644 --- a/vars/cloudFoundryDeploy.groovy +++ b/vars/cloudFoundryDeploy.groovy @@ -269,46 +269,11 @@ def deployMta (config) { } } - withCredentials([usernamePassword( - credentialsId: config.cloudFoundry.credentialsId, - passwordVariable: 'password', - usernameVariable: 'username' - )]) { - echo "[${STEP_NAME}] Deploying MTA (${config.mtaPath}) with following parameters: ${config.mtaExtensionDescriptor} ${config.mtaDeployParameters}" - def cfTraceFile = 'cf.log' - def deployScript = """#!/bin/bash - export HOME=${config.dockerWorkspace} - export CF_TRACE="${cfTraceFile}" - set +x - set -e - cf api ${config.cloudFoundry.apiEndpoint} ${config.apiParameters} - cf login -u ${username} -p '${password}' -a ${config.cloudFoundry.apiEndpoint} -o \"${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\" ${config.loginParameters} - cf plugins - cf ${deployCommand} ${config.mtaPath} ${config.mtaDeployParameters} ${config.mtaExtensionDescriptor}""" + def deployStatement = "cf ${deployCommand} ${config.mtaPath} ${config.mtaDeployParameters} ${config.mtaExtensionDescriptor}" + def apiStatement = "cf api ${config.cloudFoundry.apiEndpoint} ${config.apiParameters}" - if(config.verbose) { - // Password contained in output below is hidden by withCredentials - echo "[INFO][$STEP_NAME] Executing deploy command '${deployScript}'" - } - - def returnCode = sh returnStatus: true, script: deployScript - - if(config.verbose || returnCode != 0) { - if(fileExists(file: cfTraceFile)) { - echo '### START OF CF CLI TRACE OUTPUT ###' - // Would be nice to inline the two next lines, but that is not understood by the test framework - def cfTrace = readFile(file: cfTraceFile) - echo cfTrace - echo '### END OF CF CLI TRACE OUTPUT ###' - } else { - echo "No trace file found at '${cfTraceFile}'" - } - } - if(returnCode != 0){ - error "[${STEP_NAME}] ERROR: The execution of the deploy command failed, see the log for details." - } - sh "cf logout" - } + echo "[${STEP_NAME}] Deploying MTA (${config.mtaPath}) with following parameters: ${config.mtaExtensionDescriptor} ${config.mtaDeployParameters}" + deploy(apiStatement, deployStatement, config, null) } private void handleCFNativeDeployment(Map config, script) { @@ -435,6 +400,12 @@ private checkIfAppNameIsAvailable(config) { } def deployCfNative (config) { + def deployStatement = "cf ${config.deployCommand} ${config.cloudFoundry.appName ?: ''} ${config.deployOptions?:''} -f '${config.cloudFoundry.manifest}' ${config.smokeTest} ${config.cfNativeDeployParameters}" + deploy(null, deployStatement, config, { c -> stopOldAppIfRunning(c) }) +} + +private deploy(def cfApiStatement, def cfDeployStatement, def config, Closure postDeployAction) { + withCredentials([usernamePassword( credentialsId: config.cloudFoundry.credentialsId, passwordVariable: 'password', @@ -448,16 +419,19 @@ def deployCfNative (config) { set -e export HOME=${config.dockerWorkspace} export CF_TRACE=${cfTraceFile} + ${cfApiStatement ?: ''} cf login -u \"${username}\" -p '${password}' -a ${config.cloudFoundry.apiEndpoint} -o \"${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\" ${config.loginParameters} cf plugins - cf ${config.deployCommand} ${config.cloudFoundry.appName ?: ''} ${config.deployOptions?:''} -f '${config.cloudFoundry.manifest}' ${config.smokeTest} ${config.cfNativeDeployParameters} + ${cfDeployStatement} """ if(config.verbose) { // Password contained in output below is hidden by withCredentials echo "[INFO][${STEP_NAME}] Executing command: '${deployScript}'." } + def returnCode = sh returnStatus: true, script: deployScript + if(config.verbose || returnCode != 0) { if(fileExists(file: cfTraceFile)) { echo '### START OF CF CLI TRACE OUTPUT ###' @@ -473,7 +447,9 @@ def deployCfNative (config) { if(returnCode != 0){ error "[${STEP_NAME}] ERROR: The execution of the deploy command failed, see the log for details." } - stopOldAppIfRunning(config) + + if(postDeployAction) postDeployAction(config) + sh "cf logout" } }