Gist này (không phải của tôi) là một trong những tùy chọn tốt hơn mà tôi tìm thấy trong khi cố gắng triển khai chức năng này. https://gist.github.com/beercan1989/b66b7643b48434f5bdf7e1c87094acb9
Đã thay đổi nó thành phương thức trong thư viện được chia sẻ vừa thử lại hoặc hủy bỏ các nhu cầu của tôi. Cũng đã thêm một lần thử lại tối đa và thực hiện biến thời gian chờ để chúng tôi có thể thay đổi tùy thuộc vào công việc hoặc giai đoạn cần nó.
package com.foo.bar.jenkins
def class PipelineHelper {
def steps
PipelineHelper(steps) {
this.steps = steps
}
void retryOrAbort(final Closure<?> action, int maxAttempts, int timeoutSeconds, final int count = 0) {
steps.echo "Trying action, attempt count is: ${count}"
try {
action.call();
} catch (final exception) {
steps.echo "${exception.toString()}"
steps.timeout(time: timeoutSeconds, unit: 'SECONDS') {
def userChoice = false
try {
userChoice = steps.input(message: 'Retry?', ok: 'Ok', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Check to retry from failed stage']])
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
userChoice = false
}
if (userChoice) {
if (count <= maxAttempts) {
steps.echo "Retrying from failed stage."
return retryOrAbort(action, maxAttempts, timeoutMinutes, count + 1)
} else {
steps.echo "Max attempts reached. Will not retry."
throw exception
}
} else {
steps.echo 'Aborting'
throw exception;
}
}
}
}
}
Ví dụ sử dụng với tối đa 2 lần thử lại chờ 60 giây cho đầu vào.
def pipelineHelper = new PipelineHelper(this)
stage ('Retry Example'){
pipelineHelper.retryOrAbort({
node{
echo 'Here is an example'
throw new RuntimeException('This example will fail.')
}
}, 2, 60)
}
Chỉ cần nhớ đặt các nút bên trong phần đóng để đợi đầu vào không chặn trình thực thi.
Nếu bạn có doanh nghiệp jenkins trả tiền, Cloudbees có plugin Checkpoint có thể xử lý tốt hơn, nhưng nó không được lên kế hoạch phát hành cho mã nguồn mở Jenkins (JENKINS-33846).
Yêu cầu tính năng tổng thể này là [JENKINS-33846] (https://issues.jenkins-ci.org/browse/JENKINS-33846). Nó (dissapointingly) chỉ được chọn cho các đường ống khai báo trong [JENKINS-45455] (https://issues.jenkins-ci.org/browse/JENKINS-45455). – mkobit