Determining the (Git) Branch of the Jenkins Pipeline Job
When running Jenkins' Multibranch pipelines, you get a handy environment variable GIT_BRANCH
which tells you what the current branch is, so you can do different things in your pipeline.
However, when you're using a Pipeline Job, that's not possible, as the environment variable isn't set.
Fortunately, this is available to the Groovy sandbox, and as Mad Physicist mentions on StackOverflow, we can reference it using:
scm.branches[0].name
This allows us to see which branch was used to check out the script that is currently executing.