Skip to content

Commit

Permalink
cnbBuild triggered by buildExecute (#4498)
Browse files Browse the repository at this point in the history
* Add cnbBuild to buildExecute

* Error message change

* Make if check simpler

Co-authored-by: Alexander Link <[email protected]>

* Switch order of check

---------

Co-authored-by: Linda Siebert <[email protected]>
Co-authored-by: Linda Siebert <[email protected]>
Co-authored-by: Alexander Link <[email protected]>
  • Loading branch information
4 people authored Sep 7, 2023
1 parent 9ba76d7 commit 9d27e0e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
88 changes: 88 additions & 0 deletions test/groovy/BuildExecuteTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,51 @@ class BuildExecuteTest extends BasePiperTest {
assertThat(buildToolCalled, is(true))
}

@Test
void testDockerWithoutCNB() {
boolean kanikoExecuteCalled = false
boolean cnbBuildCalled = false
binding.setVariable('docker', new DockerMock('test'))
def pushParams = [:]
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
kanikoExecuteCalled = true
return
})
helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
cnbBuildCalled = true
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'docker',
)
assertThat(cnbBuildCalled, is(false))
assertThat(kanikoExecuteCalled, is(true))
}

@Test
void testDockerWithCNB() {
boolean kanikoExecuteCalled = false
boolean cnbBuildCalled = false
binding.setVariable('docker', new DockerMock('test'))
def pushParams = [:]
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
kanikoExecuteCalled = true
return
})
helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
cnbBuildCalled = true
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'docker',
cnbBuild: true
)
assertThat(cnbBuildCalled, is(true))
assertThat(kanikoExecuteCalled, is(false))
}

@Test
void testKaniko() {
binding.setVariable('docker', new DockerMock('test'))
Expand All @@ -315,6 +360,49 @@ class BuildExecuteTest extends BasePiperTest {
assertThat(buildToolCalled, is(true))
}

@Test
void testCnbBuildCalledWhenConfigured() {
def cnbBuildCalled = false
def npmExecuteScriptsCalled = false
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
npmExecuteScriptsCalled = true
})
helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
cnbBuildCalled = true
return
})
assertThat(nullScript.commonPipelineEnvironment.getContainerProperty('buildpacks'), nullValue())

stepRule.step.buildExecute(
script: nullScript,
buildTool: 'npm',
cnbBuild: true
)

assertThat(npmExecuteScriptsCalled, is(true))
assertThat(cnbBuildCalled, is(true))
}

@Test
void testCnbBuildNotCalledWhenNotConfigured() {
def cnbBuildCalled = false
def npmExecuteScriptsCalled = false
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
npmExecuteScriptsCalled = true
})
helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
cnbBuildCalled = true
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'npm',
cnbBuild: false
)
assertThat(npmExecuteScriptsCalled, is(true))
assertThat(cnbBuildCalled, is(false))
}

@Test
void testHelmExecuteCalledWhenConfigured() {
def helmExecuteCalled = false
Expand Down
17 changes: 14 additions & 3 deletions vars/buildExecute.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hudson.AbortException

import com.sap.piper.DockerUtils
import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
Expand Down Expand Up @@ -33,6 +35,8 @@ import static com.sap.piper.Prerequisites.checkScript
'npmInstall',
/** For buildTool npm: List of npm run scripts to execute */
'npmRunScripts',
/** Defines if a container image(s) should be created with Cloud Native Buildpacks using the artifact produced by the `buildTool`. */
'cnbBuild',
/** toggles if a helmExecute is triggered at end of the step after invoking the build tool */
'helmExecute'
])
Expand Down Expand Up @@ -87,8 +91,7 @@ void call(Map parameters = [:]) {
case 'npm':
npmExecuteScripts script: script, install: config.npmInstall, runScripts: config.npmRunScripts
break
case ['docker', 'kaniko']:
kanikoExecute script: script
case ['docker', 'kaniko']: //handled below
break
default:
if (config.dockerImage && config.dockerCommand) {
Expand All @@ -102,7 +105,15 @@ void call(Map parameters = [:]) {
error "[${STEP_NAME}] buildTool not set and no dockerImage & dockerCommand provided."
}
}

if (config.cnbBuild) {
if (config.buildTool in ['npm', 'gradle', 'maven', 'mta', 'docker']) {
cnbBuild script: script
} else {
throw new AbortException("ERROR - 'cnbBuild' does not support '${config.buildTool}' as a buildTool.")
}
} else if (config.buildTool == 'kaniko' || config.buildTool == 'docker') {
kanikoExecute script: script
}
if(config.helmExecute) {
helmExecute script: script
}
Expand Down

0 comments on commit 9d27e0e

Please sign in to comment.