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

Use go-based npmExecuteScripts, mavenBuild in buildExecute (#1622)

This commit is contained in:
Florian Wilhelm 2020-06-04 17:53:06 +02:00 committed by GitHub
parent 285537f6a4
commit c490ce9211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 19 deletions

View File

@ -340,6 +340,9 @@ steps:
mavenExecute:
dockerImage: 'maven:3.5-jdk-7'
logSuccessfulMavenTransfers: false
buildExecute:
npmInstall: true
npmRunScripts: []
mtaBuild:
buildTarget: 'NEO'
mtaBuildTool: cloudMbt

View File

@ -15,7 +15,10 @@ import static org.hamcrest.CoreMatchers.containsString
import static org.hamcrest.CoreMatchers.hasItem
import static org.hamcrest.CoreMatchers.is
import static org.hamcrest.CoreMatchers.nullValue
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertThat
import static org.junit.Assert.assertTrue
class BuildExecuteTest extends BasePiperTest {
private ExpectedException exception = ExpectedException.none()
@ -34,9 +37,10 @@ class BuildExecuteTest extends BasePiperTest {
def dockerMockArgs = [:]
class DockerMock {
DockerMock(name){
DockerMock(name) {
dockerMockArgs.name = name
}
def build(image, options) {
return [image: image, options: options]
}
@ -67,22 +71,31 @@ class BuildExecuteTest extends BasePiperTest {
@Test
void testMaven() {
def buildToolCalled = false
helper.registerAllowedMethod('mavenExecute', [Map.class], {m ->
boolean buildToolCalled = false
boolean installOptionSet = false
helper.registerAllowedMethod('mavenBuild', [Map.class], { m ->
buildToolCalled = true
return
})
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
installOptionSet = m['install']
return
})
helper.registerAllowedMethod('fileExists', [String.class], { s ->
return s == 'package.json'
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'maven',
)
assertThat(buildToolCalled, is(true))
assertTrue(buildToolCalled)
assertTrue(installOptionSet)
}
@Test
void testMta() {
def buildToolCalled = false
helper.registerAllowedMethod('mtaBuild', [Map.class], {m ->
helper.registerAllowedMethod('mtaBuild', [Map.class], { m ->
buildToolCalled = true
return
})
@ -96,7 +109,7 @@ class BuildExecuteTest extends BasePiperTest {
@Test
void testNpm() {
def buildToolCalled = false
helper.registerAllowedMethod('npmExecute', [Map.class], {m ->
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
buildToolCalled = true
return
})
@ -107,11 +120,56 @@ class BuildExecuteTest extends BasePiperTest {
assertThat(buildToolCalled, is(true))
}
@Test
void testNpmWithScripts() {
boolean actualValue = false
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
actualValue = (m['runScripts'][0] == 'foo' && m['runScripts'][1] == 'bar')
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'npm',
npmRunScripts: ['foo', 'bar']
)
assertTrue(actualValue)
}
@Test
void testNpmWithInstallFalse() {
boolean actualValue = true
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
actualValue = m['install']
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'npm',
npmInstall: false
)
assertFalse(actualValue)
}
@Test
void testNpmWithInstallTrue() {
boolean actualValue = false
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
actualValue = m['install']
return
})
stepRule.step.buildExecute(
script: nullScript,
buildTool: 'npm',
npmInstall: true
)
assertTrue(actualValue)
}
@Test
void testDocker() {
binding.setVariable('docker', new DockerMock('test'))
def pushParams= [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m ->
def pushParams = [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
pushParams = m
return
})
@ -132,8 +190,8 @@ class BuildExecuteTest extends BasePiperTest {
void testDockerWithEnv() {
nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0')
binding.setVariable('docker', new DockerMock('test'))
def pushParams= [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m ->
def pushParams = [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
pushParams = m
return
})
@ -152,8 +210,8 @@ class BuildExecuteTest extends BasePiperTest {
@Test
void testDockerNoPush() {
binding.setVariable('docker', new DockerMock('test'))
def pushParams= [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m ->
def pushParams = [:]
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
pushParams = m
return
})
@ -172,7 +230,7 @@ class BuildExecuteTest extends BasePiperTest {
@Test
void testKaniko() {
def kanikoParams = [:]
helper.registerAllowedMethod('kanikoExecute', [Map.class], {m ->
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
kanikoParams = m
return
})
@ -191,7 +249,7 @@ class BuildExecuteTest extends BasePiperTest {
@Test
void testKanikoNoPush() {
def kanikoParams = [:]
helper.registerAllowedMethod('kanikoExecute', [Map.class], {m ->
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
kanikoParams = m
return
})
@ -211,7 +269,7 @@ class BuildExecuteTest extends BasePiperTest {
void testSwitchToKaniko() {
shellCallRule.setReturnValue('docker ps -q > /dev/null', 1)
def kanikoParams = [:]
helper.registerAllowedMethod('kanikoExecute', [Map.class], {m ->
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
kanikoParams = m
return
})

View File

@ -3,7 +3,6 @@ import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import groovy.text.GStringTemplateEngine
import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@ -22,7 +21,7 @@ import static com.sap.piper.Prerequisites.checkScript
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
/** Only for Docker builds on the local deamon: Defines the build options for the build.*/
/** Only for Docker builds on the local daemon: Defines the build options for the build.*/
'containerBuildOptions',
/** For custom build types: Defines the command to be executed within the `dockerImage` in order to execute the build. */
'dockerCommand',
@ -30,6 +29,10 @@ import static com.sap.piper.Prerequisites.checkScript
'dockerImage',
/** For Docker builds only (mandatory): tag of the image to be built. */
'dockerImageTag',
/** For buildTool npm: Execute npm install (boolean, default 'true') */
'npmInstall',
/** For buildTool npm: List of npm run scripts to execute */
'npmRunScripts'
])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
@ -68,13 +71,17 @@ void call(Map parameters = [:]) {
switch(config.buildTool){
case 'maven':
mavenExecute script: script
mavenBuild script: script
// in case node_modules exists we assume npm install was executed by maven clean install
if (fileExists('package.json') && !fileExists('node_modules')) {
npmExecuteScripts script: script, install: true
}
break
case 'mta':
mtaBuild script: script
break
case 'npm':
npmExecute script: script
npmExecuteScripts script: script, install: config.npmInstall, runScripts: config.npmRunScripts
break
case ['docker', 'kaniko']:
DockerUtils dockerUtils = new DockerUtils(script)