Building on @elouan-keryell-even 's answer, you can create a function in a shared library to wrap the check, like this:
@NonCPS
def getCurrentUsername() {
String userId
if(currentBuild.upstreamBuilds){
userId = currentBuild.upstreamBuilds[0].getBuildCauses()[0].userId
} else {
userId = currentBuild.getBuildCauses()[0].userId
}
return userId.toLowerCase().trim()
}
Then in your Jenkinsfile you can do:
getCurrentUsername()