实践:JenkinsCoreApi&JobDSL创建项目

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

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元湛江做网站,已为上家服务,为湛江各地企业和个人服务,联系电话:18982081108

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 = """ 
  38.  
  39.      
  40.     this is my first job 
  41.     false 
  42.      
  43.          
  44.              
  45.                  
  46.                      
  47.                          
  48.                             1 
  49.                             2 
  50.                             3 
  51.                          
  52.                      
  53.                     test 
  54.                      
  55.                  
  56.              
  57.          
  58.          
  59.             https://github.com/https://gitlab.com/xxx/xxx.git/ 
  60.          
  61.      
  62.      
  63.      
  64.         Jenkinsfile 
  65.         false 
  66.          
  67.              
  68.                  
  69.                     https://github.com/https://gitlab.com/xxx/xxx.git.git 
  70.                     24982560-17fc-4589-819b-bc5bea89da77 
  71.                  
  72.              
  73.              
  74.                  
  75.                     */master 
  76.                  
  77.              
  78.             2 
  79.             false 
  80.             Default 
  81.              
  82.                 https://github.com/https://gitlab.com/xxx/xxx.git/ 
  83.              
  84.          
  85.      
  86.     false 
  87.  
  88. """ 
  89. String itemName = "my-first-pipeline" 
  90.  
  91. createOrUpdateJob(itemName, xmlData) 

4.通过Jenkins Script Console运行

创建完成

网页题目:实践:JenkinsCoreApi&JobDSL创建项目
网页地址:http://www.shufengxianlan.com/qtweb/news9/1409.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联