1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/vars/artifactSetVersion.groovy

122 lines
4.8 KiB
Groovy
Raw Normal View History

2018-03-12 23:23:10 +02:00
import com.sap.piper.ConfigurationHelper
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
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-03-12 23:19:45 +02:00
def call(Map parameters = [:]) {
2018-03-12 23:19:45 +02:00
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def gitUtils = parameters.juStabGitUtils
if (gitUtils == null) {
gitUtils = new GitUtils()
}
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}"
}
def script = parameters.script
if (script == null)
script = this
2018-03-12 23:19:45 +02:00
// load default & individual configuration
Map configuration = ConfigurationHelper
.loadStepDefaults(this)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
2018-03-12 23:19:45 +02:00
.mixin(gitCommitId: gitUtils.getGitCommitIdOrNull())
.mixin(parameters, PARAMETER_KEYS)
.use()
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, script, 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.'")
} 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
if (configuration.commitVersion) {
sh 'git add .'
sshagent([configuration.gitCredentialsId]) {
def gitUserMailConfig = ''
if (configuration.gitUserName && configuration.gitUserEMail)
gitUserMailConfig = "-c user.email=\"${configuration.gitUserEMail}\" -c user.name=\"${configuration.gitUserName}\""
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}"
}
sh "git remote set-url origin ${configuration.gitSshUrl}"
sh "git tag ${configuration.tagPrefix}${newVersion}"
sh "git push origin ${configuration.tagPrefix}${newVersion}"
gitCommitId = gitUtils.getGitCommitIdOrNull()
}
}
if (buildTool == 'docker' && configuration.artifactType == 'appContainer') {
script.commonPipelineEnvironment.setAppContainerProperty('artifactVersion', newVersion)
script.commonPipelineEnvironment.setAppContainerProperty('gitCommitId', gitCommitId)
} else {
//standard case
script.commonPipelineEnvironment.setArtifactVersion(newVersion)
script.commonPipelineEnvironment.setGitCommitId(gitCommitId)
}
2018-03-12 23:19:45 +02:00
echo "[${STEP_NAME}]New version: ${newVersion}"
}
}
def getTimestamp(pattern){
return sh(returnStdout: true, script: "date --universal +'${pattern}'").trim()
}