Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend Jenkins job page with new links and icons

I'm developing my first Jenkins plugin and followed the tutorial at wiki.jenkins-ci.org. After adding a BuildStep and generating the results I now want to publish them to the user. I would like to do this via a new link entry on the job page and a corrsponding result view page.

Unfortunatelly I do not find the right extension points for the navigation bar at the left side, the main navigation links in the center as well as the new target page. Can somebody point me in the right direction or give me a link to a tutorial or blog post that explains this scenario?

Thanks

Extend Job Page

like image 812
Richard Avatar asked Jul 05 '14 18:07

Richard


1 Answers

Root Action and Actions are different. The first one goes only to initial page (root), the second one can be attach to a Project/Job or to a Build.

To create a Root Action, just need to create a class that it's:

  1. Annotated with @Extension (so it can be found and automatically loaded by Jenkins)
  2. Implements RootAction Interface
  3. Override 3 methods: getIconFileName(), getDisplayName() and getUrlName()

For example:

@Extension
public class GoogleRootAction implements RootAction{

    @Override
    public String getIconFileName() {
        return "clipboard.png";
    }

    @Override
    public String getDisplayName() {
        return "Google URL";
    }

    @Override
    public String getUrlName() {
        return "http://www.google.pt";
    }    
}

To create an Action at a Project it's more complicated, and there's more than a way, depending of what you want.

But first, the class Action itself is the easy part, since it's very similar to a class RootAction. It's not annotated with @Extension and implements Action interface instead of RootAction.

For example:

public class LatestConsoleProjectAction implements Action {

    private AbstractProject<?, ?> project;

    @Override
    public String getIconFileName() {
        return (Jenkins.RESOURCE_PATH + "/images/48x48/terminal.png").replaceFirst("^/", "");
    }

    @Override
    public String getDisplayName() {
        return Messages.Latest_Console_Project_Action();
    }

    @Override
    public String getUrlName() {
        return "lastBuild/console";
    }

    public LatestConsoleProjectAction(final AbstractProject<?, ?> project) {
        this.project = project;
    }

}

The tricky part is to inform jenkins that this class Action exists. As I said, there are different ways.

For instance, one can associate an Action to a Builder or Publisher or other by just overriding getProjectAction() method at those classes.

For example:

@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
    return new LatestConsoleProjectAction(project);
}

But this way, the Action link will only show on Project left menu, if the corresponding Builder or Publisher is used by the job (or selected at Job configurations).

Another way, that always shows your Action link on left menu, it's create a factory class to inform jenkins. There are many factories, but at my example I will use TransientProjectActionFactory class.

For this, one will need to create a class that:

  1. It's annotated with @Extensions
  2. Extends TransientProjectActionFactory class (or another Factory class)
  3. Override createFor method to create your class Action associated with Project object

For example:

@Extension
public class LatestConsoleProjectActionFactory extends TransientProjectActionFactory {

    @Override
    public Collection<? extends Action> createFor(AbstractProject abstractProject) {

        return Collections.singletonList(new LatestConsoleProjectAction(abstractProject));
    }
}

One can still filter project object to just the projects types you want. The one you don't want, just return Collections.emptyList().

Beside this two ways, I think there are others. You can see this link to reference: https://wiki.jenkins-ci.org/display/JENKINS/Action+and+its+family+of+subtypes

Although, they refer to addAction method and others, but I couldn't use it (I have 2.19.2 Jenkins version). Also they refer groovy, but I didn't try it, since I want to stick with Java :)

Btw, my example will create an action link to open console page of last build. Useful to avoid selecting last build and then select his console page.

like image 50
dchang Avatar answered Oct 09 '22 02:10

dchang