需要帮助来确定一种方式,使我们可以获取前一个作业的执行时间总和,以作为詹金斯管道中下一个作业的输入。 说,P是具有n个作业的管道
J1-> J2-> J3-> J4
我想提供执行时间的J4输入(J1 + J2 + J3) 谁能帮助我解决问题。
答案 0 :(得分:1)
如果必须执行类似的操作,则将使用Jenkins API,JsonSlurper和TimeUnit。 这是这种情况的示例代码:
import groovy.json.JsonSlurper
import java.util.concurrent.TimeUnit
def convert(int millisToConvert) {
long hours = TimeUnit.MILLISECONDS.toHours(millisToConvert);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millisToConvert) % TimeUnit.HOURS.toMinutes(1);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millisToConvert) % TimeUnit.MINUTES.toSeconds(1);
String format = String.format("%02d:%02d:%02d", Math.abs(hours), Math.abs(minutes), Math.abs(seconds));
return format;
}
node {
def time = 0;
int n = parameter.toInteger()
def m = n - 1;
def list = []
for(int x=m; x<=n; x++){
job = ["curl", "-k", "-u", "<user>:<apitoken>", "https://host/jenkins/view/TOOLS/job/somejob/{$x}/api/json?tree=duration"].execute().text
list.push( job )
}
println list
list.each {
print it
def d = new JsonSlurper().parseText( it );
def t = d.duration;
time += t;
}
def VARIABLE = convert(time)
println "Your time is ${VARIABLE}"
}
您可以通过n
和m
参数控制需要汇总的构建数量以及从何处开始。我使用convert
来获得00:03:46之类的格式,但这取决于您的需要。
输出:
Started by user 3sky
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /app/jenkins/home/workspace/testing_job
[Pipeline] {
[Pipeline] echo
[{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","duration":175052}, {"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","duration":51014}]
[Pipeline] echo
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","duration":175052}
[Pipeline] echo
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","duration":51014}
[Pipeline] echo
Your time is 00:03:46
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS