Skip to content

Commit

Permalink
Merge e9382be into 17e8390
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilhe committed Oct 16, 2018
2 parents 17e8390 + e9382be commit d9d081a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
8 changes: 6 additions & 2 deletions documentation/docs/steps/cloudFoundryDeploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ The following parameters can also be specified as step/stage/general parameters
## Example

```groovy
artifactSetVersion script: this, buildTool: 'maven'
cloudFoundryDeploy(
script: script,
deployType: 'blue-green',
cloudFoundry: target,
deployTool: 'cf_native'
)
```


24 changes: 24 additions & 0 deletions src/com/sap/piper/CfManifestUtils.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sap.piper

import com.cloudbees.groovy.cps.NonCPS

class CfManifestUtils {
@NonCPS
static Map transform(Map manifest) {
if (manifest.applications[0].buildpacks) {
manifest['applications'].each { Map application ->
def buildpacks = application['buildpacks']
if (buildpacks) {
if (buildpacks instanceof List) {
if (buildpacks.size > 1) {
throw new RuntimeException('More than one Cloud Foundry Buildpack is not supported. Please check your manifest.yaml file.')
}
application['buildpack'] = buildpacks[0]
}
application.remove('buildpacks')
}
}
}
return manifest
}
}
25 changes: 24 additions & 1 deletion test/groovy/CloudFoundryDeployTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class CloudFoundryDeployTest extends BasePiperTest {

@Test
void testCfNativeWithAppName() {
jryr.registerYaml('test.yml', "applications: [[name: 'manifestAppName']]")
helper.registerAllowedMethod('writeYaml', [Map], { Map parameters ->
generatedFile = parameters.file
data = parameters.data
})
jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -125,6 +130,11 @@ class CloudFoundryDeployTest extends BasePiperTest {

@Test
void testCfNativeWithAppNameCustomApi() {
jryr.registerYaml('test.yml', "applications: [[name: 'manifestAppName']]")
helper.registerAllowedMethod('writeYaml', [Map], { Map parameters ->
generatedFile = parameters.file
data = parameters.data
})
jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -142,6 +152,11 @@ class CloudFoundryDeployTest extends BasePiperTest {

@Test
void testCfNativeWithAppNameCompatible() {
jryr.registerYaml('test.yml', "applications: [[name: 'manifestAppName']]")
helper.registerAllowedMethod('writeYaml', [Map], { Map parameters ->
generatedFile = parameters.file
data = parameters.data
})
jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -165,7 +180,11 @@ class CloudFoundryDeployTest extends BasePiperTest {
@Test
void testCfNativeAppNameFromManifest() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })
jryr.registerYaml('test.yml', "[applications: [[name: 'manifestAppName']]]")
jryr.registerYaml('test.yml', "applications: [[name: 'manifestAppName']]")
helper.registerAllowedMethod('writeYaml', [Map], { Map parameters ->
generatedFile = parameters.file
data = parameters.data
})

jsr.step.cloudFoundryDeploy([
script: nullScript,
Expand All @@ -185,6 +204,10 @@ class CloudFoundryDeployTest extends BasePiperTest {
void testCfNativeWithoutAppName() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })
jryr.registerYaml('test.yml', "applications: [[]]")
helper.registerAllowedMethod('writeYaml', [Map], { Map parameters ->
generatedFile = parameters.file
data = parameters.data
})
thrown.expect(hudson.AbortException)
thrown.expectMessage('[cloudFoundryDeploy] ERROR: No appName available in manifest test.yml.')

Expand Down
30 changes: 30 additions & 0 deletions test/groovy/com/sap/piper/CfManifestUtilsTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sap.piper

import org.junit.Test

import static org.junit.Assert.*

class CfManifestUtilsTest {

@Test
void testManifestTransform() {
Map testFixture = [applications: [[buildpacks: ['sap_java_buildpack']]]]
Map expected = [applications: [[buildpack: 'sap_java_buildpack']]]
def actual = CfManifestUtils.transform(testFixture)
assertEquals(expected, actual)
}

@Test(expected = RuntimeException)
void testManifestTransformMultipleBuildpacks() {
Map testFixture = [applications: [[buildpacks: ['sap_java_buildpack', 'another_buildpack']]]]
CfManifestUtils.transform(testFixture)
}

@Test
void testManifestTransformShouldNotChange() {
Map testFixture = [applications: [[buildpack: 'sap_java_buildpack']]]
Map expected = [applications: [[buildpack: 'sap_java_buildpack']]]
def actual = CfManifestUtils.transform(testFixture)
assertEquals(expected, actual)
}
}
11 changes: 10 additions & 1 deletion vars/cloudFoundryDeploy.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import com.sap.piper.CfManifestUtils

import groovy.transform.Field

Expand Down Expand Up @@ -112,6 +113,7 @@ def deployCfNative (config) {
def deployCommand = 'push'
if (config.deployType == 'blue-green') {
deployCommand = 'blue-green-deploy'
handleLegacyCfManifest(config)
} else {
config.smokeTest = ''
}
Expand All @@ -129,7 +131,7 @@ def deployCfNative (config) {
}

sh """#!/bin/bash
set +x
set +x
export HOME=${config.dockerWorkspace}
cf login -u \"${username}\" -p '${password}' -a ${config.cloudFoundry.apiEndpoint} -o \"${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\"
cf plugins
Expand Down Expand Up @@ -166,3 +168,10 @@ def deployMta (config) {
sh "cf logout"
}
}

def handleLegacyCfManifest(config) {
def manifest = readYaml file: config.cloudFoundry.manifest
manifest = CfManifestUtils.transform(manifest)
sh "rm ${config.cloudFoundry.manifest}"
writeYaml file: config.cloudFoundry.manifest, data: manifest
}

0 comments on commit d9d081a

Please sign in to comment.