排除/中止由作业(例如作业A)手动触发的作业(例如作业A)触发的正在运行的构建作业(例如:Job-B)。
通过使用以下代码,无论是否手动启动,我都能获得作业的详细信息。
def was_previous_build_triggered_manually() {
for (cause in currentBuild?.rawBuild?.getPreviousBuild()?.getCauses()) {
if (cause.getShortDescription() =~ 'Started by user') {
return true
}
}
return false
}
所以我只需要在此处添加一些条件-检查该作业是否由其他手动触发的作业触发(因为我已经有了上面的代码来检查它是否手动触发)
答案 0 :(得分:0)
我们使用共享库来评估几种不同情况的原因。与您的相同,检查它是否是自动构建还是用户调用的(两种不同的原因可能意味着用户调用了)。
一个将原因归纳到一个好的列表中的函数。
一个函数,如果存在相关原因,则评估是非。
简单的条件条件。
我们在/ vars jobCauses.groovy下的共享库
/**
* Checks if job causes contain automated causes
* Return true if automated cause found
*
* @return boolean
*/
boolean hasAutomatedCauses() {
List automatedCauses = ['UpstreamCause', 'TimerTriggerCause']
List intersection = []
intersection = automatedCauses.intersect(getCauses())
// if no automated causes are found means intersection is empty and then return false
return !intersection.isEmpty()
}
/**
* Checks if job causes contain Non-automated causes
* Either
*** Run by a User
*** Rebuilt by a User
*** Replayed by a User
* Return true if non automated cause found
*
* @return boolean
*/
boolean hasNonAutomatedCauses() {
List nonAutomatedCauses = ['UserIdCause', 'ReplayCause']
List intersection = []
intersection = nonAutomatedCauses.intersect(getCauses())
// if no user triggered causes are found means intersection is empty and then return false
return !intersection.isEmpty()
}
/**
* Retrieves list of causes that generated job execution
*
* @return list
*/
List getCauses() {
return currentBuild.rawBuild.getCauses().collect { it.getClass().getCanonicalName().tokenize('.').last() }
}
在我们的Jenkins文件中:
Boolean isHumanTriggered = jobCauses.hasNonAutomatedCauses()
if ( isHumanTriggered ) {
//do the thing
}