Pipeline: I know currentBuild, but how can I get a reference to the Project?

5,803 views
Skip to first unread message

Ramón Rial

unread,
Aug 25, 2016, 1:49:43 PM8/25/16
to Jenkins Developers
With System Groovy scripts I can get a reference to the Project which corresponding to the build with:

Thread.currentThread().executable.project

With Pipeline I have the currentBuild, but this is an instance of org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, and the method build() is private and getRawBuild is not allowed in secured environments.

How can I get the project corresponding to currentBuild?

Is any list of the binding variables passed to the pipeline scripts?

Thank you

Jesse Glick

unread,
Aug 25, 2016, 1:53:58 PM8/25/16
to Jenkins Dev
On Thu, Aug 25, 2016 at 1:49 PM, Ramón Rial <rri...@gmail.com> wrote:
> With Pipeline I have the currentBuild, but this is an instance of
> org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, and the
> method build() is private and getRawBuild is not allowed in secured
> environments.
>
> How can I get the project corresponding to currentBuild?

A whitelisted equivalent to `.rawBuild.parent` would need to be added,
plus whatever information you wanted to get from _that_. But anyway

> Is any list of the binding variables passed to the pipeline scripts?

What do you mean by “the binding variables”? If you stated your goal
up front it might be easier to help you.

Ramón Rial

unread,
Aug 26, 2016, 3:06:58 AM8/26/16
to Jenkins Developers
Thank you, Jesse.

I have designed a hierarchical structure for the projects in my Jenkins, so I can autogenerate many information to write to the Jenkins project in any moment, based on the fullName of that structure (and other files, of course).

I have a collection of groovy scripts that parses that information, and assign and/or fixes missing data (scm url's, ownership, folder and project descriptions...) to the project.
But there is some work that should be done at "build time", because the data needed is inside of the files in the scm repository, so I need to do a checkout first, and then I can read the filename (for example, the node description of pom.xml, or the version).

And with pipeline I don't know how to:

* Get the full name of the project (getFullName(), I use the Folders plugin).
* Change the description of a project.
* Get the Ownership plugin of a project.

And son on.

And for "binding variables" I mean what are the variables I can use in pipeline scripts, i.e., currentBuild is a binding Variable (when I try to use a non defined variable, for example, project, the error I get is undefined Binding Variable).

So, where can I find documentation about pipeline that says something like "You can use the currentBuild variable in pipeline scripts to...".

Thank you for your help.

Ramón Rial

unread,
Aug 26, 2016, 6:07:47 AM8/26/16
to Jenkins Developers
And for "binding variables" I mean what are the variables I can use in pipeline scripts, i.e., currentBuild is a binding Variable (when I try to use a non defined variable, for example, project, the error I get is undefined Binding Variable).
I answer myself: ${MY_JENKINS_URL}/pipeline-syntax/globals 

This is a begining... But I don't know how can I modify the description of the project, for example, because I have no idea about getting the Project reference. I know I can disable "Use the groovy sandbox", but I think this is not a great idea.

Jesse Glick

unread,
Aug 26, 2016, 12:03:49 PM8/26/16
to Jenkins Dev
On Fri, Aug 26, 2016 at 3:06 AM, Ramón Rial <rri...@gmail.com> wrote:
> I have a collection of groovy scripts that parses that information, and
> assign and/or fixes missing data (scm url's, ownership, folder and project
> descriptions...) to the project.
> But there is some work that should be done at "build time"

This is not, and should never be, possible from within a sandboxed
build. You could use the Groovy Postbuild plugin to add metadata to
the current *build* (not *job*).

> with pipeline I don't know how to:
>
> * Get the full name of the project (getFullName(), I use the Folders
> plugin).
> * Change the description of a project.
> * Get the Ownership plugin of a project.

These are just Jenkins model API questions which can be answered in
the Javadoc; it is not specific to Pipeline, once you get the
`currentBuild.rawBuild` which is a `hudson.model.Run`.

Ramón Rial

unread,
Aug 26, 2016, 7:14:10 PM8/26/16
to Jenkins Developers
Thank you, Jesse
Reply all
Reply to author
Forward
0 new messages