Jenkins插件開發(fā)

介紹
Jenkins的很多功能都是借助它的插件機制來實現(xiàn)的,類似Eclipse一樣,沒有安裝任何插件Jenkins幾乎沒有什么可用的功能。

如果你想要使用某個功能,首先應(yīng)該從插件管理里查找是否有對應(yīng)的插件。沒有找到的話,可以自己來開發(fā)一款插件。本文介紹Jenkins插件開發(fā)相關(guān)的內(nèi)容,這里給出的代碼都可以在我的Github帳號上找到。

hudson.ExtensionPoint

hudson.model.Describable

hudson.model.Descriptor

org.codehaus.cargo.container.deployer.Deployer

Maven
Jenkins提供了三個骨架(archetype)用來快速地創(chuàng)建一個插件工程,分別是:hello-world-plugin、empty-plugin、global-configuration-plugin。我們可以根據(jù)實際情況來進行選擇。hello-world插件會幫你生成一個具有構(gòu)建步驟的插件,安裝后通過新建一個構(gòu)建類型的任務(wù)就能看大效果。empty-plugin就是一個空白的Jenkins插件工程。

在pom.xml文件中有幾個地方要注意:

打包方式必須是hpi,name標(biāo)簽就是該插件的名稱(會在插件管理界面顯示),url標(biāo)簽就是插件管理界面對應(yīng)的超鏈接

運行
要運行你的插件,有幾種方式。打包后通過Jenkins插件管理界面來安裝,或者執(zhí)行在工程的根目錄下執(zhí)行命令。

mvn hpi:run

擴展點
構(gòu)建、流水線、服務(wù)

流水線
增加流水線步驟,需要繼承類Step,下面是一個沒有參數(shù)的步驟實現(xiàn):

package org.jenkinsci.plugins.phoenix.steps;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Set;

public class TriggerStep extends Step
{
@DataBoundConstructor
public TriggerStep(){}

@Override
public StepExecution start(StepContext stepContext) throws Exception
{
    return new Execution(stepContext);
}

@Extension
public static final class DescriptorImpl extends StepDescriptor
{

    @Override
    public Set<? extends Class<?>> getRequiredContext()
    {
        return Collections.singleton(FilePath.class);
    }

    @Override
    public String getFunctionName()
    {
        return "suren";
    }
}

public static class Execution extends AbstractStepExecutionImpl
{
    private StepContext stepContext;

    public Execution(StepContext stepContext)
    {
        this.stepContext = stepContext;
    }

    @Override
    public boolean start() throws Exception
    {
        System.out.println("trigger start");
        System.out.println(this.stepContext.hasBody());

        TaskListener listener = this.stepContext.get(TaskListener.class);

        listener.getLogger().println("phoenix logger");
        stepContext.onSuccess("success result");

        return true;
    }

    @Override
    public void stop(@Nonnull Throwable throwable) throws Exception
    {
        System.out.println("trigger stop");
    }
}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容