github.com/xgoffin/jenkins-library@v1.154.0/vars/writeTemporaryCredentials.groovy (about) 1 import com.sap.piper.ConfigurationHelper 2 import com.sap.piper.ConfigurationLoader 3 import com.sap.piper.GenerateDocumentation 4 import com.sap.piper.JsonUtils 5 import com.sap.piper.TemporaryCredentialsUtils 6 import com.sap.piper.Utils 7 8 import groovy.transform.Field 9 10 import static com.sap.piper.Prerequisites.checkScript 11 12 @Field String STEP_NAME = getClass().getName() 13 14 @Field Set GENERAL_CONFIG_KEYS = [] 15 @Field Set STEP_CONFIG_KEYS = [ 16 /** 17 * The list of credentials that are written to a temporary file for the execution of the body. 18 * Each element of credentials must be a map containing a property alias and a property credentialId. 19 * You have to ensure that corresponding credential entries exist in your Jenkins configuration. 20 */ 21 'credentials', 22 /** 23 * The list of paths to directories where credentials files need to be placed. 24 */ 25 'credentialsDirectories' 26 ] 27 @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS 28 29 /** 30 * Writes credentials to a temporary file and deletes it after the body has been executed. 31 */ 32 @GenerateDocumentation 33 void call(Map parameters = [:], Closure body) { 34 handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) { 35 def script = checkScript(this, parameters) ?: this 36 String stageName = parameters.stageName ?: env.STAGE_NAME 37 38 Map config = ConfigurationHelper.newInstance(this) 39 .loadStepDefaults([:], stageName) 40 .mixin(ConfigurationLoader.defaultStageConfiguration(script, stageName)) 41 .mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS) 42 .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS) 43 .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS) 44 .mixin(parameters, PARAMETER_KEYS) 45 .use() 46 47 // telemetry reporting 48 new Utils().pushToSWA([ 49 step: STEP_NAME, 50 stepParamKey1: 'scriptMissing', 51 stepParam1: parameters?.script == null 52 ], config) 53 54 if (config.credentials && !(config.credentials instanceof List)) { 55 error "[${STEP_NAME}] The execution failed, since credentials is not a list. Please provide credentials as a list of maps. For example:\n" + 56 "credentials: \n" + " - alias: 'ERP'\n" + " credentialId: 'erp-credentials'" 57 } 58 if (!config.credentialsDirectories) { 59 error "[${STEP_NAME}] The execution failed, since no credentialsDirectories are defined. Please provide a list of paths for the credentials files.\n" 60 } 61 if (!(config.credentialsDirectories instanceof List)) { 62 error "[${STEP_NAME}] The execution failed, since credentialsDirectories is not a list. Please provide credentialsDirectories as a list of paths.\n" 63 } 64 65 TemporaryCredentialsUtils credUtils = new TemporaryCredentialsUtils(script) 66 67 credUtils.handleTemporaryCredentials(config.credentials, config.credentialsDirectories) { 68 body() 69 } 70 } 71 }