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

313 lines
14 KiB
Groovy
Raw Normal View History

import static com.sap.piper.Prerequisites.checkScript
2019-03-28 11:46:19 +02:00
import com.sap.piper.GenerateDocumentation
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.GStringTemplateEngine
enum GitPushMode {NONE, HTTPS, SSH}
@Field String STEP_NAME = getClass().getName()
@Field Map CONFIG_KEY_COMPATIBILITY = [gitSshKeyCredentialsId: 'gitCredentialsId']
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
2018-03-12 23:19:45 +02:00
@Field Set STEP_CONFIG_KEYS = [
2019-03-28 11:46:19 +02:00
/**
* Defines the type of the artifact.
* @possibleValues `appContainer`
*/
2018-03-12 23:19:45 +02:00
'artifactType',
2019-03-28 11:46:19 +02:00
/**
* Defines the tool which is used for building the artifact.
2019-07-03 11:27:07 +02:00
* @possibleValues `dub`, `docker`, `golang`, `maven`, `mta`, `npm`, `pip`, `sbt`
2019-03-28 11:46:19 +02:00
*/
2018-03-12 23:19:45 +02:00
'buildTool',
2019-03-28 11:46:19 +02:00
/**
* Controls if the changed version is committed and pushed to the git repository.
* If this is enabled (which is the default), you need to provide `gitCredentialsId` and `gitSshUrl`.
* @possibleValues `true`, `false`
*/
2018-03-12 23:19:45 +02:00
'commitVersion',
2019-07-16 11:11:59 +02:00
/**
* Prints some more information for troubleshooting. May reveal security relevant information. Usage is recommanded for troubleshooting only. Productive usage
2019-07-16 16:41:21 +02:00
* is not recommended.
* @possibleValues `true`, `false`
2019-07-16 11:11:59 +02:00
*/
2019-07-16 16:01:43 +02:00
'verbose',
2019-07-16 11:11:59 +02:00
,
2019-03-28 11:46:19 +02:00
/**
* Specifies the source to be used for the main version which is used for generating the automatic version.
* * This can either be the version of the base image - as retrieved from the `FROM` statement within the Dockerfile, e.g. `FROM jenkins:2.46.2`
* * Alternatively the name of an environment variable defined in the Docker image can be used which contains the version number, e.g. `ENV MY_VERSION 1.2.3`
* * The third option `appVersion` applies only to the artifactType `appContainer`. Here the version of the app which is packaged into the container will be used as version for the container itself.
* @possibleValues FROM, (ENV name),appVersion
*/
2018-03-12 23:19:45 +02:00
'dockerVersionSource',
2019-03-28 11:46:19 +02:00
/**
* Defines a custom path to the descriptor file.
*/
2018-03-12 23:19:45 +02:00
'filePath',
2019-03-28 11:46:19 +02:00
/**
* Defines the ssh git credentials to be used for writing the tag.
*/
'gitSshKeyCredentialsId',
2019-07-16 15:20:27 +02:00
/** */
2019-07-16 15:45:08 +02:00
'gitHttpsCredentialsId',
2019-03-28 11:46:19 +02:00
/**
* Allows to overwrite the global git setting 'user.email' available on your Jenkins server.
*/
2018-03-12 23:19:45 +02:00
'gitUserEMail',
2019-03-28 11:46:19 +02:00
/**
* Allows to overwrite the global git setting 'user.name' available on your Jenkins server.
*/
2018-03-12 23:19:45 +02:00
'gitUserName',
2019-03-28 11:46:19 +02:00
/**
* Defines the git ssh url to the source code repository. Used in conjunction with 'GitPushMode.SSH'.
* @mandatory for `gitPushMode` `SSH`
2019-03-28 11:46:19 +02:00
*/
2018-03-12 23:19:45 +02:00
'gitSshUrl',
/**
* Defines the git https url to the source code repository. Used in conjunction with 'GitPushMode.HTTPS'.
* @mandatory for `gitPushMode` `HTTPS`
*/
'gitHttpsUrl',
/**
* Disables the ssl verification for git push. Intended to be used only for troubleshooting. Productive usage is not recommanded.
*/
2019-07-16 15:20:27 +02:00
'gitDisableSslVerification',
2019-03-28 11:46:19 +02:00
/**
* Defines the prefix which is used for the git tag which is written during the versioning run.
*/
2018-03-12 23:19:45 +02:00
'tagPrefix',
2019-03-28 11:46:19 +02:00
/**
* Defines the timestamp to be used in the automatic version string. You could overwrite the default behavior by explicitly setting this string.
*/
2018-03-12 23:19:45 +02:00
'timestamp',
2019-03-28 11:46:19 +02:00
/** Defines the template for the timestamp which will be part of the created version. */
2018-03-12 23:19:45 +02:00
'timestampTemplate',
2019-03-28 11:46:19 +02:00
/** Defines the template for the automatic version which will be created. */
'versioningTemplate',
/** Controls which protocol is used for performing push operation to remote repo.
* Required credentials needs to be configured ('gitSshKeyCredentialsId'/'gitHttpsCredentialsId').
* Push is only performed in case 'commitVersion' is set to 'true'.
* @possibleValues 'SSH', 'HTTPS', 'NONE'
*/
2019-07-16 11:18:08 +02:00
'gitPushMode'
2018-03-12 23:19:45 +02:00
]
2019-03-28 11:46:19 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus(
/**
* Defines the version prefix of the automatically generated version. By default it will take the long commitId hash.
* You could pass any other string (e.g. the short commitId hash) to be used. In case you don't want to have the gitCommitId added to the automatic versioning string you could set the value to an empty string: `''`.
*/
'gitCommitId'
)
2019-03-28 11:46:19 +02:00
/**
* The continuous delivery process requires that each build is done with a unique version number.
*
* The version generated using this step will contain:
*
* * Version (major.minor.patch) from descriptor file in master repository is preserved. Developers should be able to autonomously decide on increasing either part of this version number.
* * Timestamp
* * CommitId (by default the long version of the hash)
*
* Optionally, but enabled by default, the new version is pushed as a new tag into the source code repository (e.g. GitHub).
* If this option is chosen, git credentials and the repository URL needs to be provided.
* Since you might not want to configure the git credentials in Jenkins, committing and pushing can be disabled using the `commitVersion` parameter as described below.
* If you require strict reproducibility of your builds, this should be used.
*/
@GenerateDocumentation
2018-08-30 16:33:07 +02:00
void call(Map parameters = [:], Closure body = null) {
2018-03-12 23:19:45 +02:00
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = checkScript(this, parameters)
def gitUtils = parameters.juStabGitUtils ?: new GitUtils()
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}"
}
if (script == null)
script = this
2018-03-12 23:19:45 +02:00
// load default & individual configuration
ConfigurationHelper configHelper = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.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)
.withMandatoryProperty('buildTool')
.dependingOn('buildTool').mixin('filePath')
.dependingOn('buildTool').mixin('versioningTemplate')
Map config = configHelper.use()
GitPushMode gitPushMode = config.gitPushMode
config = configHelper.addIfEmpty('timestamp', getTimestamp(config.timestampTemplate))
.use()
new Utils().pushToSWA([
step: STEP_NAME,
stepParamKey1: 'buildTool',
stepParam1: config.buildTool,
stepParamKey2: 'artifactType',
stepParam2: config.artifactType,
stepParamKey3: 'scriptMissing',
stepParam3: parameters?.script == null
], config)
def artifactVersioning = ArtifactVersioning.getArtifactVersioning(config.buildTool, script, config)
def currentVersion = artifactVersioning.getVersion()
def newVersion
if (config.artifactType == 'appContainer' && config.dockerVersionSource == 'appVersion'){
newVersion = currentVersion
} else {
def binding = [version: currentVersion, timestamp: config.timestamp, commitId: config.gitCommitId]
newVersion = new GStringTemplateEngine().createTemplate(config.versioningTemplate).make(binding).toString()
}
artifactVersioning.setVersion(newVersion)
if(body != null){
body(newVersion)
}
if (config.commitVersion) {
def gitConfig = []
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
git add .
git ${gitConfig} commit -m 'update version ${newVersion}'
git tag ${config.tagPrefix}${newVersion}"""
config.gitCommitId = gitUtils.getGitCommitIdOrNull()
} catch (e) {
2018-09-05 11:44:40 +02:00
error "[${STEP_NAME}]git commit and tag failed: ${e}"
}
if(gitPushMode == GitPushMode.SSH) {
config = ConfigurationHelper.newInstance(this, config)
.addIfEmpty('gitSshUrl', isAppContainer(config)
?script.commonPipelineEnvironment.getAppContainerProperty('gitSshUrl')
:script.commonPipelineEnvironment.getGitSshUrl())
.withMandatoryProperty('gitSshUrl')
.use()
sshagent([config.gitSshKeyCredentialsId]) {
sh "git push ${config.gitSshUrl} ${config.tagPrefix}${newVersion}"
}
} else if(gitPushMode == GitPushMode.HTTPS) {
config = ConfigurationHelper.newInstance(this, config)
.addIfEmpty('gitSshUrl', isAppContainer(config)
?script.commonPipelineEnvironment.getAppContainerProperty('gitHttpsUrl')
:script.commonPipelineEnvironment.getGitHttpsUrl())
.withMandatoryProperty('gitHttpsUrl')
.use()
withCredentials([usernamePassword(
2019-07-16 15:45:08 +02:00
credentialsId: config.gitHttpsCredentialsId,
2019-07-16 15:20:27 +02:00
passwordVariable: 'PASSWORD',
usernameVariable: 'USERNAME')]) {
// Problem: when username/password is encoded and in case the encoded version differs from
2019-07-16 16:46:04 +02:00
// the non-encoded version (e.g. '@' gets replaced by '%40') the encoded version
2019-07-16 15:20:27 +02:00
// it is not replaced by stars in the log by surrounding withCredentials.
// In order to avoid having the secrets in the log we take the following actions in case
// the encoded version(s) differs from the non-encoded versions
//
// 1.) we switch off '-x' in the hashbang
// 2.) we tell git push to be silent
// 3.) we send stderr to /dev/null
//
// Disadvantage: In this case we don't see any output for troubleshooting.
def USERNAME_ENCODED = URLEncoder.encode(USERNAME, 'UTF-8'),
PASSWORD_ENCODED = URLEncoder.encode(PASSWORD, 'UTF-8')
boolean encodedVersionsDiffers = USERNAME_ENCODED != USERNAME || PASSWORD_ENCODED != PASSWORD
def prefix = 'https://'
def gitUrlWithCredentials = config.gitHttpsUrl.replaceAll("^${prefix}", "${prefix}${USERNAME_ENCODED}:${PASSWORD_ENCODED}@")
def hashbangFlags = '-xe'
def gitPushFlags = []
def streamhandling = ''
def gitDebug = ''
gitConfig = []
if(config.gitHttpProxy) {
gitConfig.add("-c http.proxy=\"${config.gitHttpProxy}\"")
}
if(config.gitDisableSslVerification) {
echo 'git ssl verification is switched off. This setting is not recommanded in productive environments.'
gitConfig.add('-c http.sslVerify=false')
}
if(encodedVersionsDiffers) {
2019-07-16 16:01:43 +02:00
if(config.verbose) { // known issue: in case somebody provides the stringish 'false' we get the boolean value 'true' here.
echo 'Verbose flag set, but encoded username/password differs from unencoded version. Cannot provide verbose output in this case. ' +
'In order to enable verbose output switch to a username/password which is not altered by url encoding.'
2019-07-16 15:20:27 +02:00
}
hashbangFlags = '-e'
streamhandling ='&>/dev/null'
gitPushFlags.add('--quiet')
echo 'Performing git push in quiet mode.'
} else {
2019-07-16 16:01:43 +02:00
if(config.verbose) { // known issue: in case somebody provides the stringish 'false' we get the boolean value 'true' here.
echo 'Verbose mode enabled. This is not recommanded for productive usage. This might reveal security sensitive information.'
2019-07-16 15:20:27 +02:00
gitDebug ='git config --list; env |grep proxy; GIT_CURL_VERBOSE=1 GIT_TRACE=1 '
gitPushFlags.add('--verbose')
}
}
gitConfig = gitConfig.join(' ')
gitPushFlags = gitPushFlags.join(' ')
2019-07-16 15:36:52 +02:00
sh script: """|#!/bin/bash ${hashbangFlags}
|${gitDebug}git ${gitConfig} push ${gitPushFlags} ${gitUrlWithCredentials} ${config.tagPrefix}${newVersion} ${streamhandling}""".stripMargin()
2019-07-16 15:20:27 +02:00
}
} else {
echo "Git push mode: ${gitPushMode.toString()}. Git push to remote has been skipped."
}
}
if (isAppContainer(config)) {
script.commonPipelineEnvironment.setAppContainerProperty('artifactVersion', newVersion)
script.commonPipelineEnvironment.setAppContainerProperty('gitCommitId', config.gitCommitId)
} else {
//standard case
script.commonPipelineEnvironment.setArtifactVersion(newVersion)
script.commonPipelineEnvironment.setGitCommitId(config.gitCommitId)
}
2018-03-12 23:19:45 +02:00
echo "[${STEP_NAME}]New version: ${newVersion}"
}
}
def isAppContainer(config){
return config.buildTool == 'docker' && config.artifactType == 'appContainer'
}
def getTimestamp(pattern){
return sh(returnStdout: true, script: "date --utc +'${pattern}'").trim()
}