github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/DownloadCacheUtils.groovy (about) 1 package com.sap.piper 2 3 4 class DownloadCacheUtils { 5 6 static Map injectDownloadCacheInParameters(Script script, Map parameters, BuildTool buildTool) { 7 if (!isEnabled(script)) { 8 return parameters 9 } 10 11 if (!parameters.dockerOptions) { 12 parameters.dockerOptions = [] 13 } 14 if (parameters.dockerOptions instanceof CharSequence) { 15 parameters.dockerOptions = [parameters.dockerOptions] 16 } 17 18 if (!(parameters.dockerOptions instanceof List)) { 19 throw new IllegalArgumentException("Unexpected type for dockerOptions. Expected was either a list or a string. Actual type was: '${parameters.dockerOptions.getClass()}'") 20 } 21 parameters.dockerOptions.add(getDockerOptions(script)) 22 23 if (buildTool == BuildTool.MAVEN || buildTool == BuildTool.MTA) { 24 String globalSettingsFile = getGlobalMavenSettingsForDownloadCache(script) 25 if (parameters.globalSettingsFile && parameters.globalSettingsFile != globalSettingsFile) { 26 throw new IllegalArgumentException("You can not specify the parameter globalSettingsFile if the download cache is active") 27 } 28 29 parameters.globalSettingsFile = globalSettingsFile 30 } 31 32 if (buildTool == BuildTool.NPM || buildTool == BuildTool.MTA) { 33 parameters['defaultNpmRegistry'] = getNpmRegistryUri(script) 34 } 35 36 return parameters 37 } 38 39 static String networkName() { 40 return System.getenv('DL_CACHE_NETWORK') 41 } 42 43 static String hostname() { 44 return System.getenv('DL_CACHE_HOSTNAME') 45 } 46 47 static boolean isEnabled(Script script) { 48 if (script.env.ON_K8S) { 49 return false 50 } 51 52 // Do not enable the DL-cache when a sidecar image is specified. 53 // This is necessary because it is currently not possible to connect a container to multiple networks. 54 // Can be removed when docker plugin supports multiple networks and jenkins-library implemented that feature 55 // See also https://github.com/SAP/jenkins-library/issues/1864 56 if (script.env.SIDECAR_IMAGE) { 57 script.echo "Download cache disabled while running with sidecar image (${script.env.SIDECAR_IMAGE})" 58 return false 59 } 60 61 return (networkName() && hostname()) 62 } 63 64 static String getDockerOptions(Script script) { 65 66 String dockerNetwork = networkName() 67 if (!dockerNetwork) { 68 return '' 69 } 70 return "--network=$dockerNetwork" 71 } 72 73 static String getGlobalMavenSettingsForDownloadCache(Script script) { 74 String globalSettingsFilePath = '.pipeline/global_settings.xml' 75 if (script.fileExists(globalSettingsFilePath)) { 76 return globalSettingsFilePath 77 } 78 79 String hostname = hostname() 80 81 if (!hostname) { 82 return '' 83 } 84 85 String mavenSettingsTemplate = script.libraryResource("com.sap.piper/templates/mvn_download_cache_proxy_settings.xml") 86 String mavenSettings = mavenSettingsTemplate.replace('__HOSTNAME__', hostname) 87 88 if (!script.fileExists('.pipeline')) { 89 script.sh "mkdir .pipeline" 90 } 91 92 script.writeFile file: globalSettingsFilePath, text: mavenSettings 93 return globalSettingsFilePath 94 } 95 96 static String getNpmRegistryUri(Script script) { 97 String hostname = hostname() 98 99 if (!hostname) { 100 return '' 101 } 102 String npmRegistry = "http://${hostname}:8081/repository/npm-proxy/" 103 return npmRegistry 104 } 105 }