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

开发 前端
在大规模的Jenkins实践中创建项目也是一个问题,如何通过模板自动化的创建Jenkins项目呢? 可以通过安装Job Dsl插件后,通过 Dsl直接创建项目。也可以通过工具将dsl转换为xml,然后再通过Jenkins API创建项目。

[[373964]]

 在大规模的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地址)

  1. pipelineJob("test-schdule-service") { 
  2.   description("this is my first job"
  3.   keepDependencies(false
  4.   parameters { 
  5.     choiceParam("test", [1, 2, 3], ""
  6.   } 
  7.   definition { 
  8.     cpsScm { 
  9.       scm { 
  10.         git { 
  11.           remote { 
  12.             github("https://gitlab.com/xxx/xxx.git""https"
  13.             credentials("24982560-17fc-4589-819b-bc5bea89da77"
  14.           } 
  15.           branch("*/master"
  16.         } 
  17.       } 
  18.       scriptPath("Jenkinsfile"
  19.     } 
  20.   } 
  21.   disabled(false

2.通过Playground转换DSL -> XML

url: http://job-dsl.herokuapp.com/


3.通过Jenkins Core Api创建项目

  1. import javax.xml.transform.stream.StreamSource 
  2. import jenkins.model.Jenkins 
  3.  
  4. //创建项目 
  5. void createOrUpdateJob(String name, String xml) { 
  6.     def j = Jenkins.instance 
  7.     String fullName = name 
  8.     if(name.contains('/')) { 
  9.         j = j.getItemByFullName(name.tokenize('/')[0..-2]) 
  10.         name = name.tokenize('/')[-1] 
  11.     } 
  12.     Jenkins.checkGoodName(name
  13.     if(j.getItem(name) == null) { 
  14.         println "Created job \"${fullName}\"." 
  15.         j.createProjectFromXML(name, new ByteArrayInputStream(xml.getBytes())) 
  16.         j.save() 
  17.     } 
  18.     else if(j.getItem(name).configFile.asString().trim() != xml.trim()) { 
  19.         j.getItem(name).updateByXml(new StreamSource(new ByteArrayInputStream(xml.getBytes()))) 
  20.         j.getItem(name).save() 
  21.         println "Job \"${fullName}\" already exists.  Updated using XML." 
  22.     } 
  23.     else { 
  24.         println "Nothing changed.  Job \"${fullName}\" already exists." 
  25.     } 
  26.  
  27. try { 
  28.     //just by trying to access properties should throw an exception 
  29.     itemName == null 
  30.     xmlData == null 
  31.     isPropertiesSet = true 
  32. } catch(MissingPropertyException e) { 
  33.     println 'ERROR Can\'t create job.' 
  34.     println 'ERROR Missing properties: itemName, xmlData' 
  35.     return 
  36.  
  37. String xmlData = """<!-- 1. test-schdule-service --> 
  38. <flow-definition> 
  39.     <actions></actions> 
  40.     <description>this is my first job</description> 
  41.     <keepDependencies>false</keepDependencies> 
  42.     <properties> 
  43.         <hudson.model.ParametersDefinitionProperty> 
  44.             <parameterDefinitions> 
  45.                 <hudson.model.ChoiceParameterDefinition> 
  46.                     <choices class='java.util.Arrays$ArrayList'
  47.                         <a class='string-array'
  48.                             <string>1</string> 
  49.                             <string>2</string> 
  50.                             <string>3</string> 
  51.                         </a> 
  52.                     </choices> 
  53.                     <name>test</name
  54.                     <description></description> 
  55.                 </hudson.model.ChoiceParameterDefinition> 
  56.             </parameterDefinitions> 
  57.         </hudson.model.ParametersDefinitionProperty> 
  58.         <com.coravy.hudson.plugins.github.GithubProjectProperty> 
  59.             <projectUrl>https://github.com/https://gitlab.com/xxx/xxx.git/</projectUrl> 
  60.         </com.coravy.hudson.plugins.github.GithubProjectProperty> 
  61.     </properties> 
  62.     <triggers></triggers> 
  63.     <definition class='org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition'
  64.         <scriptPath>Jenkinsfile</scriptPath> 
  65.         <lightweight>false</lightweight> 
  66.         <scm class='hudson.plugins.git.GitSCM'
  67.             <userRemoteConfigs> 
  68.                 <hudson.plugins.git.UserRemoteConfig> 
  69.                     <url>https://github.com/https://gitlab.com/xxx/xxx.git.git</url> 
  70.                     <credentialsId>24982560-17fc-4589-819b-bc5bea89da77</credentialsId> 
  71.                 </hudson.plugins.git.UserRemoteConfig> 
  72.             </userRemoteConfigs> 
  73.             <branches> 
  74.                 <hudson.plugins.git.BranchSpec> 
  75.                     <name>*/master</name
  76.                 </hudson.plugins.git.BranchSpec> 
  77.             </branches> 
  78.             <configVersion>2</configVersion> 
  79.             <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations> 
  80.             <gitTool>Default</gitTool> 
  81.             <browser class='hudson.plugins.git.browser.GithubWeb'
  82.                 <url>https://github.com/https://gitlab.com/xxx/xxx.git/</url> 
  83.             </browser> 
  84.         </scm> 
  85.     </definition> 
  86.     <disabled>false</disabled> 
  87. </flow-definition> 
  88. ""
  89. String itemName = "my-first-pipeline" 
  90.  
  91. createOrUpdateJob(itemName, xmlData) 

4.通过Jenkins Script Console运行


创建完成


 

责任编辑:姜华 来源: DevOps云学堂
相关推荐

2010-01-27 10:32:40

Visual Stud

2011-10-31 13:58:32

API

2016-06-30 11:25:52

VisualNET开发

2011-05-11 10:12:05

Mobl

2019-01-21 14:20:26

Java开发代码

2013-06-13 09:21:31

RESTful APIRESTfulAPI

2016-12-27 08:49:55

API设计策略

2022-03-29 10:04:44

APIHarmony文件管理

2010-02-22 09:09:02

Visual Stud

2022-02-10 23:38:23

API架构设计

2021-03-12 00:04:52

网关Api

2021-07-26 05:21:37

JenkinsAndroid自动化

2021-02-22 09:00:00

Jenkins工具开发

2010-07-13 14:02:05

SQL Server创

2017-03-13 14:09:19

RESTful API实践

2023-05-04 16:08:43

2023-11-07 07:08:57

2019-04-30 09:00:33

SQL数据库Apache Flin

2022-09-20 07:33:15

Jenkinshttps://mp

2022-03-21 09:40:48

TektonJenkinsPipeline
点赞
收藏

51CTO技术栈公众号