詹金斯插件。 RootAction。 index.jelly在单独的窗口中

时间:2013-09-30 10:57:09

标签: plugins jenkins jelly

我正在写一个简单的插件,我被迫创建一个显示页面(index.jelly)的RootAction,需要一些额外的值来确认,然后执行该方法。

我的问题是,index.jelly文件总是显示在空白窗口上。 但我确实需要它像往常一样被包含在主表中的Jenkinstemplate中。

似乎无法弄清楚为什么会这样。

有什么想法吗?

RestartJksLink.java

package org.jenkinsci.plugins.tomcat_app_restart;

import hudson.Extension;
import hudson.model.ManagementLink;

/**
 *
 *
 * @author [...]
 */
@Extension
public class RestartJksLink extends ManagementLink {
    @Override
    public String getIconFileName() {
        return "/plugin/tomcat-app-restart/images/restart.png";
    }

    @Override
    public String getUrlName() {
        return "jksrestart";
    }

    @Override
    public String getDescription() {
       return "Restart your Jenkins-Application on Tomcat";
    }

    public String getDisplayName() {
        return "Restart Jenkins-App on Tomcat";
    }
}

RestartJksRootAction.java

package org.jenkinsci.plugins.tomcat_app_restart;

import java.io.IOException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

import jenkins.model.Jenkins;
import hudson.Extension;
import hudson.model.RootAction;
import hudson.util.FormValidation;

@Extension
public class RestartJksRootAction implements RootAction {
    public String getDisplayName() {
        return "Restart Jenkins on Tomcat";
    }

    public String getIconFileName() {
        if (!Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) {
          return null;
        }

        if (!Jenkins.getInstance().getLifecycle().canRestart()) {
          return null;
        }

        return "/plugin/tomcat-app-restart/images/restart.png";
    }

    public String getUrlName() {
        return "jksrestart";
    }

    public FormValidation doJksRestart() {
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication ("admin", "admin".toCharArray());
            }
        });

        URL url;
        try {
            url = new URL("http://localhost:8888/manager/text/start?path=/jenkins");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            System.out.println("" + connection.getResponseMessage());

            return FormValidation.ok("Success");
        } catch (IOException e) {

            return FormValidation.error("Client error: " + e.getMessage());
        }
    }
}

index.jelly里面:resources.org.jenkinsci.plugins.tomcat_app_restart.RestartJksRootAction

<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" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
  <f:validateButton
   title="${%Restart Jenkins}" progress="${%Restarting...}"
   method="JksRestart" with="" />
</j:jelly>

谢谢你们!

我是jenkins插件开发的新手,这对我有很多帮助。

亲切的问候。

2 个答案:

答案 0 :(得分:1)

这个演示(rootaction-example-plugin)有很多帮助。你可以阅读它。

https://github.com/gustavohenrique/jenkins-plugins/tree/master/rootaction-example-plugin

答案 1 :(得分:1)

<l:main-panel>标记和<l:layout norefresh="true">标记添加到index.jelly文件中。

并包括侧面板:

  • 将构建传递给Action(通过构造函数的参数)
    • 可以从继承自BuildStepCompatibilityLayer类(通过Extending Publisher)继承的perform方法的参数中检索构建。
  • 在Action类
  • 中创建getBuild()方法
  • 使用构建
  • 添加<st:include it="${it.build}" page="sidepanel.jelly" />标记

果冻示例(index.jelly):

<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" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
    <l:layout norefresh="true">
        <st:include it="${it.build}" page="sidepanel.jelly" />
        <l:main-panel>
            <f:validateButton title="${%Restart Jenkins}" progress="${%Restarting...}" method="JksRestart" with="" />
        </l:main-panel>
    </l:layout>
</j:jelly>

Java Action类示例:

package tryPublisher.tryPublisher;

import hudson.model.Action;
import hudson.model.AbstractBuild;


public class ExampleAction implements Action {
    AbstractBuild<?,?> build;

    public ExampleAction(AbstractBuild<?,?> build) {
        this.build = build;
    }

    @Override
    public String getIconFileName() {
        return "/plugin/action.png";
    }

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

    @Override
    public String getUrlName() {
        return "ExampleActionUrl";
    }

    public AbstractBuild<?,?> getBuild() {
        return this.build;
    }
}

Java Publisher类示例:

package tryPublisher.tryPublisher;

import java.io.IOException;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;

public class ExamplePublisher extends Publisher {

    @Override
    public BuildStepMonitor getRequiredMonitorService() {
        return BuildStepMonitor.NONE;
    }

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

        build.getActions().add(new ExampleAction(build));

        return true;
    }

}

.jelly文件必须位于插件项目的正确资源图中。在与实现Action的Java类名称相同的映射中。 .jelly的名字也很重要。