实践: Jenkins Core Api & Job DSL创建项目

系统运维2025-11-05 06:32:583622

 在大规模的实践Jenkins实践中创建项目也是一个问题,如何通过模板自动化的创建创建Jenkins项目呢? 可以通过安装Job Dsl插件后,通过 Dsl直接创建项目。项目也可以通过工具将dsl转换为xml,亿华云计算实践然后再通过Jenkins API创建项目。创建相对比较第一种方式更加直接一些,项目由于时间问题今天暂时分享第二种创建项目的实践方式。

1.根据Job DSL API生成模板

我们需要先安装好Job Dsl 插件,创建然后执行DSL创建项目。云服务器项目地址:https://jenkinsci.github.io/job-dsl-plugin/

例如: 使用官网的实践example。这里定义了一个流水线项目,创建配置了项目的项目信息包括(项目描述、项目参数、云南idc服务商实践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 Cant 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运行

创建完成

本文地址:http://www.bzuk.cn/html/57f33999603.html
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

全站热门

电光火石电脑安装教程(为您解析电脑组装步骤,让您成为电脑高手)

华硕系统装机教程(华硕系统装机教程,让你成为电脑大师)

揭秘鼠标光学分辨率dpi的奥秘(了解dpi的重要性与工作原理,提升鼠标操作体验)

1分钟教你Word流程图制作方法(快速掌握Word绘制流程图的技巧)

使用U盘进行Win10重装的教程(简单易行的步骤,让你轻松重装系统)

大白菜pe制作工具教程(教你如何使用大白菜pe制作工具,快速实现创意想法)

神舟K650DI7D3(领先科技助力畅快体验一款值得期待的高性能电脑)

扫描件修复教程(以扫描件修复教程帮您快速修复文件损坏问题)

友情链接

滇ICP备2023006006号-33