通过Groovy Postbuild优先安装Jenkins

时间:2018-04-13 09:07:45

标签: jenkins groovy jenkins-plugins post-build-event

我想通过Groovy Postbuild将作业安排在Jenkins的构建队列的顶部。

例如,对于Build X

如果以前构建队列如下所示:

  • Generic Build 1
  • Generic Build 2
  • Generic build 3

现在看起来应该是这样的:

  • Build X
  • Generic Build 1
  • Generic Build 2
  • Generic Build 3

到目前为止,我已经能够使用

安排工作
def waitingItem = Jenkins.get().getQueue().schedule(job, 0)

if (waitingItem == null) {
    manager.listener.logger.println "Error scheduling ${job}!"
} else {
    manager.listener.logger.println "${job.name} was scheduled!"
}

但现在我还希望 waitingItem 位于队列的顶部。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

好的,所以经过无数个小时的浏览网络,我已经能够提出一个Groovy Postbuild脚本,它执行以下操作:一旦完成构建,它会触发另一个构建(让我们调用它) buildToBeTriggered)和buildToBeTriggered自动被推到队列的前面。

代码如下:

import hudson.model.AbstractProject
import hudson.model.Queue
import hudson.model.queue.QueueSorter
import jenkins.model.Jenkins

def job = (AbstractProject)Jenkins.get().getItem('gcimpoies-toTrigger')
def isSuccess = manager.getResult() == 'SUCCESS'
def isRelease = manager.getEnvVariable('RELEASE') == 'true'
def secondsToWait = 20

if (isSuccess) {

def waitingItem = Jenkins.get().getQueue().schedule(job, 0)
if (waitingItem == null) {
    manager.listener.logger.println "Error scheduling ${job}!"
} else {
    manager.listener.logger.println "${job.name} was scheduled!"
}
Thread.sleep(secondsToWait * 1000)

//replace the original queue sorter with one that will place our project build first in the queue
QueueSorter originalQueueSorter = Jenkins.get().getQueue().getSorter()

AcceleratedBuildNowSorter acceleratedBuildNowSorter = new AcceleratedBuildNowSorter(job, originalQueueSorter)
Jenkins.get().getQueue().setSorter(acceleratedBuildNowSorter);

// we sort the queue so that our project is next to be built on the list
Jenkins.get().getQueue().getSorter().sortBuildableItems(Jenkins.getInstance().getQueue().getBuildableItems())

}

class AcceleratedBuildNowSorter extends QueueSorter {

private final AbstractProject project
private final QueueSorter originalQueueSorter
private final AcceleratedBuildNowComparator comparator

AcceleratedBuildNowSorter(AbstractProject project, QueueSorter originalQueueSorter) {
    this.project = project
    this.originalQueueSorter = originalQueueSorter
    comparator = new AcceleratedBuildNowComparator(this.project)
}

@Override
void sortBuildableItems(List<Queue.BuildableItem> buildables) {
    if (this.originalQueueSorter != null) {
        this.originalQueueSorter.sortBuildableItems(buildables)
    }
    Collections.sort(buildables, comparator)
}
}

class AcceleratedBuildNowComparator implements Comparator<Queue.BuildableItem> {

private final AbstractProject mostPriorityProject;

AcceleratedBuildNowComparator(AbstractProject mostPriorityProject) {
    this.mostPriorityProject = mostPriorityProject;
}

int compare(Queue.BuildableItem buildableItem0, Queue.BuildableItem buildableItem1) {
    AbstractProject<?, ?> project0 = (AbstractProject<?, ?>) buildableItem0.task
    AbstractProject<?, ?> project1 = (AbstractProject<?, ?>) buildableItem1.task
    if (project0.equals(mostPriorityProject)) {
        return -1
    }
    if (project1.equals(mostPriorityProject)) {
        return 1
    }
    return 0
}
}