2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
2018-07-30 09:28:24 +02:00
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
2018-10-17 11:01:09 +02:00
import com.sap.piper.CfManifestUtils
2018-07-30 09:28:24 +02:00
import groovy.transform.Field
@Field String STEP_NAME = 'cloudFoundryDeploy'
2018-10-25 14:24:17 +02:00
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
2018-07-30 09:28:24 +02:00
@Field Set STEP_CONFIG_KEYS = [
'cloudFoundry' ,
'deployUser' ,
'deployTool' ,
'deployType' ,
'dockerImage' ,
'dockerWorkspace' ,
'mtaDeployParameters' ,
'mtaExtensionDescriptor' ,
'mtaPath' ,
'smokeTestScript' ,
'smokeTestStatusCode' ,
'stashContent' ]
@Field Map CONFIG_KEY_COMPATIBILITY = [ cloudFoundry: [ apiEndpoint: 'cfApiEndpoint' , appName: 'cfAppName' , credentialsId: 'cfCredentialsId' , manifest: 'cfManifest' , org: 'cfOrg' , space: 'cfSpace' ] ]
2018-10-25 14:24:17 +02:00
2018-07-30 09:28:24 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2018-08-30 16:33:07 +02:00
void call ( Map parameters = [ : ] ) {
2018-07-30 09:28:24 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
def utils = parameters . juStabUtils
if ( utils = = null ) {
utils = new Utils ( )
}
2018-09-21 16:55:31 +02:00
def script = checkScript ( this , parameters )
2018-07-30 09:28:24 +02:00
if ( script = = null )
2018-10-31 09:40:12 +02:00
script = this
2018-07-30 09:28:24 +02:00
2018-10-17 11:05:20 +02:00
Map config = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-10-25 14:24:17 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS , CONFIG_KEY_COMPATIBILITY )
2018-09-06 16:45:30 +02:00
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS , CONFIG_KEY_COMPATIBILITY )
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS , CONFIG_KEY_COMPATIBILITY )
. mixin ( parameters , PARAMETER_KEYS , CONFIG_KEY_COMPATIBILITY )
2018-07-30 09:28:24 +02:00
. dependingOn ( 'deployTool' ) . mixin ( 'dockerImage' )
. dependingOn ( 'deployTool' ) . mixin ( 'dockerWorkspace' )
. withMandatoryProperty ( 'cloudFoundry/org' )
. withMandatoryProperty ( 'cloudFoundry/space' )
. withMandatoryProperty ( 'cloudFoundry/credentialsId' )
. use ( )
2018-10-30 17:22:42 +02:00
utils . pushToSWA ( [ step: STEP_NAME , stepParam1: config . deployTool , stepParam2: config . deployType , stepParam3: parameters ? . script = = null ] , config )
2018-08-09 11:35:33 +02:00
2018-07-30 09:28:24 +02:00
echo "[${STEP_NAME}] General parameters: deployTool=${config.deployTool}, deployType=${config.deployType}, cfApiEndpoint=${config.cloudFoundry.apiEndpoint}, cfOrg=${config.cloudFoundry.org}, cfSpace=${config.cloudFoundry.space}, cfCredentialsId=${config.cloudFoundry.credentialsId}, deployUser=${config.deployUser}"
2018-08-15 09:46:08 +02:00
config . stashContent = utils . unstashAll ( config . stashContent )
2018-07-30 09:28:24 +02:00
if ( config . deployTool = = 'mtaDeployPlugin' ) {
// set default mtar path
2018-10-17 11:37:40 +02:00
config = ConfigurationHelper . newInstance ( this , config )
2018-07-30 09:28:24 +02:00
. addIfEmpty ( 'mtaPath' , config . mtaPath ? : findMtar ( ) )
. use ( )
dockerExecute ( dockerImage: config . dockerImage , dockerWorkspace: config . dockerWorkspace , stashContent: config . stashContent ) {
deployMta ( config )
}
return
}
if ( config . deployTool = = 'cf_native' ) {
config . smokeTest = ''
2018-07-30 12:06:35 +02:00
if ( config . smokeTestScript = = 'blueGreenCheckScript.sh' ) {
2018-07-30 09:28:24 +02:00
writeFile file: config . smokeTestScript , text: libraryResource ( config . smokeTestScript )
}
2018-08-15 09:46:08 +02:00
2018-07-30 09:28:24 +02:00
config . smokeTest = '--smoke-test $(pwd)/' + config . smokeTestScript
sh "chmod +x ${config.smokeTestScript}"
echo "[${STEP_NAME}] CF native deployment (${config.deployType}) with cfAppName=${config.cloudFoundry.appName}, cfManifest=${config.cloudFoundry.manifest}, smokeTestScript=${config.smokeTestScript}"
dockerExecute (
dockerImage: config . dockerImage ,
dockerWorkspace: config . dockerWorkspace ,
stashContent: config . stashContent ,
dockerEnvVars: [ CF_HOME: "${config.dockerWorkspace}" , CF_PLUGIN_HOME: "${config.dockerWorkspace}" , STATUS_CODE: "${config.smokeTestStatusCode}" ]
) {
deployCfNative ( config )
}
return
}
}
}
def findMtar ( ) {
def mtarPath = ''
def mtarFiles = findFiles ( glob: '**/target/*.mtar' )
if ( mtarFiles . length > 1 ) {
error 'Found multiple *.mtar files, please specify file via mtaPath parameter! ${mtarFiles}'
}
if ( mtarFiles . length = = 1 ) {
return mtarFiles [ 0 ] . path
}
error 'No *.mtar file found!'
}
def deployCfNative ( config ) {
withCredentials ( [ usernamePassword (
credentialsId: config . cloudFoundry . credentialsId ,
passwordVariable: 'password' ,
usernameVariable: 'username'
) ] ) {
def deployCommand = 'push'
if ( config . deployType = = 'blue-green' ) {
deployCommand = 'blue-green-deploy'
2018-10-17 11:01:09 +02:00
handleLegacyCfManifest ( config )
2018-07-30 09:28:24 +02:00
} else {
config . smokeTest = ''
}
// check if appName is available
if ( config . cloudFoundry . appName = = null | | config . cloudFoundry . appName = = '' ) {
2018-11-07 11:39:30 +02:00
if ( config . deployType = = 'blue-green' ) {
error "[${STEP_NAME}] ERROR: Blue-green plugin requires app name to be passed (see https://github.com/bluemixgaragelondon/cf-blue-green-deploy/issues/27)"
}
2018-07-30 09:28:24 +02:00
if ( fileExists ( config . cloudFoundry . manifest ) ) {
def manifest = readYaml file: config . cloudFoundry . manifest
if ( ! manifest | | ! manifest . applications | | ! manifest . applications [ 0 ] . name )
error "[${STEP_NAME}] ERROR: No appName available in manifest ${config.cloudFoundry.manifest}."
} else {
error "[${STEP_NAME}] ERROR: No manifest file ${config.cloudFoundry.manifest} found."
}
}
sh "" " # ! /bin/ bash
2018-10-17 11:01:09 +02:00
set + x
2018-07-30 09:28:24 +02:00
export HOME = $ { config . dockerWorkspace }
cf login - u \ "${username}\" -p '${password}' -a ${config.cloudFoundry.apiEndpoint} -o \"${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\"
cf plugins
2018-11-07 11:39:30 +02:00
cf $ { deployCommand } $ { config . cloudFoundry . appName ? : '' } $ { config . deployType = = 'blue-green' ? '--delete-old-apps' : '' } - f '${config.cloudFoundry.manifest}' $ { config . smokeTest } "" "
2018-07-30 09:28:24 +02:00
sh "cf logout"
}
}
def deployMta ( config ) {
if ( config . mtaExtensionDescriptor = = null ) config . mtaExtensionDescriptor = ''
if ( ! config . mtaExtensionDescriptor . isEmpty ( ) & & ! config . mtaExtensionDescriptor . startsWith ( '-e ' ) ) config . mtaExtensionDescriptor = "-e ${config.mtaExtensionDescriptor}"
def deployCommand = 'deploy'
if ( config . deployType = = 'blue-green' )
deployCommand = 'bg-deploy'
withCredentials ( [ usernamePassword (
credentialsId: config . cloudFoundry . credentialsId ,
passwordVariable: 'password' ,
usernameVariable: 'username'
) ] ) {
echo "[${STEP_NAME}] Deploying MTA (${config.mtaPath}) with following parameters: ${config.mtaExtensionDescriptor} ${config.mtaDeployParameters}"
sh "" " # ! /bin/ bash
export HOME = $ { config . dockerWorkspace }
set + x
cf api $ { config . cloudFoundry . apiEndpoint }
cf login - u $ { username } - p '${password}' - a $ { config . cloudFoundry . apiEndpoint } - o \ "${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\"
cf plugins
cf $ { deployCommand } $ { config . mtaPath } $ { config . mtaDeployParameters } $ { config . mtaExtensionDescriptor } "" "
sh "cf logout"
}
}
2018-10-17 11:01:09 +02:00
def handleLegacyCfManifest ( config ) {
def manifest = readYaml file: config . cloudFoundry . manifest
String originalManifest = manifest . toString ( )
manifest = CfManifestUtils . transform ( manifest )
String transformedManifest = manifest . toString ( )
if ( originalManifest ! = transformedManifest ) {
echo "" " The file $ { config . cloudFoundry . manifest } is not compatible with the Cloud Foundry blue - green deployment plugin . Re - writing inline .
See this issue if you are interested in the background: https: //github.com/cloudfoundry/cli/issues/1445.\n
Original manifest file content: $originalManifest \ n
Transformed manifest file content: $transformedManifest "" "
sh "rm ${config.cloudFoundry.manifest}"
writeYaml file: config . cloudFoundry . manifest , data: manifest
}
}