groovy.lang.MissingPropertyException: No such property: manager for class: Script1
Asked Answered
P

4

26

I am trying to invoke Groovy inside Hudson (using groovy plugin) to get some properties for our build. But I am getting this exception:

groovy.lang.MissingPropertyException: No such property: manager for class: Script1

I get this with the following line:

def buildNUmber = manager.build.number

This happens when I run as an inline command within Jenkins as well as using a script:

I tried the solution below, but it fails during the declaration itself (line two):

Binding binding = new Binding();
binding.setVariable("manager", manager);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(new File("d:/dev/others/hudson/userContent/ScriptStuff.groovy").text);

The above is run using: Groovy command. And when I run the build it errors and complains about the line - binding.setVariable("manager", manager);

When I use the Groovy script file, then it complains about:

 def buildNumber = manager.build.number

Both errors are :

groovy.lang.MissingPropertyException: No such property: manager for class: Script1

Tried everything mentioned in this thread as well:

I am using Hudson 2.2.1 and Groovy 2.1.3. What could be wrong?

Percept answered 18/4, 2013 at 13:14 Comment(1)
I tired in the script console and it fails there with the same exception.Percept
A
3

Maybe I'm missing some part of your code, but where do you define the manager? If that's the complete Groovy script, you're trying to bind a variable which isn't declared anything, so it isn't surprising that it fails.

Just define a manager it that's what you want, like:

def manager = "my manager" // probably not what you want

This should solve your current error.

Awed answered 21/4, 2013 at 18:54 Comment(3)
Sorry I can't do that way, as the manager is a value that we should get from Hudson instance.Percept
I understand, but it seems like you are trying to execute a Groovy script yourself, instead of using wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin, based on the GroovyShell.execute() command.Awed
Make sure you doing a "Post Build Action" and not a "Post Step"Wicks
T
20

manager is provided by certain Groovy script plugins, but not all. To make your script generic, use the Jenkins/Hudson API instead:

import hudson.model.*

def build = Thread.currentThread().executable
def buildNumber = build.number
...
Troat answered 4/11, 2014 at 8:40 Comment(2)
Great! How do you get job name from build variable? I can see build contains job name and build number but don't know how to print it. Tried all kinds of combinationsProffitt
Once you have build object you can do . . . 'jobname = build.getEnvVars()["JOB_NAME"]' 'workspace = build.getEnvVars()["WORKSPACE"]'Zephan
T
11

One of the reasons groovy.lang.MissingPropertyException: is thrown when you are using a variable outside of its scope or you haven't defined that variable.

Thorson answered 10/8, 2017 at 18:52 Comment(0)
P
10

Just in case it helps, if you are using the 'Execute System Groovy Script', you don't need to use the 'manager' variable. This worked for me -

def workspace = build.getEnvVars()["WORKSPACE"]
Petrozavodsk answered 21/8, 2016 at 8:36 Comment(3)
It helps. Thanks. '''def JOB_NAME = build.getEnvVars()["JOB_NAME"]''' then later in html use $JOB_NAME.Zephan
Thanks you, it helped me !Road
It was reasonably hard to find this helpful answer, so thank you!Jasper
A
3

Maybe I'm missing some part of your code, but where do you define the manager? If that's the complete Groovy script, you're trying to bind a variable which isn't declared anything, so it isn't surprising that it fails.

Just define a manager it that's what you want, like:

def manager = "my manager" // probably not what you want

This should solve your current error.

Awed answered 21/4, 2013 at 18:54 Comment(3)
Sorry I can't do that way, as the manager is a value that we should get from Hudson instance.Percept
I understand, but it seems like you are trying to execute a Groovy script yourself, instead of using wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin, based on the GroovyShell.execute() command.Awed
Make sure you doing a "Post Build Action" and not a "Post Step"Wicks

© 2022 - 2024 — McMap. All rights reserved.