Плагин Дженкинс. Корневое действие. index.jelly в отдельном окне

Я пишу простой плагин и вынужден создать RootAction, который отображает страницу (index.jelly) и требует некоторых дополнительных значений для подтверждения и последующего выполнения метода.

Моя проблема в том, что файл 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, это поможет мне многое понять.

С уважением.


person user2776900    schedule 30.09.2013    source источник


Ответы (2)


эта демонстрация (rootaction-example-plugin) очень помогла. Вы можете прочитать ее.

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

person bystander    schedule 27.09.2014

Добавьте теги <l:main-panel> и <l:layout norefresh="true">tag в файл index.jelly.

И включите боковую панель:

  • Pass the the build to Action (through a parameter of the constructor)
    • The build can be retrieved out of the parameters of the perform method which is inherited from the BuildStepCompatibilityLayer class (by Extending Publisher).
  • Создайте метод getBuild() в классе Action.
  • Добавьте тег <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>

Пример класса Action Java:

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 должен находиться на правильной карте ресурсов проекта плагина. В карте с тем же именем, что и имя класса Java, реализующего Action. Имя .jelly также важно.

person QuestionBomber    schedule 10.12.2014