[FIX JENKINS-20280] Calculates the order to override variables. This is an improved fix for [JENKINS-19488] and [JENKINS-19926].
[FIXED JENKINS-20280]

Merged pull request #986.