Expose data to API from Jenkins Shared library

104 views
Skip to first unread message

Oscar Arevalo Loyola

unread,
Dec 15, 2021, 5:27:27 AM12/15/21
to Jenkins Developers
Hi,

I'm trying to expose internal Run data to the REST API.
Currently I'm using a Shared Library to create an object containing data about downstream builds in multiple levels

Is it possible to expose the data to the REST API from Shared library code?
I was checking several methods but still not able to achieve it.
So, I tried with Run.addAction and I'm able to see my action in in the url/api/json but it doesn't expose the properties of my action (I added @ExportedBean to the class and @Exported to the properties)

Basically, I want to achieve this:
but from a Shared Library, not from a plugin.

Ullrich Hafner

unread,
Dec 15, 2021, 6:17:04 AM12/15/21
to JenkinsCI Developers
Does your action implement a method:

public Api getApi();
?

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/7997e32b-cf5f-42a2-8879-880fbaa11d44n%40googlegroups.com.

Oscar Arevalo Loyola

unread,
Dec 15, 2021, 12:31:15 PM12/15/21
to Jenkins Developers
Thanks for your response.

Yes, I implemented the method in my class.

My test code looks like:

import hudson.model.Action
import hudson.model.Api
import org.kohsuke.stapler.export.ExportedBean
import org.kohsuke.stapler.export.Exported

@ExportedBean
public class myAction implements Action {
   
    private String name;
   
    public Api getApi() {
        return new Api(this);
    }

    public myAction(String name) {
        this.name = name;
    }
   
    @Exported
    public String getName() {
        return name;
    }
   
    @Override
    public String getIconFileName() {
        return null;
    }

    @Override
    public String getDisplayName() {
        return null;
    }

    @Override
    public String getUrlName() {
        return null;
    }
}

Then from the pipeline:

currentBuild.rawBuild.addAction(new myAction("actionName"))

In the api/json response I see myAction:

{ "_class": "myAction" },

but no properties exposed.

I tried defining visibility in @Exported and also increasing depth in the api request but didn't work.

Gavin Mogan

unread,
Dec 15, 2021, 12:51:32 PM12/15/21
to Jenkins Developers
Is that exported bean a java class or a shared library groovy class?

i feel like stapler only checks classes on startup or something?

Gavin

Jesse Glick

unread,
Dec 15, 2021, 2:43:48 PM12/15/21
to jenkin...@googlegroups.com
On Wed, Dec 15, 2021 at 5:27 AM Oscar Arevalo Loyola <osar...@gmail.com> wrote:
I want to achieve this:
but from a Shared Library, not from a plugin.

That is not supported. Just use a plugin. 

Oscar Arevalo Loyola

unread,
Dec 15, 2021, 3:37:33 PM12/15/21
to Jenkins Developers
I appreciate your responses.

Any other suggestion to expose custom Run data through the REST API? (To be consumed from external systems)

Gavin Mogan

unread,
Dec 15, 2021, 4:49:37 PM12/15/21
to Jenkins Developers
Its pretty easy to make a plugin, and you can put that class in said plugin and nothing else. You can also then expand it so its viewable and such.


--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.

Oscar Arevalo Loyola

unread,
Dec 17, 2021, 8:34:22 PM12/17/21
to Jenkins Developers
I think that works for my purpose. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages