I've created and successfully tested a Shared Library function with the following:
def call() { def jobNameParts = env.JOB_NAME.tokenize('/') as String[] jobNameParts.length < 2 ? env.JOB_NAME : jobNameParts[jobNameParts.length - 2] }
This takes care of the possibility that the multibranch pipeline is nested several folders deep (per Jesse Glick's comment that this should be the penultimate – next to last – token).