Geek Geek - 22 days ago 13
Groovy Question

Access BUILD_NUMBER in Jenkins Promoted build plugin scripts

I am using Promoted Build plugin. And using some custom groovy scripts to validate the build! I wanted to access the value of

BUILD_NUMBER
from the groovy script. Can anyone suggest me how i can achieve this?

Another thing i am writing
println
statement in this groovy script but its no where getting logged. Any suggestion to debug the script flow how can i log the info ?

Thanks

Answer

If it's in runtime you can use:

def env = System.getenv()
//Print all the environment variables.

env.each{
println it
} 
// You can also access the specific variable, say 'username', as show below 
String user= env['USERNAME']

if it's in system groovy you can use:

// get current thread / Executor and current build
def thr = Thread.currentThread()
def build = thr?.executable

//Get from Env
def stashServer= build.parent.builds[0].properties.get("envVars").find {key, value -> key == 'ANY_ENVIRONMENT_PARAMETER' }

//Get from Job Params
def jobParam= "jobParamName"
def resolver = build.buildVariableResolver
def jobParamValue= resolver.resolve(jobParam)

Any println is sending output to the standard output steam, try looking at the console log. Good luck!