2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
2019-04-01 11:55:39 +02:00
import com.sap.piper.GenerateDocumentation
2018-08-15 11:53:28 +02:00
import com.sap.piper.ConfigurationHelper
2018-03-13 10:31:01 +02:00
import com.sap.piper.MtaUtils
2018-08-15 11:53:28 +02:00
import com.sap.piper.Utils
import groovy.transform.Field
2017-11-24 16:59:34 +02:00
2019-04-24 12:47:37 +02:00
import static com . sap . piper . Utils . downloadSettingsFromUrl
2018-11-29 10:54:05 +02:00
@Field def STEP_NAME = getClass ( ) . getName ( )
2018-02-08 14:18:04 +02:00
2018-08-15 11:53:28 +02:00
@Field Set GENERAL_CONFIG_KEYS = [ ]
@Field Set STEP_CONFIG_KEYS = [
2019-04-01 11:55:39 +02:00
/** The name of the application which is being built. If the parameter has been provided and no `mta.yaml` exists, the `mta.yaml` will be automatically generated using this parameter and the information (`name` and `version`) from `package.json` before the actual build starts.*/
2018-08-15 11:53:28 +02:00
'applicationName' ,
2019-04-01 11:55:39 +02:00
/ * *
2019-11-22 13:31:32 +02:00
* mtaBuildTool classic only: The target platform to which the mtar can be deployed .
2019-04-01 11:55:39 +02:00
* @possibleValues 'CF' , 'NEO' , 'XSA'
* /
2018-08-15 11:53:28 +02:00
'buildTarget' ,
2019-11-21 15:56:40 +02:00
/ * *
* Tool to use when building the MTA
* @possibleValues 'classic' , 'cloudMbt'
* /
2019-11-22 13:31:32 +02:00
'mtaBuildTool' ,
2019-04-01 11:55:39 +02:00
/** @see dockerExecute */
2018-09-04 11:32:54 +02:00
'dockerImage' ,
2019-07-25 11:57:21 +02:00
/** @see dockerExecute */
'dockerEnvVars' ,
/** @see dockerExecute */
'dockerOptions' ,
/** @see dockerExecute */
'dockerWorkspace' ,
2019-04-01 11:55:39 +02:00
/** The path to the extension descriptor file.*/
2018-08-15 11:53:28 +02:00
'extension' ,
2019-04-01 11:55:39 +02:00
/ * *
* The location of the SAP Multitarget Application Archive Builder jar file , including file name and extension .
2019-11-17 23:08:47 +02:00
* If you run on Docker , this must match the location of the jar file in the container as well .
2019-04-01 11:55:39 +02:00
* /
2019-04-17 10:44:55 +02:00
'mtaJarLocation' ,
2019-04-24 12:47:37 +02:00
/** Path or url to the mvn settings file that should be used as global settings file.*/
'globalSettingsFile' ,
2019-12-06 08:48:20 +02:00
/** The name of the generated mtar file including its extension. */
'mtarName' ,
2019-11-21 15:56:40 +02:00
/ * *
2019-11-22 13:31:32 +02:00
* mtaBuildTool cloudMbt only: The target platform to which the mtar can be deployed .
2019-11-21 15:56:40 +02:00
* @possibleValues 'CF' , 'NEO' , 'XSA'
* /
'platform' ,
2019-04-17 10:44:55 +02:00
/** Path or url to the mvn settings file that should be used as project settings file.*/
'projectSettingsFile'
2018-08-15 11:53:28 +02:00
]
2018-09-04 11:32:54 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS . plus ( [
2019-05-09 08:51:11 +02:00
/** Url to the npm registry that should be used for installing npm dependencies.*/
'defaultNpmRegistry'
2018-09-04 11:32:54 +02:00
] )
2018-02-08 14:18:04 +02:00
2019-04-01 11:55:39 +02:00
/ * *
* Executes the SAP Multitarget Application Archive Builder to create an mtar archive of the application .
* /
@GenerateDocumentation
2018-09-12 10:32:30 +02:00
void call ( Map parameters = [ : ] ) {
2018-09-04 11:32:54 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2018-09-21 16:55:31 +02:00
2018-10-31 09:40:12 +02:00
final script = checkScript ( this , parameters ) ? : this
2018-02-08 14:18:04 +02:00
2018-08-15 11:53:28 +02:00
// load default & individual configuration
2018-10-17 11:05:20 +02:00
Map configuration = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-08-15 11:53:28 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
2018-09-04 11:32:54 +02:00
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS )
2018-08-15 11:53:28 +02:00
. mixin ( parameters , PARAMETER_KEYS )
2019-11-22 13:31:32 +02:00
. dependingOn ( 'mtaBuildTool' ) . mixin ( 'dockerImage' )
2018-08-15 11:53:28 +02:00
. use ( )
2018-02-08 14:18:04 +02:00
2019-01-21 09:47:34 +02:00
new Utils ( ) . pushToSWA ( [
step: STEP_NAME ,
stepParamKey1: 'scriptMissing' ,
stepParam1: parameters ? . script = = null
] , configuration )
2017-07-11 15:12:03 +02:00
2019-07-25 11:57:21 +02:00
dockerExecute (
script: script ,
dockerImage: configuration . dockerImage ,
dockerEnvVars: configuration . dockerEnvVars ,
dockerOptions: configuration . dockerOptions ,
dockerWorkspace: configuration . dockerWorkspace
) {
2018-02-20 14:09:16 +02:00
2019-04-24 12:47:37 +02:00
String projectSettingsFile = configuration . projectSettingsFile ? . trim ( )
if ( projectSettingsFile ) {
if ( projectSettingsFile . startsWith ( "http" ) ) {
projectSettingsFile = downloadSettingsFromUrl ( this , projectSettingsFile , 'project-settings.xml' )
}
2019-04-17 10:44:55 +02:00
sh 'mkdir -p $HOME/.m2'
2019-04-24 12:47:37 +02:00
sh "cp ${projectSettingsFile} \$HOME/.m2/settings.xml"
}
String globalSettingsFile = configuration . globalSettingsFile ? . trim ( )
if ( globalSettingsFile ) {
if ( globalSettingsFile . startsWith ( "http" ) ) {
globalSettingsFile = downloadSettingsFromUrl ( this , globalSettingsFile , 'global-settings.xml' )
}
sh "cp ${globalSettingsFile} \$M2_HOME/conf/settings.xml"
2019-04-17 10:44:55 +02:00
}
2019-05-09 08:51:11 +02:00
String defaultNpmRegistry = configuration . defaultNpmRegistry ? . trim ( )
if ( defaultNpmRegistry ) {
sh "npm config set registry $defaultNpmRegistry"
}
2018-09-04 11:32:54 +02:00
def mtaYamlName = "mta.yaml"
def applicationName = configuration . applicationName
2018-03-13 10:31:01 +02:00
2018-09-04 11:32:54 +02:00
if ( ! fileExists ( mtaYamlName ) ) {
if ( ! applicationName ) {
2018-11-13 09:55:18 +02:00
error "'${mtaYamlName}' not found in project sources and 'applicationName' not provided as parameter - cannot generate '${mtaYamlName}' file."
2018-09-04 11:32:54 +02:00
} else {
2018-11-13 09:55:18 +02:00
echo "[INFO] '${mtaYamlName}' file not found in project sources, but application name provided as parameter - generating '${mtaYamlName}' file."
2018-09-04 11:32:54 +02:00
MtaUtils mtaUtils = new MtaUtils ( this )
mtaUtils . generateMtaDescriptorFromPackageJson ( "package.json" , mtaYamlName , applicationName )
}
2018-11-13 09:55:18 +02:00
} else {
echo "[INFO] '${mtaYamlName}' file found in project sources."
2018-03-21 13:29:15 +02:00
}
2017-07-11 15:12:03 +02:00
2018-09-04 11:32:54 +02:00
//[Q]: Why not yaml.dump()? [A]: This reformats the whole file.
sh "sed -ie \"s/\\\${timestamp}/`date +%Y%m%d%H%M%S`/g\" \"${mtaYamlName}\""
2017-07-11 15:12:03 +02:00
2019-11-21 15:56:40 +02:00
def mtaCall
def options = [ ]
2019-12-06 08:48:20 +02:00
String mtarName = configuration . mtarName ? . trim ( )
if ( ! mtarName ) {
def mtaId = getMtaId ( mtaYamlName )
mtarName = "${mtaId}.mtar"
}
options . push ( "--mtar ${mtarName}" )
2019-11-21 15:56:40 +02:00
2019-11-22 13:31:32 +02:00
switch ( configuration . mtaBuildTool ) {
2019-11-21 15:56:40 +02:00
case 'classic' :
// If it is not configured, it is expected on the PATH
def mtaJar = configuration . mtaJarLocation ? : 'mta.jar'
options . push ( "--build-target=${configuration.buildTarget}" )
if ( configuration . extension ) options . push ( "--extension=${configuration.extension}" )
mtaCall = "java -jar ${mtaJar} ${options.join(' ')} build"
break
case 'cloudMbt' :
options . push ( "--platform ${configuration.platform}" )
2019-12-04 14:24:23 +02:00
options . push ( "--target ./" )
2019-11-21 15:56:40 +02:00
if ( configuration . extension ) options . push ( "--extensions=${configuration.extension}" )
mtaCall = "mbt build ${options.join(' ')}"
break
default :
2019-11-22 13:31:32 +02:00
error "[ERROR][${STEP_NAME}] MTA build tool '${configuration.mtaBuildTool}' not supported!"
2018-09-04 11:32:54 +02:00
}
2017-07-11 15:12:03 +02:00
2018-11-12 16:50:38 +02:00
echo "[INFO] Executing mta build call: '${mtaCall}'."
2019-06-13 15:33:18 +02:00
//[Q]: Why extending the path? [A]: To be sure e.g. grunt can be found
//[Q]: Why escaping \$PATH ? [A]: We want to extend the PATH variable in e.g. the container and not substituting it with the Jenkins environment when using ${PATH}
2018-09-04 11:32:54 +02:00
sh "" " # ! /bin/ bash
2019-06-13 13:33:03 +02:00
export PATH = . /node_modules/ . bin : \ $PATH
2018-04-17 17:35:10 +02:00
$mtaCall
2017-07-11 15:12:03 +02:00
"" "
2019-12-06 08:48:20 +02:00
script ? . commonPipelineEnvironment ? . setMtarFilePath ( "${mtarName}" )
2018-09-04 11:32:54 +02:00
}
2017-07-11 15:12:03 +02:00
}
}
2019-11-21 15:56:40 +02:00
def String getMtaId ( String fileName ) {
def mtaYaml = readYaml file: fileName
if ( ! mtaYaml . ID ) {
error "Property 'ID' not found in ${fileName} file."
}
return mtaYaml . ID
}