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

Merge remote-tracking branch 'github/master' into HEAD

This commit is contained in:
Marcus Holl 2019-04-05 11:19:24 +02:00
commit a92c01b640
3 changed files with 40 additions and 7 deletions

View File

@ -227,6 +227,7 @@ steps:
libraryDocumentationUrl: 'https://sap.github.io/jenkins-library/'
libraryRepositoryUrl: 'https://github.com/SAP/jenkins-library/'
mandatorySteps: []
stepTimeouts: {}
healthExecuteCheck:
healthEndpoint: ''
influxWriteData:

View File

@ -141,4 +141,24 @@ class HandlePipelineStepErrorsTest extends BasePiperTest {
}
assertThat(errorOccured, is(false))
}
@Test
void testHandleErrorsTimeout() {
def timeout = 0
helper.registerAllowedMethod('timeout', [Map.class, Closure.class], {m, body ->
timeout = m.time
throw new org.jenkinsci.plugins.workflow.steps.FlowInterruptedException(hudson.model.Result.ABORTED, new jenkins.model.CauseOfInterruption.UserInterruption('Test'))
})
stepRule.step.handlePipelineStepErrors([
stepName: 'test',
stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
failOnError: false,
stepTimeouts: [test: 10]
]) {
//do something
}
assertThat(timeout, is(10))
assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
}
}

View File

@ -7,6 +7,8 @@ import groovy.text.SimpleTemplateEngine
import groovy.transform.Field
import hudson.AbortException
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
@Field STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = []
@ -17,7 +19,12 @@ import hudson.AbortException
*/
'failOnError',
/** Defines a list of mandatory steps (step names) which have to be successful (=stop the pipeline), even if `failOnError: false` */
'mandatorySteps'
'mandatorySteps',
/**
* Defines a Map containing step name as key and timout in minutes in order to stop an execution after a certain timeout.
* This helps to make pipeline runs more resilient with respect to long running steps.
* */
'stepTimeouts'
])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus([
/**
@ -59,14 +66,19 @@ void call(Map parameters = [:], body) {
try {
if (config.echoDetails)
echo "--- Begin library step of: ${config.stepName} ---"
body()
} catch (AbortException ae) {
if (!config.failOnError && config.stepTimeouts?.get(config.stepName)) {
timeout(time: config.stepTimeouts[config.stepName]) {
body()
}
} else {
body()
}
} catch (AbortException | FlowInterruptedException ex) {
if (config.echoDetails)
message += formatErrorMessage(config, ae)
writeErrorToInfluxData(config, ae)
message += formatErrorMessage(config, ex)
writeErrorToInfluxData(config, ex)
if (config.failOnError || config.stepName in config.mandatorySteps) {
throw ae
throw ex
}
if (config.stepParameters?.script) {
config.stepParameters?.script.currentBuild.result = 'UNSTABLE'