[JENKINS-20280] Use topological sorting to decide the order to override variables.