2023-09-07 11:14:04 +02:00
import hudson.AbortException
2019-07-02 13:00:36 +02:00
import com.sap.piper.DockerUtils
import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import groovy.transform.Field
import static com . sap . piper . Prerequisites . checkScript
@Field String STEP_NAME = getClass ( ) . getName ( )
@Field Set GENERAL_CONFIG_KEYS = [
/ * *
* Defines the tool used for the build .
* @possibleValues ` docker ` , ` kaniko ` , ` maven ` , ` mta ` , ` npm `
* /
'buildTool' ,
/** For Docker builds only (mandatory): name of the image to be built. */
'dockerImageName' ,
/** For Docker builds only: Defines the registry url where the image should be pushed to, incl. the protocol like `https://my.registry.com`. If it is not defined, image will not be pushed to a registry.*/
'dockerRegistryUrl' ,
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS . plus ( [
2020-06-04 17:53:06 +02:00
/** Only for Docker builds on the local daemon: Defines the build options for the build.*/
2019-07-02 13:00:36 +02:00
'containerBuildOptions' ,
/** For custom build types: Defines the command to be executed within the `dockerImage` in order to execute the build. */
'dockerCommand' ,
/** For custom build types: Image to be used for builds in case they should run inside a custom Docker container */
'dockerImage' ,
/** For Docker builds only (mandatory): tag of the image to be built. */
'dockerImageTag' ,
2020-06-04 17:53:06 +02:00
/** For buildTool npm: Execute npm install (boolean, default 'true') */
'npmInstall' ,
/** For buildTool npm: List of npm run scripts to execute */
2023-08-21 11:10:00 +02:00
'npmRunScripts' ,
2023-09-07 11:14:04 +02:00
/** Defines if a container image(s) should be created with Cloud Native Buildpacks using the artifact produced by the `buildTool`. */
'cnbBuild' ,
2023-08-21 11:10:00 +02:00
/** toggles if a helmExecute is triggered at end of the step after invoking the build tool */
'helmExecute'
2019-07-02 13:00:36 +02:00
] )
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
/ * *
* This step serves as generic entry point in pipelines for building artifacts .
*
* You can use pre - defined ` buildTool ` s .
*
* Alternatively you can define a command via ` dockerCommand ` which should be executed in ` dockerImage ` . < br / >
* This allows you to trigger any build tool using a defined Docker container which provides the required build infrastructure .
*
* When using ` buildTool: docker ` or ` buildTool: kaniko ` the created container image is uploaded to a container registry . < br / >
* You need to make sure that the required credentials are provided to the step .
*
* For all other ` buildTool ` s the artifact will just be stored in the workspace and could then be ` stash ` ed for later use .
*
* /
@GenerateDocumentation
void call ( Map parameters = [ : ] ) {
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
final script = checkScript ( this , parameters ) ? : this
def utils = parameters . juStabUtils ? : new Utils ( )
2020-08-26 15:32:58 +02:00
String stageName = parameters . stageName ? : env . STAGE_NAME
2019-07-02 13:00:36 +02:00
// handle deprecated parameters
// load default & individual configuration
Map config = ConfigurationHelper . newInstance ( this )
2020-08-26 15:32:58 +02:00
. loadStepDefaults ( [ : ] , stageName )
2019-07-02 13:00:36 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
2020-08-26 15:32:58 +02:00
. mixinStageConfig ( script . commonPipelineEnvironment , stageName , STEP_CONFIG_KEYS )
2019-07-02 13:00:36 +02:00
. mixin ( parameters , PARAMETER_KEYS )
. addIfEmpty ( 'dockerImageTag' , script . commonPipelineEnvironment . getArtifactVersion ( ) )
2020-06-09 14:52:03 +02:00
. addIfEmpty ( 'buildTool' , script . commonPipelineEnvironment . getBuildTool ( ) )
2019-07-02 13:00:36 +02:00
. use ( )
// telemetry reporting
utils . pushToSWA ( [ stepParam1: config . buildTool , 'buildTool' : config . buildTool ] , config )
switch ( config . buildTool ) {
case 'maven' :
2020-06-04 17:53:06 +02:00
mavenBuild script: script
// in case node_modules exists we assume npm install was executed by maven clean install
if ( fileExists ( 'package.json' ) & & ! fileExists ( 'node_modules' ) ) {
npmExecuteScripts script: script , install: true
}
2019-07-02 13:00:36 +02:00
break
case 'mta' :
mtaBuild script: script
break
case 'npm' :
2020-06-04 17:53:06 +02:00
npmExecuteScripts script: script , install: config . npmInstall , runScripts: config . npmRunScripts
2019-07-02 13:00:36 +02:00
break
2023-09-07 11:14:04 +02:00
case [ 'docker' , 'kaniko' ] : //handled below
2019-07-02 13:00:36 +02:00
break
default :
if ( config . dockerImage & & config . dockerCommand ) {
dockerExecute (
script: script ,
dockerImage: config . dockerImage ,
) {
sh "${config.dockerCommand}"
}
} else {
error "[${STEP_NAME}] buildTool not set and no dockerImage & dockerCommand provided."
}
}
2023-09-07 11:14:04 +02:00
if ( config . cnbBuild ) {
if ( config . buildTool in [ 'npm' , 'gradle' , 'maven' , 'mta' , 'docker' ] ) {
cnbBuild script: script
} else {
throw new AbortException ( "ERROR - 'cnbBuild' does not support '${config.buildTool}' as a buildTool." )
}
} else if ( config . buildTool = = 'kaniko' | | config . buildTool = = 'docker' ) {
kanikoExecute script: script
}
2023-08-21 11:10:00 +02:00
if ( config . helmExecute ) {
helmExecute script: script
}
2019-07-02 13:00:36 +02:00
}
}