Expand environment variables

This patch includes both fixes for JENKINS-22256 (provided by myself) and JENKINS-21643 (provided by rcalso).