diff --git a/src/com/sap/piper/BuildTool.groovy b/src/com/sap/piper/BuildTool.groovy new file mode 100644 index 000000000..c177a5b76 --- /dev/null +++ b/src/com/sap/piper/BuildTool.groovy @@ -0,0 +1,5 @@ +package com.sap.piper + +enum BuildTool { + MAVEN, NPM, MTA +} diff --git a/src/com/sap/piper/DownloadCacheUtils.groovy b/src/com/sap/piper/DownloadCacheUtils.groovy index a38e09461..24b28f62e 100644 --- a/src/com/sap/piper/DownloadCacheUtils.groovy +++ b/src/com/sap/piper/DownloadCacheUtils.groovy @@ -3,7 +3,7 @@ package com.sap.piper class DownloadCacheUtils { - static Map injectDownloadCacheInMavenParameters(Script script, Map parameters) { + static Map injectDownloadCacheInParameters(Script script, Map parameters, BuildTool buildTool) { if (DownloadCacheUtils.isEnabled(script)) { if (!parameters.dockerOptions) { @@ -18,11 +18,17 @@ class DownloadCacheUtils { } parameters.dockerOptions.add(DownloadCacheUtils.getDockerOptions(script)) - if (parameters.globalSettingsFile) { - throw new IllegalArgumentException("You can not specify the parameter globalSettingsFile if the download cache is active") + if (buildTool == BuildTool.MAVEN || buildTool == BuildTool.MTA) { + if (parameters.globalSettingsFile) { + throw new IllegalArgumentException("You can not specify the parameter globalSettingsFile if the download cache is active") + } + + parameters.globalSettingsFile = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(script) } - parameters.globalSettingsFile = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(script) + if (buildTool == BuildTool.NPM || buildTool == buildTool.MTA) { + parameters['defaultNpmRegistry'] = DownloadCacheUtils.getNpmRegistryUri(script) + } } return parameters @@ -76,9 +82,10 @@ class DownloadCacheUtils { } static String getNpmRegistryUri(Script script) { + String npmRegistry = '' script.node('master') { - return "http://${script.env.DL_CACHE_HOSTNAME}:8081/repository/npm-proxy/" + npmRegistry = "http://${script.env.DL_CACHE_HOSTNAME}:8081/repository/npm-proxy/" } - return "" + return npmRegistry } } diff --git a/test/groovy/DownloadCacheUtilsTest.groovy b/test/groovy/DownloadCacheUtilsTest.groovy index 3285d9b35..72b83def6 100644 --- a/test/groovy/DownloadCacheUtilsTest.groovy +++ b/test/groovy/DownloadCacheUtilsTest.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import org.junit.Before import org.junit.Rule @@ -91,13 +92,13 @@ class DownloadCacheUtilsTest extends BasePiperTest { } @Test - void 'injectDownloadCacheInMavenParameters should not change the parameters if dl cache not active'() { - Map newParameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(nullScript, [:]) + void 'injectDownloadCacheInParameters should not change the parameters if dl cache not active'() { + Map newParameters = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN) assertTrue(newParameters.isEmpty()) } @Test - void 'injectDownloadCacheInMavenParameters should set docker options and global settings'() { + void 'injectDownloadCacheInParameters should set docker options and global settings for maven'() { nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache' nullScript.env.DL_CACHE_NETWORK = 'cx-network' @@ -106,13 +107,44 @@ class DownloadCacheUtilsTest extends BasePiperTest { globalSettingsFile: '.pipeline/global_settings.xml' ] - Map actual = DownloadCacheUtils.injectDownloadCacheInMavenParameters(nullScript, [:]) + Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN) assertEquals(expected, actual) } @Test - void 'injectDownloadCacheInMavenParameters should append docker options'() { + void 'injectDownloadCacheInParameters should set docker options, global settings and npm default registry for mta'() { + nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache' + nullScript.env.DL_CACHE_NETWORK = 'cx-network' + + Map expected = [ + dockerOptions: ['--network=cx-network'], + globalSettingsFile: '.pipeline/global_settings.xml', + defaultNpmRegistry: 'http://cx-downloadcache:8081/repository/npm-proxy/' + ] + + Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MTA) + + assertEquals(expected, actual) + } + + @Test + void 'injectDownloadCacheInParameters should set docker options and default npm config for npm'() { + nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache' + nullScript.env.DL_CACHE_NETWORK = 'cx-network' + + Map expected = [ + dockerOptions: ['--network=cx-network'], + defaultNpmRegistry: 'http://cx-downloadcache:8081/repository/npm-proxy/' + ] + + Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.NPM) + + assertEquals(expected, actual) + } + + @Test + void 'injectDownloadCacheInParameters should append docker options'() { nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache' nullScript.env.DL_CACHE_NETWORK = 'cx-network' @@ -120,7 +152,7 @@ class DownloadCacheUtilsTest extends BasePiperTest { - Map actual = DownloadCacheUtils.injectDownloadCacheInMavenParameters(nullScript, [dockerOptions: '--test']) + Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [dockerOptions: '--test'], BuildTool.MAVEN) assertEquals(expectedDockerOptions, actual.dockerOptions) } diff --git a/vars/artifactPrepareVersion.groovy b/vars/artifactPrepareVersion.groovy index 01c90014e..986aeb3e0 100644 --- a/vars/artifactPrepareVersion.groovy +++ b/vars/artifactPrepareVersion.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import groovy.transform.Field @@ -13,6 +14,6 @@ void call(Map parameters = [:]) { [type: 'ssh', id: 'gitSshKeyCredentialsId'], [type: 'usernamePassword', id: 'gitHttpsCredentialsId', env: ['PIPER_username', 'PIPER_password']], ] - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials, false, false, true) } diff --git a/vars/fortifyExecuteScan.groovy b/vars/fortifyExecuteScan.groovy index 94465d422..914ad2a15 100644 --- a/vars/fortifyExecuteScan.groovy +++ b/vars/fortifyExecuteScan.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import groovy.transform.Field @@ -10,7 +11,7 @@ import static com.sap.piper.Prerequisites.checkScript void call(Map parameters = [:]) { final script = checkScript(this, parameters) ?: this - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) List credentials = [[type: 'token', id: 'fortifyCredentialsId', env: ['PIPER_authToken']]] piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) diff --git a/vars/mavenBuild.groovy b/vars/mavenBuild.groovy index 0c0523827..2bf6bfb70 100644 --- a/vars/mavenBuild.groovy +++ b/vars/mavenBuild.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import groovy.transform.Field @@ -9,7 +10,7 @@ import static com.sap.piper.Prerequisites.checkScript void call(Map parameters = [:]) { List credentials = [ ] final script = checkScript(this, parameters) ?: this - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) } diff --git a/vars/mavenExecute.groovy b/vars/mavenExecute.groovy index bfaeeff7e..385035e67 100644 --- a/vars/mavenExecute.groovy +++ b/vars/mavenExecute.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import com.sap.piper.BashUtils import groovy.transform.Field @@ -8,7 +9,7 @@ import static com.sap.piper.Prerequisites.checkScript def call(Map parameters = [:]) { final script = checkScript(this, parameters) ?: this - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) validateParameter(parameters.defines, 'defines') validateParameter(parameters.flags, 'flags') diff --git a/vars/mavenExecuteStaticCodeChecks.groovy b/vars/mavenExecuteStaticCodeChecks.groovy index 5d7ec0063..492ab1fd8 100644 --- a/vars/mavenExecuteStaticCodeChecks.groovy +++ b/vars/mavenExecuteStaticCodeChecks.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.ConfigurationLoader import com.sap.piper.DownloadCacheUtils import com.sap.piper.QualityCheck @@ -12,7 +13,7 @@ import static com.sap.piper.Prerequisites.checkScript void call(Map parameters = [:]) { final script = checkScript(this, parameters) ?: null List credentials = [] - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) try { piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 9ad0be8b9..c090b4d6c 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import groovy.transform.Field import static com.sap.piper.Prerequisites.checkScript import com.sap.piper.DownloadCacheUtils @@ -7,7 +8,7 @@ import com.sap.piper.DownloadCacheUtils void call(Map parameters = [:]) { final script = checkScript(this, parameters) ?: this - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MTA) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, []) } diff --git a/vars/nexusUpload.groovy b/vars/nexusUpload.groovy index e4d0ea113..8b50450e0 100644 --- a/vars/nexusUpload.groovy +++ b/vars/nexusUpload.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import groovy.transform.Field @@ -10,7 +11,7 @@ import static com.sap.piper.Prerequisites.checkScript void call(Map parameters = [:]) { final script = checkScript(this, parameters) ?: this - parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN) List credentials = [[type: 'usernamePassword', id: 'nexusCredentialsId', env: ['PIPER_user', 'PIPER_password']]] piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) diff --git a/vars/npmExecuteScripts.groovy b/vars/npmExecuteScripts.groovy index bceaa4c09..4af34150f 100644 --- a/vars/npmExecuteScripts.groovy +++ b/vars/npmExecuteScripts.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.BuildTool import com.sap.piper.DownloadCacheUtils import groovy.transform.Field @@ -14,10 +15,6 @@ void call(Map parameters = [:]) { // No credentials required/supported as of now List credentials = [] - - parameters['dockerOptions'] = DownloadCacheUtils.getDockerOptions(script) - if (DownloadCacheUtils.isEnabled(script)) { - parameters['defaultNpmRegistry'] = DownloadCacheUtils.getNpmRegistryUri(script) - } + parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.NPM) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) }