How to cancel redundant builds on branches/pulls, but not master

It can be done in a script step

if (env.CHANGE_ID != null) {
    properties([disableConcurrentBuilds(abortPrevious: true)])
}

but place that before anything that allocates an agent, so that it can be executed while all agents are busy.

Related discussion in https://issues.jenkins.io/browse/JENKINS-41392?focusedCommentId=344397&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-344397.