1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00

[refactoring] condence common coding for cf deploy (#895)

* [refactoring] condence common coding for cf deploy

Small change beyond refactoring: for mtaDeploy the user is now quoted.

* more general name: logoutAction -> postDeployAction
This commit is contained in:
Marcus Holl 2019-10-22 13:53:08 +02:00 committed by GitHub
parent 514755e4ef
commit 8e987c46e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 43 deletions

View File

@ -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

View File

@ -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"
}
}