2018-09-21 16:55:31 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
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'
|
2018-08-08 22:21:26 +02:00
|
|
|
@Field Map CONFIG_KEY_COMPATIBILITY = [gitSshKeyCredentialsId: 'gitCredentialsId']
|
2018-10-25 14:11:11 +02:00
|
|
|
|
|
|
|
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
|
|
|
|
|
2018-03-12 23:19:45 +02:00
|
|
|
@Field Set STEP_CONFIG_KEYS = [
|
|
|
|
'artifactType',
|
|
|
|
'buildTool',
|
|
|
|
'commitVersion',
|
|
|
|
'dockerVersionSource',
|
|
|
|
'filePath',
|
2018-08-08 22:21:26 +02:00
|
|
|
'gitSshKeyCredentialsId',
|
2018-03-12 23:19:45 +02:00
|
|
|
'gitUserEMail',
|
|
|
|
'gitUserName',
|
|
|
|
'gitSshUrl',
|
|
|
|
'tagPrefix',
|
|
|
|
'timestamp',
|
|
|
|
'timestampTemplate',
|
|
|
|
'versioningTemplate'
|
|
|
|
]
|
2018-10-25 14:11:11 +02:00
|
|
|
|
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-08-30 16:33:07 +02:00
|
|
|
void call(Map parameters = [:], Closure body = null) {
|
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
|
|
|
|
2018-09-21 16:55:31 +02:00
|
|
|
def script = checkScript(this, parameters)
|
|
|
|
|
2018-08-06 08:57:36 +02:00
|
|
|
def gitUtils = parameters.juStabGitUtils ?: new GitUtils()
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-09-13 14:34:26 +02:00
|
|
|
if (gitUtils.isWorkTreeDirty()) {
|
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
|
|
|
if (script == null)
|
2018-04-05 11:36:51 +02:00
|
|
|
script = this
|
2018-03-12 23:19:45 +02:00
|
|
|
// load default & individual configuration
|
2018-10-17 11:05:20 +02:00
|
|
|
ConfigurationHelper configHelper = ConfigurationHelper.newInstance(this)
|
2018-09-07 10:08:16 +02:00
|
|
|
.loadStepDefaults()
|
2018-10-25 14:11:11 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS, CONFIG_KEY_COMPATIBILITY)
|
2018-09-06 16:45:30 +02:00
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS, CONFIG_KEY_COMPATIBILITY)
|
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS, CONFIG_KEY_COMPATIBILITY)
|
2018-03-12 23:19:45 +02:00
|
|
|
.mixin(gitCommitId: gitUtils.getGitCommitIdOrNull())
|
2018-09-06 16:45:30 +02:00
|
|
|
.mixin(parameters, PARAMETER_KEYS, CONFIG_KEY_COMPATIBILITY)
|
2018-08-06 08:57:36 +02:00
|
|
|
.withMandatoryProperty('buildTool')
|
2018-08-08 22:21:26 +02:00
|
|
|
.dependingOn('buildTool').mixin('filePath')
|
|
|
|
.dependingOn('buildTool').mixin('versioningTemplate')
|
2018-03-14 13:29:21 +02:00
|
|
|
|
2018-08-16 09:43:32 +02:00
|
|
|
Map config = configHelper.use()
|
|
|
|
|
|
|
|
config = configHelper.addIfEmpty('timestamp', getTimestamp(config.timestampTemplate))
|
|
|
|
.use()
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-10-30 17:22:42 +02:00
|
|
|
new Utils().pushToSWA([step: STEP_NAME, stepParam1: config.buildTool, stepParam2: config.artifactType, stepParam3: parameters?.script == null], config)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-08-06 08:57:36 +02:00
|
|
|
def artifactVersioning = ArtifactVersioning.getArtifactVersioning(config.buildTool, script, config)
|
2018-08-08 22:21:26 +02:00
|
|
|
def currentVersion = artifactVersioning.getVersion()
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-08-08 22:21:26 +02:00
|
|
|
def newVersion
|
|
|
|
if (config.artifactType == 'appContainer' && config.dockerVersionSource == 'appVersion'){
|
|
|
|
newVersion = currentVersion
|
2018-02-07 14:17:33 +02:00
|
|
|
} else {
|
2018-08-08 22:21:26 +02:00
|
|
|
def binding = [version: currentVersion, timestamp: config.timestamp, commitId: config.gitCommitId]
|
|
|
|
newVersion = new SimpleTemplateEngine().createTemplate(config.versioningTemplate).make(binding).toString()
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
artifactVersioning.setVersion(newVersion)
|
|
|
|
|
2018-08-08 22:21:26 +02:00
|
|
|
if(body != null){
|
|
|
|
body(newVersion)
|
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-08-06 08:57:36 +02:00
|
|
|
if (config.commitVersion) {
|
2018-10-17 11:37:40 +02:00
|
|
|
config = ConfigurationHelper.newInstance(this, config)
|
2018-08-16 09:43:32 +02:00
|
|
|
.addIfEmpty('gitSshUrl', isAppContainer(config)
|
|
|
|
?script.commonPipelineEnvironment.getAppContainerProperty('gitSshUrl')
|
|
|
|
:script.commonPipelineEnvironment.getGitSshUrl())
|
|
|
|
.withMandatoryProperty('gitSshUrl')
|
|
|
|
.use()
|
|
|
|
|
2018-09-03 13:39:59 +02:00
|
|
|
def gitConfig = []
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-09-03 13:39:59 +02:00
|
|
|
if(config.gitUserEMail) gitConfig.add("-c user.email=\"${config.gitUserEMail}\"")
|
|
|
|
if(config.gitUserName) gitConfig.add("-c user.name=\"${config.gitUserName}\"")
|
|
|
|
gitConfig = gitConfig.join(' ')
|
|
|
|
|
|
|
|
try {
|
|
|
|
sh """#!/bin/bash
|
2018-09-27 16:15:49 +02:00
|
|
|
git add .
|
2018-09-03 13:39:59 +02:00
|
|
|
git ${gitConfig} commit -m 'update version ${newVersion}'
|
|
|
|
git tag ${config.tagPrefix}${newVersion}"""
|
2018-08-08 22:21:26 +02:00
|
|
|
config.gitCommitId = gitUtils.getGitCommitIdOrNull()
|
2018-09-03 13:39:59 +02:00
|
|
|
} catch (e) {
|
2018-09-05 11:44:40 +02:00
|
|
|
error "[${STEP_NAME}]git commit and tag failed: ${e}"
|
2018-09-03 13:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sshagent([config.gitSshKeyCredentialsId]) {
|
|
|
|
sh "git push ${config.gitSshUrl} ${config.tagPrefix}${newVersion}"
|
2018-03-05 10:04:53 +02:00
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 09:43:32 +02:00
|
|
|
if (isAppContainer(config)) {
|
2018-02-07 14:17:33 +02:00
|
|
|
script.commonPipelineEnvironment.setAppContainerProperty('artifactVersion', newVersion)
|
2018-08-08 22:21:26 +02:00
|
|
|
script.commonPipelineEnvironment.setAppContainerProperty('gitCommitId', config.gitCommitId)
|
2018-02-07 14:17:33 +02:00
|
|
|
} else {
|
|
|
|
//standard case
|
|
|
|
script.commonPipelineEnvironment.setArtifactVersion(newVersion)
|
2018-08-08 22:21:26 +02:00
|
|
|
script.commonPipelineEnvironment.setGitCommitId(config.gitCommitId)
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:43:32 +02:00
|
|
|
def isAppContainer(config){
|
|
|
|
return config.buildTool == 'docker' && config.artifactType == 'appContainer'
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|