1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-11-24 08:32:32 +02:00

Fix injection of download cache parameters (#1605)

- NPMRegistry was always an empty string
- MTA did not use npm registry
This commit is contained in:
Daniel Kurzynski 2020-05-27 18:20:10 +02:00 committed by GitHub
parent e6f5544601
commit 3992f17b32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 72 additions and 24 deletions

View File

@ -0,0 +1,5 @@
package com.sap.piper
enum BuildTool {
MAVEN, NPM, MTA
}

View File

@ -3,7 +3,7 @@ package com.sap.piper
class DownloadCacheUtils { class DownloadCacheUtils {
static Map injectDownloadCacheInMavenParameters(Script script, Map parameters) { static Map injectDownloadCacheInParameters(Script script, Map parameters, BuildTool buildTool) {
if (DownloadCacheUtils.isEnabled(script)) { if (DownloadCacheUtils.isEnabled(script)) {
if (!parameters.dockerOptions) { if (!parameters.dockerOptions) {
@ -18,11 +18,17 @@ class DownloadCacheUtils {
} }
parameters.dockerOptions.add(DownloadCacheUtils.getDockerOptions(script)) parameters.dockerOptions.add(DownloadCacheUtils.getDockerOptions(script))
if (parameters.globalSettingsFile) { if (buildTool == BuildTool.MAVEN || buildTool == BuildTool.MTA) {
throw new IllegalArgumentException("You can not specify the parameter globalSettingsFile if the download cache is active") 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 return parameters
@ -76,9 +82,10 @@ class DownloadCacheUtils {
} }
static String getNpmRegistryUri(Script script) { static String getNpmRegistryUri(Script script) {
String npmRegistry = ''
script.node('master') { 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
} }
} }

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
@ -91,13 +92,13 @@ class DownloadCacheUtilsTest extends BasePiperTest {
} }
@Test @Test
void 'injectDownloadCacheInMavenParameters should not change the parameters if dl cache not active'() { void 'injectDownloadCacheInParameters should not change the parameters if dl cache not active'() {
Map newParameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(nullScript, [:]) Map newParameters = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN)
assertTrue(newParameters.isEmpty()) assertTrue(newParameters.isEmpty())
} }
@Test @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_HOSTNAME = 'cx-downloadcache'
nullScript.env.DL_CACHE_NETWORK = 'cx-network' nullScript.env.DL_CACHE_NETWORK = 'cx-network'
@ -106,13 +107,44 @@ class DownloadCacheUtilsTest extends BasePiperTest {
globalSettingsFile: '.pipeline/global_settings.xml' globalSettingsFile: '.pipeline/global_settings.xml'
] ]
Map actual = DownloadCacheUtils.injectDownloadCacheInMavenParameters(nullScript, [:]) Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@Test @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_HOSTNAME = 'cx-downloadcache'
nullScript.env.DL_CACHE_NETWORK = 'cx-network' 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) assertEquals(expectedDockerOptions, actual.dockerOptions)
} }

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import groovy.transform.Field import groovy.transform.Field
@ -13,6 +14,6 @@ void call(Map parameters = [:]) {
[type: 'ssh', id: 'gitSshKeyCredentialsId'], [type: 'ssh', id: 'gitSshKeyCredentialsId'],
[type: 'usernamePassword', id: 'gitHttpsCredentialsId', env: ['PIPER_username', 'PIPER_password']], [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) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials, false, false, true)
} }

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import groovy.transform.Field import groovy.transform.Field
@ -10,7 +11,7 @@ import static com.sap.piper.Prerequisites.checkScript
void call(Map parameters = [:]) { void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this 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']]] List credentials = [[type: 'token', id: 'fortifyCredentialsId', env: ['PIPER_authToken']]]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import groovy.transform.Field import groovy.transform.Field
@ -9,7 +10,7 @@ import static com.sap.piper.Prerequisites.checkScript
void call(Map parameters = [:]) { void call(Map parameters = [:]) {
List credentials = [ ] List credentials = [ ]
final script = checkScript(this, parameters) ?: this 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) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
} }

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import com.sap.piper.BashUtils import com.sap.piper.BashUtils
import groovy.transform.Field import groovy.transform.Field
@ -8,7 +9,7 @@ import static com.sap.piper.Prerequisites.checkScript
def call(Map parameters = [:]) { def call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this final script = checkScript(this, parameters) ?: this
parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN)
validateParameter(parameters.defines, 'defines') validateParameter(parameters.defines, 'defines')
validateParameter(parameters.flags, 'flags') validateParameter(parameters.flags, 'flags')

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.ConfigurationLoader import com.sap.piper.ConfigurationLoader
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import com.sap.piper.QualityCheck import com.sap.piper.QualityCheck
@ -12,7 +13,7 @@ import static com.sap.piper.Prerequisites.checkScript
void call(Map parameters = [:]) { void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: null final script = checkScript(this, parameters) ?: null
List credentials = [] List credentials = []
parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN)
try { try {
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import groovy.transform.Field import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
@ -7,7 +8,7 @@ import com.sap.piper.DownloadCacheUtils
void call(Map parameters = [:]) { void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this final script = checkScript(this, parameters) ?: this
parameters = DownloadCacheUtils.injectDownloadCacheInMavenParameters(script, parameters) parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MTA)
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, []) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, [])
} }

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import groovy.transform.Field import groovy.transform.Field
@ -10,7 +11,7 @@ import static com.sap.piper.Prerequisites.checkScript
void call(Map parameters = [:]) { void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this 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']]] List credentials = [[type: 'usernamePassword', id: 'nexusCredentialsId', env: ['PIPER_user', 'PIPER_password']]]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)

View File

@ -1,3 +1,4 @@
import com.sap.piper.BuildTool
import com.sap.piper.DownloadCacheUtils import com.sap.piper.DownloadCacheUtils
import groovy.transform.Field import groovy.transform.Field
@ -14,10 +15,6 @@ void call(Map parameters = [:]) {
// No credentials required/supported as of now // No credentials required/supported as of now
List credentials = [] List credentials = []
parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.NPM)
parameters['dockerOptions'] = DownloadCacheUtils.getDockerOptions(script)
if (DownloadCacheUtils.isEnabled(script)) {
parameters['defaultNpmRegistry'] = DownloadCacheUtils.getNpmRegistryUri(script)
}
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials) piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
} }