This works for replayed (not rebuilt builds) builds. The output is the build number of the original build from which your current build was replayed from:
def getReplayCauseNumber() {
// This function is used to access the build number of the build from which a build was replayed from
def cause = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.workflow.cps.replay.ReplayCause)
if (cause == null){
return null
}
def originalNum = cause.getOriginalNumber()
echo "This build was replayed from build #${originalNum}"
return originalNum
}