Jenkins插件开发

MuYusen 于 2023-10-29 发布 本文总阅读量

背景

直白点:Jenkins的进阶操作,让你可以更随心所欲的定义Jenkins的行为。

开发环境

创建一个插件

mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:"

注意:执行该命令可能会得到以下结果,并不出现提示交互,也不会生成maven项目。

[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] Your filter doesn't match any archetype, so try again with another value.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.071 s
[INFO] Finished at: 2023-10-28T14:06:56+08:00
[INFO] ------------------------------------------------------------------------

这是因为maven在本地仓库根目录archetype-catalog.xml文件中没找到groupIdio.jenkins.archetypes的archetype,之后会在远端仓库的该文件中查找,如果还是没找到则出现上述问题。 解决方法:将archetype-catalog.xml文件下载到本地Maven仓库根目录即可 (注意,这不是指的安装路径,而是mvn运行时默认的存放repository的路径,一般在用户根目录下的一个隐藏目录,~/.m2。),获取地址: https://repo1.maven.org/maven2/archetype-catalog.xml

重新执行命令:

10066 ◯ : mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:" 

...
Choose archetype:
1: remote -> io.jenkins.archetypes:empty-plugin (Skeleton of a Jenkins plugin with a POM and an empty source tree.)
2: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.)
3: remote -> io.jenkins.archetypes:global-shared-library (Uses the Jenkins Pipeline Unit mock library to test the usage of a Global Shared Library)
4: remote -> io.jenkins.archetypes:hello-world-plugin (Skeleton of a Jenkins plugin with a POM and an example build step.)
5: remote -> io.jenkins.archetypes:scripted-pipeline (Uses the Jenkins Pipeline Unit mock library to test the logic inside a Pipeline script.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 4
Choose io.jenkins.archetypes:hello-world-plugin version: 
1: 1.1
2: 1.2
3: 1.3
4: 1.4
5: 1.5
6: 1.6
7: 1.7
8: 1.8
9: 1.9
10: 1.10
11: 1.11
12: 1.12
13: 1.13
14: 1.14
15: 1.15
16: 1.16
17: 1.17
18: 1.19
19: 1.20
20: 1.21
Choose a number: 20: 20
...
[INFO] Using property: groupId = unused
[INFO] Using property: package = io.jenkins.plugins.sample                                              
[INFO] Using property: hostOnJenkinsGitHub = true                                                        
Define value for property 'artifactId': demo                                                             
Define value for property 'version' 1.0-SNAPSHOT: :                                                                                                                                                                
Confirm properties configuration:                                                                        
groupId: unused                                                                                          
package: io.jenkins.plugins.sample
hostOnJenkinsGitHub: true                                                                                
artifactId: demo             
version: 1.0-SNAPSHOT                        
 Y: : y         
mv demo demo-plugin
cd demo-plugin
mvn verify
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:40 min
[INFO] Finished at: 2023-10-29T14:20:48+08:00
[INFO] ------------------------------------------------------------------------

生成并运行插件

Maven 的HPI插件用于构建和打包Jenkins插件的。它还提供了一种使用插件运行Jenkins实例的方便方法:

mvn hpi:run

#默认端口是8080,可以使用 -Dport指定端口
mvn hpi:run -Dport=5000

命令执行后,如果显示 Jenkins is fully up and running,浏览器打开http://localhost:5000/jenkins/,查看插件功能。

PS:插件目录下面的work/目录,就是Jenkins的home目录。

Console Output

Started by user unknown or anonymous
Running as SYSTEM
Building in workspace /home/zhaofusen/eclipse-workspace/demo-plugin/work/workspace/test_plugin
Hello, Hello 2024!
Finished: SUCCESS

PS:终端执行Ctrl + C,可以停止Jenkins。

参考

参考