How to extend Jenkins job page with new links and icons

后端 未结 5 1027
难免孤独
难免孤独 2021-02-14 13:29

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 u

5条回答
  •  悲&欢浪女
    2021-02-14 13:47

    After a lot of trial and error I figured out the solution.

    All in all you need two different things in your project:

    1) A class that inherits from ProminentProjectAction:

    import hudson.model.ProminentProjectAction;
    
    public class MyProjectAction implements ProminentProjectAction {
    
        @Override
        public String getIconFileName() {
            // return the path to the icon file
            return "/images/jenkins.png";
        }
    
        @Override
        public String getDisplayName() {
            // return the label for your link
            return "MyActionLink";
        }
    
        @Override
        public String getUrlName() {
            // defines the suburl, which is appended to ...jenkins/job/jobname
            return "myactionpage";
        }
    }
    

    2) Even more important is that you add this action somehow to your project.

    In my case I wanted to show the link if and only if the related build step of my plugin is configured for the actual project. So I took my Builder class and overwrote the getProjectActionsMethod.

    public class MyBuilder extends Builder {
    
        ...
    
        @Override
        public Collection getProjectActions(AbstractProject project) {
            List actions = new ArrayList<>();
            actions.add(new MyProjectAction());
    
            return actions;
        }
    }
    

    Maybe this is not the perfect solution yet (because I'm still trying to figure out how all the artifacts are working together), but it might give people which want to implement the same a good starting point.

    The page, which is loaded after clicking the link is defined as index.jelly file under source/main/resources and an underlying package with the name of the package of your Action class appended by its class name (e.g. src/main/resources/org/example/myplugin/MyProjectAction).

提交回复
热议问题