当前位置:网站首页>Jenkins plug-in development -- plug-in expansion
Jenkins plug-in development -- plug-in expansion
2022-07-25 02:42:00 【Free and easy mental retardation】
Jenkins Plug-in development —— Expansion of plug-ins
Preface
For each build , We may hope that Jenkins There are other places besides the console log to host the results of the build , At this time, you can expand the plug-in , This article uses a simple example to demonstrate how to expand their own development Jenkins plug-in unit .
establish HelloWorldAction
This development is based on the plug-in project sorted out in the previous blog , The specific code directory of the project is as follows :
First, in the MyBuilder Build the same package path of the main class com/yzh Create HelloWorldAction, This class needs to inherit hudson.model.Action Interface ,Action The interface provides the following methods :
public interface Action extends ModelObject {
@CheckForNull String getIconFileName();
@Override
@CheckForNull String getDisplayName();
@CheckForNull String getUrlName();
}
- getIconFileName Method means that after the construction is completed , Icon on the side panel .jenkins There are many built-in icons , Here you can directly get and use .
- getDisplayName Method represents the label of the side panel .
- getUrlName Method means that after the construction is completed , You can use this url To visit .
I am here HelloWorldAction in , Provides a member variable name, And provides getter And constructors :
public class HelloWorldAction implements Action {
@Getter
private final String name;
public HelloWorldAction(String name) {
this.name = name;
}
@Override
public String getIconFileName() {
return "document.png";
}
@Override
public String getDisplayName() {
return "HelloWorld";
}
@Override
public String getUrlName() {
return "hello";
}
}
Through the configuration of the above implementation , The final effect is in the side panel , There will be one. HelloWorld The label of , And link to URL http://{jenkins}:{port}/{jobName}/{buildNumber}/hello
After the MyBuilder Build the main class perform In the method , Anywhere in the build , You can call hudson.model.AbstractBuild#addAction Method , Add the action example :
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
PrintStream logger = listener.getLogger();
logger.println("Hello " + name);
// Add custom action
build.addAction(new HelloWorldAction(name));
boolean printLog = getDescriptor().isPrintLog();
if (printLog) {
logger.println("Log is " + getDescriptor().getLog());
}
return true;
}
Add display view
stay src/main/resources/com/yzh/ Under the table of contents , Create a sum HelloWorldAction Directory with the same name , This directory contains and HelloWorldAction Resource file corresponding to class . Here and the main class built Builder similar .
stay src/main/resources/com/yzh/HelloWorldAction/ Under the table of contents , We create a index.jelly file , This file is the end of the build, which can be passed URL http://{jenkins}:{port}/{jobName}/{buildNumber}/hello Accessed files .
index.jelly The documents are as follows ( About jelly For the usage of documents, please refer to :Apache Commons Jelly):
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="HelloWorld">
<l:main-panel>
<h1>
Name: ${it.name}
</h1>
</l:main-panel>
</l:layout>
</j:jelly>
among ,${it.name} It's a JEXL expression . it Reference the... To which the view belongs Java object ( Be similar to Java Medium this), In this case HelloWorldAction example . it.name Equivalent to calling getName().
After through mvn hpi:run Build plug-ins , The final page effect is like this :
Add the built side panels to the view
In the method call above , The view of the side panel is empty . In this case , You can inherit Action The interface is replaced with RunAction2, This interface adds two new methods , This method can be implemented according to the following code ( Why don't you study it in detail , That's all I know ~~~)
/** * Extension plug-ins 2 Support side panel view echo * * @author yuanzhihao * @since 2022/7/19 */
public class HelloWorldAction2 implements RunAction2 {
@Getter
private final String name;
public HelloWorldAction2(String name) {
this.name = name;
}
private transient Run run;
@Override
public void onAttached(Run<?, ?> run) {
this.run = run;
}
@Override
public void onLoad(Run<?, ?> run) {
this.run = run;
}
public Run getRun() {
return run;
}
@Override
public String getIconFileName() {
return "document.png";
}
@Override
public String getDisplayName() {
return "HelloWorld2";
}
@Override
public String getUrlName() {
return "hello2";
}
}
Add... To the build HelloWorldAction2 example :
build.addAction(new HelloWorldAction2(name));
src/main/resources/com/yzh/HelloWorldAction2/index.jelly The configuration is as follows :
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="HelloWorld2">
<!-- Add side panel -->
<l:side-panel>
<st:include page="sidepanel.jelly" it="${it.run}" optional="true" />
</l:side-panel>
<l:main-panel>
<h1>
Name: ${it.name}
</h1>
</l:main-panel>
</l:layout>
</j:jelly>
adopt mvn hpi:run Build plug-ins , The final page effect , You can see the sum Jenkins UI Correct integration :
Conclusion
This time about jenkins The expansion of plug-ins is sorted out here , This expansion is based on jenkins Doing secondary development is also very helpful .
Reference resources :https://www.jenkins.io/zh/doc/developer/tutorial/extend/
Code address :https://github.com/yzh19961031/blogDemo/tree/master/jenkinsPlugin
边栏推荐
- Detailed explanation of manually writing servlet in idea
- Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
- Deep understanding of string class
- "Ask the sky" is in place! Yu Jingchuan's "China Space Station" in 2013 is about to set sail in the sea of stars
- Apk packaging process
- Domain driven model (DDD)
- Industrial control safety PLC firmware reverse III
- Pycharm writes SQLite statements without code prompts
- R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set
- Codeforces:d1. choosing carrots (easy version) [max min problem + control one side to make the other side as close as possible + thinking]
猜你喜欢

Use unicloud cloud function to decode wechat motion steps in applet
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/26/18fd3106f887f503479f081e29dac3.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

MySQL advanced (13) command line export import database

R language one page and many pictures
![[system design] distributed key value database](/img/57/4d835d83f0e6ffb87e8ba39ec3b482.png)
[system design] distributed key value database

ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline

Simulation Implementation of string function (Part 1)

"Ask the sky" is in place! Yu Jingchuan's "China Space Station" in 2013 is about to set sail in the sea of stars

Project management tool Zen

Pycharm writes SQLite statements without code prompts
随机推荐
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
Tp5.1 login configuration method of whether to login public functions (complete login case)
6. Object storage
【C】 Advanced knowledge of file operation
ICO objects in classification
How to use blender to make 360 degree panorama and panoramic video?
Creating elements of DOM series
Cloudrev deploy your own public cloud disk (pagoda installation method)
"Ask the sky" is in place! Yu Jingchuan's "China Space Station" in 2013 is about to set sail in the sea of stars
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
A weird jedisconnectionexception: connection rejected problem
Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
SQL recursive follow-up
BGP introduction
Ctfshow misc introduction
Automatic backup of Linux server PostgreSQL database
Using ODBC to consume SAP ABAP CDs view in Excel
ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
Remote sensing image classification tool and visualization application of WebGIS