Groovy Postbuild do not execute scripts on Jenkins

I found the solution:

Script was executed but wasn't printed to console output. To print result to console output you need to write: manager.listener.logger.println("Some string") instead of println.

To make it shorter do:

logger = manager.listener.logger.&println // and call like this: logger("test log message")

EDIT: add in logger example and to describe how to get env vars (and how to not get them) and to hopefully save people some debugging time . . . this is simple but awkward stuff.

To get the workspace you can go through the manager object. Like this:

manager.build.workspace

To get env vars, this does not work:

String jbN = System.getenv('JOB_NAME')

It shows jbN is null. That makes sense as JOB_NAME is not an actual system environment var.

This also does not work to get env vars, an exception is thrown:

${manager.envVars['WORKSPACE']}

This does work to get jenkins job "env vars" like WORKSPACE, JOB_NAME, BUILD_NAME:

def build = Thread.currentThread().executable workspace = build.getEnvVars()["WORKSPACE"]

Example of use, you can call a groovy script in workspace like this:

evaluate(new File(manager.build.workspace.toString() + "/dirinworkspace/scriptname.groovy"))