Looking at the Jenkins code, specifically at Queue#scheduleInternal(), I see that there is a mechanism to decide whether to schedule a new build. Every item in the queue is asked whether to schedule a new build given the list of actions. If a queue item responds through shouldSchedule() that the new build should not be scheduled, that queue item absorbs the new actions without scheduling the build. Only actions that implement QueueAction interface are involved in deciding whether to schedule a build. That mechanism could be used to replace StashRepository#cancelPreviousJobsInQueueThatMatch() in a safer manner. Cancelling a queue item is risky because the new build can fail to be scheduled (e.g. Jenkins is being rebooted). Instead, the new build would replace data (StashCause) in the existing queue item. Git plugin implements RevisionParameterAction that is added to the actions when scheduling. It's responsible for deciding whether to schedule a new build based on the git revision. It could serve as an example. |