2018-03-12 23:23:10 +02:00
import com.sap.piper.ConfigurationHelper
2018-02-07 14:17:33 +02:00
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import com.sap.piper.versioning.ArtifactVersioning
2018-03-12 23:19:45 +02:00
import groovy.transform.Field
2018-02-07 14:17:33 +02:00
import groovy.text.SimpleTemplateEngine
2018-03-12 23:19:45 +02:00
@Field String STEP_NAME = 'artifactSetVersion'
@Field Set STEP_CONFIG_KEYS = [
'artifactType' ,
'buildTool' ,
'commitVersion' ,
'dockerVersionSource' ,
'filePath' ,
'gitCredentialsId' ,
'gitUserEMail' ,
'gitUserName' ,
'gitSshUrl' ,
'tagPrefix' ,
'timestamp' ,
'timestampTemplate' ,
'versioningTemplate'
]
2018-03-12 23:23:10 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS . plus ( 'gitCommitId' )
2018-02-07 14:17:33 +02:00
2018-03-12 23:19:45 +02:00
def call ( Map parameters = [ : ] ) {
2018-02-07 14:17:33 +02:00
2018-03-12 23:19:45 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2018-02-07 14:17:33 +02:00
def gitUtils = parameters . juStabGitUtils
if ( gitUtils = = null ) {
gitUtils = new GitUtils ( )
}
2018-03-05 10:04:53 +02:00
if ( fileExists ( '.git' ) ) {
if ( sh ( returnStatus: true , script: 'git diff --quiet HEAD' ) ! = 0 )
2018-03-12 23:19:45 +02:00
error "[${STEP_NAME}] Files in the workspace have been changed previously - aborting ${STEP_NAME}"
2018-03-05 10:04:53 +02:00
}
2018-02-07 14:17:33 +02:00
def script = parameters . script
if ( script = = null )
script = [ commonPipelineEnvironment: commonPipelineEnvironment ]
2018-03-12 23:19:45 +02:00
// load default & individual configuration
Map configuration = ConfigurationHelper
. loadStepDefaults ( this )
. mixinStepConfig ( script , STEP_CONFIG_KEYS )
. mixin ( gitCommitId: gitUtils . getGitCommitIdOrNull ( ) )
. mixin ( parameters , PARAMETER_KEYS )
. use ( )
2018-02-07 14:17:33 +02:00
def utils = new Utils ( )
def buildTool = utils . getMandatoryParameter ( configuration , 'buildTool' )
if ( ! configuration . filePath )
configuration . filePath = configuration [ buildTool ] . filePath //use default configuration
def newVersion
def artifactVersioning = ArtifactVersioning . getArtifactVersioning ( buildTool , this , configuration )
if ( configuration . artifactType = = 'appContainer' & & configuration . dockerVersionSource = = 'appVersion' ) {
if ( script . commonPipelineEnvironment . getArtifactVersion ( ) )
//replace + sign if available since + is not allowed in a Docker tag
newVersion = script . commonPipelineEnvironment . getArtifactVersion ( ) . replace ( '+' , '_' )
else
2018-03-12 23:19:45 +02:00
error ( "[${STEP_NAME}] No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the artifactVersion for the application artifact.'" )
2018-02-07 14:17:33 +02:00
} else {
def currentVersion = artifactVersioning . getVersion ( )
def timestamp = configuration . timestamp ? configuration . timestamp : getTimestamp ( configuration . timestampTemplate )
def versioningTemplate = configuration . versioningTemplate ? configuration . versioningTemplate : configuration [ configuration . buildTool ] . versioningTemplate
//defined in default configuration
def binding = [ version: currentVersion , timestamp: timestamp , commitId: configuration . gitCommitId ]
def templatingEngine = new SimpleTemplateEngine ( )
def template = templatingEngine . createTemplate ( versioningTemplate ) . make ( binding )
newVersion = template . toString ( )
}
artifactVersioning . setVersion ( newVersion )
def gitCommitId
2018-03-05 10:04:53 +02:00
if ( configuration . commitVersion ) {
sh 'git add .'
2018-02-07 14:17:33 +02:00
2018-03-05 10:04:53 +02:00
sshagent ( [ configuration . gitCredentialsId ] ) {
def gitUserMailConfig = ''
if ( configuration . gitUserName & & configuration . gitUserEMail )
gitUserMailConfig = "-c user.email=\"${configuration.gitUserEMail}\" -c user.name \"${configuration.gitUserName}\""
2018-02-07 14:17:33 +02:00
2018-03-05 10:04:53 +02:00
try {
sh "git ${gitUserMailConfig} commit -m 'update version ${newVersion}'"
} catch ( e ) {
2018-03-12 23:19:45 +02:00
error "[${STEP_NAME}]git commit failed: ${e}"
2018-03-05 10:04:53 +02:00
}
sh "git remote set-url origin ${configuration.gitSshUrl}"
sh "git tag ${configuration.tagPrefix}${newVersion}"
sh "git push origin ${configuration.tagPrefix}${newVersion}"
2018-02-07 14:17:33 +02:00
2018-03-05 10:04:53 +02:00
gitCommitId = gitUtils . getGitCommitIdOrNull ( )
}
2018-02-07 14:17:33 +02:00
}
2018-03-05 10:04:53 +02:00
if ( buildTool = = 'docker' & & configuration . artifactType = = 'appContainer' ) {
2018-02-07 14:17:33 +02:00
script . commonPipelineEnvironment . setAppContainerProperty ( 'artifactVersion' , newVersion )
script . commonPipelineEnvironment . setAppContainerProperty ( 'gitCommitId' , gitCommitId )
} else {
//standard case
script . commonPipelineEnvironment . setArtifactVersion ( newVersion )
script . commonPipelineEnvironment . setGitCommitId ( gitCommitId )
}
2018-03-05 10:04:53 +02:00
2018-03-12 23:19:45 +02:00
echo "[${STEP_NAME}]New version: ${newVersion}"
2018-02-07 14:17:33 +02:00
}
}
def getTimestamp ( pattern ) {
2018-03-02 17:35:35 +02:00
return sh ( returnStdout: true , script: "date --universal +'${pattern}'" ) . trim ( )
2018-02-07 14:17:33 +02:00
}