2020-10-05 12:50:03 +02:00
|
|
|
import com.cloudbees.groovy.cps.NonCPS
|
|
|
|
|
2018-09-21 16:55:31 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
2019-04-03 11:13:08 +02:00
|
|
|
import com.sap.piper.GenerateDocumentation
|
2018-08-06 08:57:36 +02:00
|
|
|
import com.sap.piper.ConfigurationHelper
|
|
|
|
import com.sap.piper.Utils
|
2019-04-11 11:39:41 +02:00
|
|
|
import com.sap.piper.analytics.InfluxData
|
2022-07-12 10:25:17 +02:00
|
|
|
import com.sap.piper.GitUtils
|
2019-04-11 11:39:41 +02:00
|
|
|
|
2018-08-06 08:57:36 +02:00
|
|
|
import groovy.transform.Field
|
|
|
|
|
2018-11-29 10:54:05 +02:00
|
|
|
@Field String STEP_NAME = getClass().getName()
|
2018-08-06 08:57:36 +02:00
|
|
|
|
2019-04-03 11:13:08 +02:00
|
|
|
@Field Set GENERAL_CONFIG_KEYS = [
|
|
|
|
/** */
|
2020-06-18 12:39:55 +02:00
|
|
|
'collectTelemetryData',
|
|
|
|
|
|
|
|
/** Credentials (username and password) used to download custom defaults if access is secured.*/
|
2020-09-04 14:45:09 +02:00
|
|
|
'customDefaultsCredentialsId',
|
|
|
|
|
|
|
|
/** Enable automatic inference of build tool (maven, npm, mta) based on existing project files.
|
|
|
|
* If this is set to true, it is not required to set the build tool by hand for those cases.
|
|
|
|
*/
|
|
|
|
'inferBuildTool'
|
2019-04-03 11:13:08 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
@Field Set STEP_CONFIG_KEYS = []
|
|
|
|
|
|
|
|
@Field Set PARAMETER_KEYS = [
|
2020-05-12 13:50:18 +02:00
|
|
|
/** Path to the pipeline configuration file defining project specific settings.*/
|
|
|
|
'configFile',
|
|
|
|
/** A list of file names which will be extracted from library resources and which serve as source for
|
|
|
|
* default values for the pipeline configuration. These are merged with and override built-in defaults, with
|
|
|
|
* a parameter supplied by the last resource file taking precedence over the same parameter supplied in an
|
|
|
|
* earlier resource file or built-in default.*/
|
|
|
|
'customDefaults',
|
|
|
|
/** A list of file paths or URLs which must point to YAML content. These work exactly like
|
|
|
|
* `customDefaults`, but from local or remote files instead of library resources. They are merged with and
|
|
|
|
* take precedence over `customDefaults`.*/
|
2020-10-05 12:50:03 +02:00
|
|
|
'customDefaultsFromFiles',
|
2020-11-11 11:44:49 +02:00
|
|
|
/** The map returned from a Jenkins git checkout. Used to set the git information in the
|
|
|
|
* common pipeline environment */
|
|
|
|
'scmInfo'
|
2019-04-03 11:13:08 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the [`commonPipelineEnvironment`](commonPipelineEnvironment.md), which is used throughout the complete pipeline.
|
|
|
|
*
|
|
|
|
* !!! tip
|
2020-03-04 18:01:49 +02:00
|
|
|
* This step needs to run at the beginning of a pipeline right after the SCM checkout.
|
|
|
|
* Then subsequent pipeline steps consume the information from `commonPipelineEnvironment`; it does not need to be passed to pipeline steps explicitly.
|
2019-04-03 11:13:08 +02:00
|
|
|
*/
|
|
|
|
@GenerateDocumentation
|
2018-08-30 16:33:07 +02:00
|
|
|
void call(Map parameters = [:]) {
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-08-09 11:35:33 +02:00
|
|
|
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-09-21 16:55:31 +02:00
|
|
|
def script = checkScript(this, parameters)
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
String configFile = parameters.get('configFile')
|
|
|
|
loadConfigurationFromFile(script, configFile)
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
// Copy custom defaults from library resources to include them in the 'pipelineConfigAndTests' stash
|
|
|
|
List customDefaultsResources = Utils.appendParameterToStringList(
|
|
|
|
['default_pipeline_environment.yml'], parameters, 'customDefaults')
|
|
|
|
customDefaultsResources.each {
|
2020-03-17 10:19:09 +02:00
|
|
|
cd ->
|
|
|
|
writeFile file: ".pipeline/${cd}", text: libraryResource(cd)
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
List customDefaultsFiles = Utils.appendParameterToStringList(
|
|
|
|
[], parameters, 'customDefaultsFromFiles')
|
|
|
|
|
|
|
|
if (script.commonPipelineEnvironment.configuration.customDefaults) {
|
|
|
|
if (!script.commonPipelineEnvironment.configuration.customDefaults in List) {
|
|
|
|
// Align with Go side on supported parameter type.
|
|
|
|
error "You have defined the parameter 'customDefaults' in your project configuration " +
|
|
|
|
"but it is of an unexpected type. Please make sure that it is a list of strings, i.e. " +
|
|
|
|
"customDefaults = ['...']. See https://sap.github.io/jenkins-library/configuration/ for " +
|
|
|
|
"more details."
|
|
|
|
}
|
2020-05-14 10:50:58 +02:00
|
|
|
customDefaultsFiles = Utils.appendParameterToStringList(
|
|
|
|
customDefaultsFiles, script.commonPipelineEnvironment.configuration as Map, 'customDefaults')
|
2020-05-12 13:50:18 +02:00
|
|
|
}
|
2020-06-18 12:39:55 +02:00
|
|
|
String customDefaultsCredentialsId = script.commonPipelineEnvironment.configuration.general?.customDefaultsCredentialsId
|
|
|
|
customDefaultsFiles = copyOrDownloadCustomDefaultsIntoPipelineEnv(script, customDefaultsFiles, customDefaultsCredentialsId)
|
2020-04-08 16:00:02 +02:00
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
prepareDefaultValues([
|
|
|
|
script: script,
|
|
|
|
customDefaults: parameters.customDefaults,
|
2020-05-14 10:50:58 +02:00
|
|
|
customDefaultsFromFiles: customDefaultsFiles ])
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2020-06-22 17:52:11 +02:00
|
|
|
piperLoadGlobalExtensions script: script, customDefaults: parameters.customDefaults, customDefaultsFromFiles: customDefaultsFiles
|
|
|
|
|
2020-12-01 13:04:27 +02:00
|
|
|
String stashIncludes = '.pipeline/**'
|
|
|
|
if (configFile && !configFile.startsWith('.pipeline/')) {
|
|
|
|
stashIncludes += ", $configFile"
|
|
|
|
}
|
|
|
|
stash name: 'pipelineConfigAndTests', includes: stashIncludes, allowEmpty: true
|
2018-08-06 08:57:36 +02:00
|
|
|
|
2018-10-17 11:05:20 +02:00
|
|
|
Map config = ConfigurationHelper.newInstance(this)
|
2018-09-07 10:08:16 +02:00
|
|
|
.loadStepDefaults()
|
2018-08-06 08:57:36 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
|
|
|
.use()
|
|
|
|
|
2020-09-04 14:45:09 +02:00
|
|
|
inferBuildTool(script, config)
|
|
|
|
|
2019-01-21 09:47:34 +02:00
|
|
|
(parameters.utils ?: new Utils()).pushToSWA([
|
|
|
|
step: STEP_NAME,
|
|
|
|
stepParamKey4: 'customDefaults',
|
2019-03-26 15:13:03 +02:00
|
|
|
stepParam4: parameters.customDefaults?'true':'false'
|
2019-01-21 09:47:34 +02:00
|
|
|
], config)
|
2019-03-21 21:23:23 +02:00
|
|
|
|
2019-04-11 11:39:41 +02:00
|
|
|
InfluxData.addField('step_data', 'build_url', env.BUILD_URL)
|
|
|
|
InfluxData.addField('pipeline_data', 'build_url', env.BUILD_URL)
|
2020-10-05 12:50:03 +02:00
|
|
|
|
2020-11-11 11:44:49 +02:00
|
|
|
def scmInfo = parameters.scmInfo
|
|
|
|
if (scmInfo) {
|
|
|
|
setGitUrlsOnCommonPipelineEnvironment(script, scmInfo.GIT_URL)
|
|
|
|
script.commonPipelineEnvironment.setGitCommitId(scmInfo.GIT_COMMIT)
|
2022-07-12 10:25:17 +02:00
|
|
|
|
|
|
|
def gitUtils = parameters.gitUtils ?: new GitUtils()
|
|
|
|
setGitRefOnCommonPipelineEnvironment(script, scmInfo.GIT_COMMIT, scmInfo.GIT_BRANCH, gitUtils)
|
2020-10-05 12:50:03 +02:00
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 14:45:09 +02:00
|
|
|
|
|
|
|
// Infer build tool (maven, npm, mta) based on existing build descriptor files in the project root.
|
|
|
|
private static void inferBuildTool(script, config) {
|
|
|
|
// For backwards compatibility, build tool inference must be enabled via inferBuildTool setting
|
|
|
|
boolean inferBuildTool = config?.inferBuildTool
|
|
|
|
|
|
|
|
if (inferBuildTool) {
|
|
|
|
boolean isMtaProject = script.fileExists('mta.yaml')
|
|
|
|
def isMavenProject = script.fileExists('pom.xml')
|
|
|
|
def isNpmProject = script.fileExists('package.json')
|
|
|
|
|
|
|
|
if (isMtaProject) {
|
|
|
|
script.commonPipelineEnvironment.buildTool = 'mta'
|
|
|
|
} else if (isMavenProject) {
|
|
|
|
script.commonPipelineEnvironment.buildTool = 'maven'
|
|
|
|
} else if (isNpmProject) {
|
|
|
|
script.commonPipelineEnvironment.buildTool = 'npm'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
private static loadConfigurationFromFile(script, String configFile) {
|
2020-03-30 14:31:24 +02:00
|
|
|
if (!configFile) {
|
|
|
|
String defaultYmlConfigFile = '.pipeline/config.yml'
|
|
|
|
String defaultYamlConfigFile = '.pipeline/config.yaml'
|
2020-05-12 13:50:18 +02:00
|
|
|
if (script.fileExists(defaultYmlConfigFile)) {
|
2020-03-30 14:31:24 +02:00
|
|
|
configFile = defaultYmlConfigFile
|
2020-05-12 13:50:18 +02:00
|
|
|
} else if (script.fileExists(defaultYamlConfigFile)) {
|
2020-03-30 14:31:24 +02:00
|
|
|
configFile = defaultYamlConfigFile
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2020-03-30 14:31:24 +02:00
|
|
|
// A file passed to the function is not checked for existence in order to fail the pipeline.
|
2019-03-26 15:13:03 +02:00
|
|
|
if (configFile) {
|
2020-05-12 13:50:18 +02:00
|
|
|
script.commonPipelineEnvironment.configuration = script.readYaml(file: configFile)
|
2020-03-30 14:31:24 +02:00
|
|
|
script.commonPipelineEnvironment.configurationFile = configFile
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:50:18 +02:00
|
|
|
|
2020-06-18 12:39:55 +02:00
|
|
|
private static List copyOrDownloadCustomDefaultsIntoPipelineEnv(script, List customDefaults, String credentialsId) {
|
2020-05-12 13:50:18 +02:00
|
|
|
List fileList = []
|
|
|
|
int urlCount = 0
|
|
|
|
for (int i = 0; i < customDefaults.size(); i++) {
|
|
|
|
// copy retrieved file to .pipeline/ to make sure they are in the pipelineConfigAndTests stash
|
2020-05-18 10:59:02 +02:00
|
|
|
if (!(customDefaults[i] in CharSequence) || customDefaults[i] == '') {
|
|
|
|
script.echo "WARNING: Ignoring invalid entry in custom defaults from files: '${customDefaults[i]}'"
|
|
|
|
continue
|
|
|
|
}
|
2020-05-12 13:50:18 +02:00
|
|
|
String fileName
|
|
|
|
if (customDefaults[i].startsWith('http://') || customDefaults[i].startsWith('https://')) {
|
|
|
|
fileName = "custom_default_from_url_${urlCount}.yml"
|
|
|
|
|
2020-06-18 12:39:55 +02:00
|
|
|
Map httpRequestParameters = [
|
2020-05-12 13:50:18 +02:00
|
|
|
url: customDefaults[i],
|
|
|
|
validResponseCodes: '100:399,404' // Allow a more specific error message for 404 case
|
2020-06-18 12:39:55 +02:00
|
|
|
]
|
|
|
|
if (credentialsId) {
|
|
|
|
httpRequestParameters.authentication = credentialsId
|
|
|
|
}
|
|
|
|
def response = script.httpRequest(httpRequestParameters)
|
2020-05-12 13:50:18 +02:00
|
|
|
if (response.status == 404) {
|
|
|
|
error "URL for remote custom defaults (${customDefaults[i]}) appears to be incorrect. " +
|
|
|
|
"Server returned HTTP status code 404. " +
|
|
|
|
"Please make sure that the path is correct and no authentication is required to retrieve the file."
|
|
|
|
}
|
|
|
|
|
|
|
|
script.writeFile file: ".pipeline/$fileName", text: response.content
|
|
|
|
urlCount++
|
|
|
|
} else if (script.fileExists(customDefaults[i])) {
|
|
|
|
fileName = customDefaults[i]
|
|
|
|
script.writeFile file: ".pipeline/$fileName", text: script.readFile(file: fileName)
|
|
|
|
} else {
|
|
|
|
script.echo "WARNING: Custom default entry not found: '${customDefaults[i]}', it will be ignored"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fileList.add(fileName)
|
|
|
|
}
|
|
|
|
return fileList
|
|
|
|
}
|
2020-10-05 12:50:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the parts of an url.
|
|
|
|
* Valid keys for the retured map are:
|
|
|
|
* - protocol
|
|
|
|
* - auth
|
|
|
|
* - host
|
|
|
|
* - port
|
|
|
|
* - path
|
|
|
|
*/
|
|
|
|
@NonCPS
|
|
|
|
/* private */ Map parseUrl(String url) {
|
|
|
|
|
|
|
|
def urlMatcher = url =~ /^((http|https|git|ssh):\/\/)?((.*)@)?([^:\/]+)(:([\d]*))?(\/?(.*))$/
|
|
|
|
|
|
|
|
return [
|
|
|
|
protocol: urlMatcher[0][2],
|
|
|
|
auth: urlMatcher[0][4],
|
|
|
|
host: urlMatcher[0][5],
|
|
|
|
port: urlMatcher[0][7],
|
|
|
|
path: urlMatcher[0][9],
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setGitUrlsOnCommonPipelineEnvironment(script, String gitUrl) {
|
|
|
|
|
|
|
|
Map url = parseUrl(gitUrl)
|
|
|
|
|
|
|
|
if (url.protocol in ['http', 'https']) {
|
|
|
|
script.commonPipelineEnvironment.setGitSshUrl("git@${url.host}:${url.path}")
|
|
|
|
script.commonPipelineEnvironment.setGitHttpsUrl(gitUrl)
|
|
|
|
} else if (url.protocol in [ null, 'ssh', 'git']) {
|
|
|
|
script.commonPipelineEnvironment.setGitSshUrl(gitUrl)
|
|
|
|
script.commonPipelineEnvironment.setGitHttpsUrl("https://${url.host}/${url.path}")
|
|
|
|
}
|
|
|
|
|
|
|
|
List gitPathParts = url.path.replaceAll('.git', '').split('/')
|
|
|
|
def gitFolder = 'N/A'
|
|
|
|
def gitRepo = 'N/A'
|
|
|
|
switch (gitPathParts.size()) {
|
2020-11-03 13:50:00 +02:00
|
|
|
case 0:
|
|
|
|
break
|
2020-10-05 12:50:03 +02:00
|
|
|
case 1:
|
|
|
|
gitRepo = gitPathParts[0]
|
|
|
|
break
|
|
|
|
case 2:
|
|
|
|
gitFolder = gitPathParts[0]
|
|
|
|
gitRepo = gitPathParts[1]
|
|
|
|
break
|
2020-11-03 13:50:00 +02:00
|
|
|
default:
|
2020-10-05 12:50:03 +02:00
|
|
|
gitRepo = gitPathParts[gitPathParts.size()-1]
|
|
|
|
gitPathParts.remove(gitPathParts.size()-1)
|
|
|
|
gitFolder = gitPathParts.join('/')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
script.commonPipelineEnvironment.setGithubOrg(gitFolder)
|
|
|
|
script.commonPipelineEnvironment.setGithubRepo(gitRepo)
|
|
|
|
}
|
2022-06-24 09:04:24 +02:00
|
|
|
|
2022-07-12 10:25:17 +02:00
|
|
|
private void setGitRefOnCommonPipelineEnvironment(script, String gitCommit, String gitBranch, def gitUtils) {
|
2022-06-24 09:04:24 +02:00
|
|
|
if(!gitBranch){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(gitBranch.contains("/")){
|
|
|
|
gitBranch = gitBranch.split("/")[1]
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:25:17 +02:00
|
|
|
if (!gitBranch.contains("PR")) {
|
|
|
|
script.commonPipelineEnvironment.setGitRef("refs/heads/" + gitBranch)
|
|
|
|
script.commonPipelineEnvironment.setGitRemoteCommitId(gitCommit)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isMergeCommit = gitUtils.isMergeCommit()
|
|
|
|
def mergeOrHead = isMergeCommit?"merge":"head"
|
|
|
|
def changeId = gitBranch.split("-")[1]
|
|
|
|
script.commonPipelineEnvironment.setGitRef("refs/pull/" + changeId + "/" + mergeOrHead)
|
|
|
|
|
|
|
|
if(!isMergeCommit){
|
|
|
|
script.commonPipelineEnvironment.setGitRemoteCommitId(gitCommit)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
String gitRemoteCommitId = gitUtils.getGitMergeCommitId(changeId)
|
|
|
|
if(gitRemoteCommitId?.trim() && gitUtils.compareParentsOfMergeAndHead(gitRemoteCommitId)){
|
|
|
|
script.commonPipelineEnvironment.setGitRemoteCommitId(gitRemoteCommitId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
echo "Exception in getting git merge commit id or comparing git merge commit parents: ${e}"
|
|
|
|
}
|
|
|
|
|
|
|
|
script.commonPipelineEnvironment.setGitRemoteCommitId("NA")
|
2022-06-24 09:04:24 +02:00
|
|
|
}
|