Given a jenkins 2.1 build pipeline, jenkins injects a env variable into the node{}. For example, BRANCH_NAME can be accessed with
env
node{}
BRANCH_NAME
The answers above, are now antiquated due to new pipeline syntax. Below prints out the environment variables.
script { sh 'env > env.txt' String[] envs = readFile('env.txt').split("\r?\n") for(String vars: envs){ println(vars) } }