I took me some time to figure it out, so I'm sharing this here:
def env = EnvironmentExpander.getEffectiveEnvironment(new EnvVars(), null, steps.getContext(EnvironmentExpander.class), null, null)
def ansiColorEnabled = 'TERM' in env
Usage example: https://github.com/voyages-sncf-technologies/hesperides-jenkins-lib/blob/master/src/com/vsct/dt/hesperides/jenkins/pipelines/http/JenkinsHTTRequester.groovy#L36
Recently I lost a lot of time on this. Hence I want to share a working solution, even if i cannot take the time to detail the issue.
I'm taking about writing reusable code for Jenkinsfiles : https://jenkins.io/doc/book/pipeline/shared-libraries/
One cannot simply use Groovy HTTPBuilder, because …