Expose data to API from a postbuild Plugin

33 views
Skip to first unread message

David Tolley

unread,
Jun 27, 2012, 7:56:42 AM6/27/12
to jenkin...@googlegroups.com
Hello,

I am trying to expose some data to the API from a post-build step, and I am
not having much luck at getting the data into the API. I have added the
@Extension and @ExtensionBean annotations, and still nothing is showing up
for me. Here is what I have, any ideas as to why I can't access the exposed
info from the API?

package com.groupon.injectartifactintoapi;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.*;

@ExportedBean
public class InjectArtifactIntoAPI extends Recorder {
    public final String artifactName;
    @Exported
    public String revision;

    @DataBoundConstructor
    public InjectArtifactIntoAPI(String artifactName) {

        this.artifactName = artifactName;

    }

    @Override
    public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {

        if (build.getHasArtifacts()) {

                for (Run.Artifact artifact : build.getArtifacts()) {
                    if (artifact.getFileName() == artifactName) {
                        FileInputStream fstream = new
FileInputStream(artifact.getFileName());
                        DataInputStream in = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new
InputStreamReader(in));
                        revision = br.readLine();
                        System.out.println(revision);
                    }
                }
        }

        final PrintStream logger = listener.getLogger();
        logger.println(StringUtils.defaultString(revision));

        revision = "243dffefef";

Kohsuke Kawaguchi

unread,
Jun 28, 2012, 8:08:26 PM6/28/12
to jenkin...@googlegroups.com, David Tolley

The problem is that there's no existing Api object that includes
Recorder in its object graph. It's bit like you've built your house all
correctly but there's no road to get to it.

You can add the following to your publisher:

public final Api getApi() {
return new Api(this);
}

... but I then noticed that a Recorder object isn't bound to URL, so
there's no URL that takes you to this Api object.

Let me add that to the core.
--
Kohsuke Kawaguchi | CloudBees, Inc. | http://cloudbees.com/
Try Nectar, our professional version of Jenkins


Kohsuke Kawaguchi

unread,
Jun 28, 2012, 8:34:05 PM6/28/12
to jenkin...@googlegroups.com, David Tolley

Fixed in jenkins-ci.org/commit/core/7bcc8107e2200fb9cd82788dbef471c612d9771f

This exposes your publisher at:


/job/foo/publishersList/com.groupon.injectartifactintoapi.InjectArtifactIntoAPI/

... which roughly translates to
Jenkins.getInstance().getJob("foo").getPublishersList().getDynamic("com.groupon.injectartifactintoapi.InjectArtifactIntoAPI")

so then you can add the getApi() method to your Recorder and append
"/api" to the above URL.
Reply all
Reply to author
Forward
0 new messages