当前位置:网站首页>Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
2022-08-09 13:43:00 【Study on programming's note】
在大规模的Jenkins实践中创建项目也是一个问题,如何通过模板自动化的创建Jenkins项目呢? 可以通过安装Job Dsl插件后,通过 Dsl直接创建项目.也可以通过工具将dsl转换为xml,然后再通过Jenkins API创建项目.相对比较第一种方式更加直接一些,由于时间问题今天暂时分享第二种创建项目的方式.
1.根据Job DSL API生成模板
我们需要先安装好Job Dsl 插件,然后执行DSL创建项目.地址:https://jenkinsci.github.io/job-dsl-plugin/

例如: 使用官网的example.这里定义了一个流水线项目,配置了项目的信息包括(项目描述、项目参数、Jenkinsfile地址)
pipelineJob("test-schdule-service") {
description("this is my first job")
keepDependencies(false)
parameters {
choiceParam("test", [1, 2, 3], "")
}
definition {
cpsScm {
scm {
git {
remote {
github("https://gitlab.com/xxx/xxx.git", "https")
credentials("24982560-17fc-4589-819b-bc5bea89da77")
}
branch("*/master")
}
}
scriptPath("Jenkinsfile")
}
}
disabled(false)
}2.通过Playground转换DSL -> XML
url: http://job-dsl.herokuapp.com/

3.通过Jenkins Core Api创建项目
import javax.xml.transform.stream.StreamSource
import jenkins.model.Jenkins
//创建项目
void createOrUpdateJob(String name, String xml) {
def j = Jenkins.instance
String fullName = name
if(name.contains('/')) {
j = j.getItemByFullName(name.tokenize('/')[0..-2])
name = name.tokenize('/')[-1]
}
Jenkins.checkGoodName(name)
if(j.getItem(name) == null) {
println "Created job \"${fullName}\"."
j.createProjectFromXML(name, new ByteArrayInputStream(xml.getBytes()))
j.save()
}
else if(j.getItem(name).configFile.asString().trim() != xml.trim()) {
j.getItem(name).updateByXml(new StreamSource(new ByteArrayInputStream(xml.getBytes())))
j.getItem(name).save()
println "Job \"${fullName}\" already exists. Updated using XML."
}
else {
println "Nothing changed. Job \"${fullName}\" already exists."
}
}
try {
//just by trying to access properties should throw an exception
itemName == null
xmlData == null
isPropertiesSet = true
} catch(MissingPropertyException e) {
println 'ERROR Can\'t create job.'
println 'ERROR Missing properties: itemName, xmlData'
return
}
String xmlData = """<!-- 1. test-schdule-service -->
<flow-definition>
<actions></actions>
<description>this is my first job</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.ChoiceParameterDefinition>
<choices class='java.util.Arrays$ArrayList'>
<a class='string-array'>
<string>1</string>
<string>2</string>
<string>3</string>
</a>
</choices>
<name>test</name>
<description></description>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
<com.coravy.hudson.plugins.github.GithubProjectProperty>
<projectUrl>https://github.com/https://gitlab.com/xxx/xxx.git/</projectUrl>
</com.coravy.hudson.plugins.github.GithubProjectProperty>
</properties>
<triggers></triggers>
<definition class='org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition'>
<scriptPath>Jenkinsfile</scriptPath>
<lightweight>false</lightweight>
<scm class='hudson.plugins.git.GitSCM'>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/https://gitlab.com/xxx/xxx.git.git</url>
<credentialsId>24982560-17fc-4589-819b-bc5bea89da77</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<configVersion>2</configVersion>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<gitTool>Default</gitTool>
<browser class='hudson.plugins.git.browser.GithubWeb'>
<url>https://github.com/https://gitlab.com/xxx/xxx.git/</url>
</browser>
</scm>
</definition>
<disabled>false</disabled>
</flow-definition>
"""
String itemName = "my-first-pipeline"
createOrUpdateJob(itemName, xmlData)4.通过Jenkins Script Console运行

创建完成

边栏推荐
- 十进制数字→十六进制字符
- 关于Retrofit网络请求URL中含有可变参数的处理
- Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
- ansible-cmdb友好展示ansible收集主机信息
- Redis源码剖析之字典(dict)
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
- SQL Server查询优化 (转载非原创)
- 注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
- Yocto 可以下载的第三方库
- 批量读取word docx文件指定表格内容,保存在excel文件中
猜你喜欢

数据挖掘-05

Scala Advanced (7): Collection Content Summary (Part 1)

5G China unicom AP:B SMS ASCII 转码要求

绘制混合密度函数图以及添加分位数线

透明tune proxy

WSA toolkit installed app store tip doesn't work how to solve?

Flutter入门进阶之旅(八)Button Widget

注释、关键字、标识符的区别你知道吗?

Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇

ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
随机推荐
26. Pipeline parameter substitution command xargs
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
陈强教授《机器学习及R应用》课程 第十三章作业
Yocto 可以下载的第三方库
陈强教授《机器学习及R应用》课程 第十六章作业
Resolved IndentationError: unindent does not match any oute r indentation Level
read stream 特别注意
ctfshow七夕杯2022
Data Mining-05
ARM板卡增加路由功能
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
陈强教授《机器学习及R应用》课程 第十七章作业
【HCIP持续更新】IS-IS协议原理与配置
陈强教授《机器学习及R应用》课程 第十四章作业
无重复字符的最长子串
Redis源码剖析之robj(redisObject)
十进制数字→十六进制字符
30行代码实现微信朋友圈自动点赞
kustomize入门示例及基本语法使用说明
造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片