5

I am looking to write a jenkins plugin that lets me add a new link that links to a new page of my creating. I have determined how to create the new link by extending RootAction and I can get it to link to a static resource in the webapp directory. My problem is many fold though. I think I need to use a jelly page because of the nature of action I want to do on this new page of mine and even if I could do what I wanted to with static web content the link pops me out of the jenkins interface to a page only containing my static web content.

What I need to know is, how do you go about creating a brand new page? I have been scowering the JavaDoc looking for an interface and the internet for more documentation on how to write plugins. But the internet seems to be sadly lacking on information pertaining to Jenkins plugin development.

What I want to do is be able to click my new link and have it take me to my new page, still with all the Jenkins navigation and such, and on this new page I am going to have a form for performing some actions on files.

Any help or pointers to documentation I have not found would be greatly appreciated.

4

1 回答 1

1

如您所见,第一步是扩展 RootAction 或 Action,具体取决于您希望页面位于何处:分别是全局或每个项目。

一个简单的 RootAction:

package my.package;

@Extension
public class MyNewPage implements hudson.model.RootAction
{

    @Override
    public String getDisplayName()
    {
        return "My Custom Page";
    }

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

    @Override
    public String getUrlName()
    {
        return "my-url"; // the url path
    }
}

接下来,添加一个关联的果冻文件位于src/main/resources/my/package/MyNewPage/index.jelly

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
    xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
    <l:layout norefresh="true">
        <l:header title="My New Page" />
        <l:main-panel>
            <h1>Put content here</h1>
        </l:main-panel>
    </l:layout>
</j:jelly>

有关更多标签,请参阅jenkins jelly 标签参考。请注意,该l:layout标签是完全可选的,您根本不必使用 jenkins 标签,而是可以在果冻中转储静态 HTML 内容

然后,这两个文件将用于呈现对http://localhost:8080/jenkins/my-url 的请求

或者,对于完全动态的内容,使用jenkins 内置的订书机库来呈现任意 url。这是一个打印 url 的愚蠢的小例子:

public void doDynamic(StaplerRequest req, StaplerResponse resp) throws IOException
{
    resp.setStatus(418);
    resp.getOutputStream().print("I'm a teapot! You requested this path: " + req.getRestOfPath());
}

只需将该方法或任何订书机方法添加到 MyNewPage(或任何支持它的扩展)。它将响应下的所有网址http://localhost:8080/jenkins/my-url/*

我发现的其他一些很好的参考资料:

于 2019-12-16T17:14:27.610 回答