1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/vars/mavenExecute.groovy

140 lines
4.8 KiB
Groovy
Raw Normal View History

import static com.sap.piper.Prerequisites.checkScript
2019-04-01 11:22:28 +02:00
import com.sap.piper.GenerateDocumentation
import com.sap.piper.ConfigurationHelper
import com.sap.piper.Utils
import groovy.transform.Field
import static com.sap.piper.Utils.downloadSettingsFromUrl
@Field def STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = [
2019-04-01 11:22:28 +02:00
/** @see dockerExecute */
'dockerImage',
2019-04-01 11:22:28 +02:00
/** Path or url to the mvn settings file that should be used as global settings file.*/
'globalSettingsFile',
2019-04-01 11:22:28 +02:00
/** Path or url to the mvn settings file that should be used as project settings file.*/
'projectSettingsFile',
2019-04-01 11:22:28 +02:00
/** Path to the pom file that should be used.*/
'pomPath',
2019-04-01 11:22:28 +02:00
/** Path to the location of the local repository that should be used.*/
'm2Path'
]
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus([
2019-04-01 11:22:28 +02:00
/** @see dockerExecute */
'dockerOptions',
2019-04-01 11:22:28 +02:00
/** Flags to provide when running mvn.*/
'flags',
2019-04-01 11:22:28 +02:00
/** Maven goals that should be executed.*/
'goals',
2019-04-01 11:22:28 +02:00
/** Additional properties.*/
'defines',
2019-04-01 11:22:28 +02:00
/**
* Configures maven to log successful downloads. This is set to `false` by default to reduce the noise in build logs.
* @possibleValues `true`, `false`
*/
'logSuccessfulMavenTransfers',
/**
* Returns the standard output of the maven command for further processing. By default stdout will be printed to the console but not returned by mavenExecute.
* @possibleValues `true`, `false`
*/
'returnStdout'
])
2019-04-01 11:22:28 +02:00
/**
* Executes a maven command inside a Docker container.
*/
@GenerateDocumentation
def call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this
String commandOutput = ''
// load default & individual configuration
Map configuration = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName ?: env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.use()
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
new Utils().pushToSWA([
step : STEP_NAME,
stepParamKey1: 'scriptMissing',
stepParam1 : parameters?.script == null
], configuration)
String command = "mvn"
String globalSettingsFile = configuration.globalSettingsFile?.trim()
if (globalSettingsFile) {
if (globalSettingsFile.startsWith("http")) {
globalSettingsFile = downloadSettingsFromUrl(this, globalSettingsFile, 'global-settings.xml')
}
command += " --global-settings '${globalSettingsFile}'"
}
String m2Path = configuration.m2Path
if (m2Path?.trim()) {
command += " -Dmaven.repo.local='${m2Path}'"
}
String projectSettingsFile = configuration.projectSettingsFile?.trim()
if (projectSettingsFile) {
if (projectSettingsFile.startsWith("http")) {
projectSettingsFile = downloadSettingsFromUrl(this, projectSettingsFile, 'project-settings.xml')
}
command += " --settings '${projectSettingsFile}'"
}
String pomPath = configuration.pomPath
if (pomPath?.trim()) {
command += " --file '${pomPath}'"
}
String mavenFlags = configuration.flags
if (mavenFlags?.trim()) {
command += " ${mavenFlags}"
}
// Always use Maven's batch mode
if (!(command =~ /--batch-mode|-B(?=\s)|-B\\|-B$/)) {
command += ' --batch-mode'
}
// Disable log for successful transfers by default. Note this requires the batch-mode flag.
final String disableSuccessfulMavenTransfersLogFlag = ' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'
if (!configuration.logSuccessfulMavenTransfers) {
if (!command.contains(disableSuccessfulMavenTransfersLogFlag)) {
command += disableSuccessfulMavenTransfersLogFlag
}
}
def mavenGoals = configuration.goals
if (mavenGoals?.trim()) {
command += " ${mavenGoals}"
}
def defines = configuration.defines
if (defines?.trim()) {
command += " ${defines}"
}
Enable support for executing on K8S as a step (#231) * Create executeDockerOnKubernetes.groovy * Update dockerExecute.groovy * Create SysEnvTest.groovy * Update default_pipeline_environment.yml * Update executeDockerOnKubernetes.groovy * Create utils object * update docker image * Update mavenExecute.groovy * Use pipeline-lib than piper * Check container name * Always change ownership to 1000 * Check for map * Fix command * Move chmod to docker execute * Use generic name for the pod * runAsPod has been added * Return false if script has no k8smapping * fix syntax error * Null checks * Returnn dockerImage name * Check method body * Return container name * Cleanup echos * Use runAsPod * Rename step * Use official jenkins JNLP agent image * Construct containersMap * Check if kubernetes plugin is active * Support JaaS * pass script object * Move configuration to default section * Use generic flag to check if running in k8s * fix jnlp agent name * Solve travis errors * Improvements to config and changes to name of the method * Improvements to config * Fix type * Rename stash config * add import * Fix map order * Fix jnlp agent name * cleanup config usage * Check if config is enabled * Use nested k8s mapping * Support custom docker workspace and move flag to env * Feature/k8s stage (#1) * Use nested k8s mapping * Support custom docker workspace and move flag to env * Check dockerOptions value * Support local execution * Add tests for dockerExecute * Move config to step and Fix tests * Use step configuration while running as a pod * Streamline parameter and config initialization * Streamline parameter and tests * Cleanup and align variable name * Use default JNLP agent if one not defined in config * Add tests for runInsidePod. Ensure lowercase container names. * Improve tests and remove unused code block * Fix permission issues * Perform stashing and unstashing inside container * Use custom jnlp agent due to user id restriction * Fix tests after jnlp agent change * Address review comments * Initialize script to default value if null * Address review comments * Update exeception handling and documentation * Improve documentation * correct indent * Link documents to the index page * Merge containerExecute and dockerExecuteOnKuberenetes step and address comments. * Update dockerExecute.md * Update dockerExecuteOnKubernetes.md * Update default_pipeline_environment.yml * update documentation * Update documentation. Use annotation for singleton * Update DockerExecuteOnKubernetesTest.groovy * Update dockerExecute.groovy * Update dockerExecuteOnKubernetes.groovy * Improve documentation and test case names * neoDeploy: switch to chained ConfigurationHelper (#244) * switch neoDeploy to chained ConfigurationHelper * update imports * Improve tests * Address review comments * Improve documentation * made dockerImage non-mandatory parm, improved test * add comment regarding userid assumption
2018-08-21 15:45:59 +02:00
dockerExecute(script: script, dockerImage: configuration.dockerImage, dockerOptions: configuration.dockerOptions) {
if (configuration.returnStdout) {
commandOutput = sh(returnStdout: true, script: command)
} else {
sh command
}
Enable support for executing on K8S as a step (#231) * Create executeDockerOnKubernetes.groovy * Update dockerExecute.groovy * Create SysEnvTest.groovy * Update default_pipeline_environment.yml * Update executeDockerOnKubernetes.groovy * Create utils object * update docker image * Update mavenExecute.groovy * Use pipeline-lib than piper * Check container name * Always change ownership to 1000 * Check for map * Fix command * Move chmod to docker execute * Use generic name for the pod * runAsPod has been added * Return false if script has no k8smapping * fix syntax error * Null checks * Returnn dockerImage name * Check method body * Return container name * Cleanup echos * Use runAsPod * Rename step * Use official jenkins JNLP agent image * Construct containersMap * Check if kubernetes plugin is active * Support JaaS * pass script object * Move configuration to default section * Use generic flag to check if running in k8s * fix jnlp agent name * Solve travis errors * Improvements to config and changes to name of the method * Improvements to config * Fix type * Rename stash config * add import * Fix map order * Fix jnlp agent name * cleanup config usage * Check if config is enabled * Use nested k8s mapping * Support custom docker workspace and move flag to env * Feature/k8s stage (#1) * Use nested k8s mapping * Support custom docker workspace and move flag to env * Check dockerOptions value * Support local execution * Add tests for dockerExecute * Move config to step and Fix tests * Use step configuration while running as a pod * Streamline parameter and config initialization * Streamline parameter and tests * Cleanup and align variable name * Use default JNLP agent if one not defined in config * Add tests for runInsidePod. Ensure lowercase container names. * Improve tests and remove unused code block * Fix permission issues * Perform stashing and unstashing inside container * Use custom jnlp agent due to user id restriction * Fix tests after jnlp agent change * Address review comments * Initialize script to default value if null * Address review comments * Update exeception handling and documentation * Improve documentation * correct indent * Link documents to the index page * Merge containerExecute and dockerExecuteOnKuberenetes step and address comments. * Update dockerExecute.md * Update dockerExecuteOnKubernetes.md * Update default_pipeline_environment.yml * update documentation * Update documentation. Use annotation for singleton * Update DockerExecuteOnKubernetesTest.groovy * Update dockerExecute.groovy * Update dockerExecuteOnKubernetes.groovy * Improve documentation and test case names * neoDeploy: switch to chained ConfigurationHelper (#244) * switch neoDeploy to chained ConfigurationHelper * update imports * Improve tests * Address review comments * Improve documentation * made dockerImage non-mandatory parm, improved test * add comment regarding userid assumption
2018-08-21 15:45:59 +02:00
}
}
if (configuration.returnStdout) {
return commandOutput
}
}