I would like to get the git commit hash of the current build from within a plugin (that is, in a Java class that extends hudson.tasks.Recorder).
This works for a freestyle project:
EnvVars env = run.getEnvironment(listener);
String hash = env.get("GIT_COMMIT");
However, in pipeline projects, the GIT_COMMIT environment variable is evidently not set, so hash becomes null. This is true even for a declarative pipeline where the scm checkout step is implicit.
Is there an alternative way to get the commit hash in a pipeline project?
Edit: To clarify, this is regarding getting git commit hash from the Java code in a Jenkins plugin, not in groovy/Jenkinsfile. It seems like the environment variable will not exist when the plugin is used in pipeline jobs, so I'm looking for a workaround.