Issue
Now I want to add a timeout input in jenkins pipeline, if 60 seconds the user not input the choose, it will jump the next steps and mark build success. If the user choose ''YES, the next steps will be executed. this is my code:
stage('push-image-uat') {
timeout(time: 60, unit: 'SECONDS') {
input {
message "Publish to uat environment?"
ok "Yes, we should."
submitter "admin,anthony"
parameters {
string(name: 'CHOOSE', defaultValue: 'NO', description: 'Should I publish to uat?')
}
}
}
when {
expression {
${params.CHOOSE} == 'YES'
}
}
steps{
script {
publishToUATEnvironment = true
}
sh "echo \"push image uat\""
echo "Hello, ${CHOOSE}, nice to meet you."
}
}
but when I run this script in jenkins, it thows this error:
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 57: Unknown stage section "timeout". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 57, column 9.
stage('push-image-uat') {
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
what should I do to fix this error?
Solution
It should be like this
stage('push-image-uat') {
steps {
timeout(time: 60, unit: 'SECONDS') {
input {
message "Publish to uat environment?"
ok "Yes, we should."
submitter "admin,anthony"
parameters {
string(name: 'CHOOSE', defaultValue: 'NO', description: 'Should I publish to uat?')
}
}
}
}
}
The timeout block
must be within steps block
and for Best practices always go with this flow for Declarative pipeline
pipeline {
stages {
stage(""){
steps {
script {
}
}
}
}
}
You can use try catch or timeout etc.
blocks within script block
Answered By - Dashrath Mundkar
Answer Checked By - Clifford M. (JavaFixing Volunteer)