jenkins中如何集成commander应用的完整步骤

时间:2022-11-29 08:31:01

前言

jenkins 是一款流行的开源持续集成(continuous integration)工具,广泛用于项目开发,具有自动化构建、测试和部署等功能。

最近参加公司的集成测试平台的开发,在开发中遇到了不少问题,两个星期的迭代也即将完成,在这也用这篇博客记录下开发中的问题,供读者参考

公司的应用较多,所以需要了解这几种应用在jenkins中如何做构建,我自己参与的有两种commander的应用,一种是大数据类的,一个是我们服务端架构组的scala应用

1、大数据应用bigdata

配置如下:

jenkins中如何集成commander应用的完整步骤

jenkins中如何集成commander应用的完整步骤

配置文件对应的xml文件:通过crul获取xml配置文件:http://host/job/tar_py_dwx_dev/config.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<project>
<actions/>
<description/>
<keepdependencies>false</keepdependencies>
<properties>
<com.dabsquared.gitlabjenkins.connection.gitlabconnectionproperty plugin="gitlab-plugin@1.5.5">
<gitlabconnection/>
</com.dabsquared.gitlabjenkins.connection.gitlabconnectionproperty>
<hudson.plugins.promoted__builds.jobpropertyimpl plugin="promoted-builds@3.1">
<activeprocessnames>
<string>deploy dev</string>
</activeprocessnames>
</hudson.plugins.promoted__builds.jobpropertyimpl>
</properties>
<scm class="hudson.plugins.git.gitscm" plugin="git@3.8.0">
<configversion>2</configversion>
<userremoteconfigs>
<hudson.plugins.git.userremoteconfig>
<url>ssh://git@172.0.10.182:10022/bigdata/dwx.git</url>
<credentialsid>84f4be19-ea8d-4271-8cfb-42af8f507285</credentialsid>
</hudson.plugins.git.userremoteconfig>
</userremoteconfigs>
<branches>
<hudson.plugins.git.branchspec>
<name>*/develop</name>
</hudson.plugins.git.branchspec>
</branches>
<dogeneratesubmoduleconfigurations>false</dogeneratesubmoduleconfigurations>
<submodulecfg class="list"/>
<extensions/>
</scm>
<assignednode>!macmini</assignednode>
<canroam>false</canroam>
<disabled>false</disabled>
<blockbuildwhendownstreambuilding>false</blockbuildwhendownstreambuilding>
<blockbuildwhenupstreambuilding>false</blockbuildwhenupstreambuilding>
<triggers>
<hudson.triggers.scmtrigger>
<spec>h/5 * * * *</spec>
<ignorepostcommithooks>false</ignorepostcommithooks>
</hudson.triggers.scmtrigger>
</triggers>
<concurrentbuild>false</concurrentbuild>
<builders>
<hudson.tasks.shell>
<command>
project=dwx1 cd ${workspace} tar zcvf ${project}.tar.gz * aws s3 cp ${project}.tar.gz s3://lattebank-jenkins-build-dev/${job_base_name}/${build_number}/ --region cn-north-1 rm -rf ${project}.tar.gz
</command>
</hudson.tasks.shell>
</builders>
<publishers/>
<buildwrappers/>
</project>

从xml中获取的信息有点和图中的配置文件有点对应不上

对于promotion的脚本在xml配置文件中是无法获取的,这时候就有一个问题,这种api是无法获取到promotion的的脚本,同时这也给我们的工作带来了极大的挑战,那也意味着单纯的通过这种方法是无法实现commander应用的部署,和通过平台的方式去直接操作jenkins的配置

但经过查询相关的api并不能找到相关的内容,经过不懈的努力,终于找到了和promote build 插件相关的api

查询:http://host/job/jobname/promotion/process/promotionname/config.xml

这个接口能获取到它的xml文件,但是并不能对该配置文件进行增加和修改

对此我自己封装了一些方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * @author chenlang
 * date 2018/5/7
 */
@slf4j
public class jenkinspromotionutils {
 
 
 private static final string sub_path_promotion_coommand = "/hudson.plugins.promoted__builds.promotionprocess";
 private static final string sub_path_build = "/buildsteps";
 private static final string sub_path_builder_shell_command = "/hudson.tasks.shell/command";
 private static final string path_promotion_command = sub_path_promotion_coommand + sub_path_build + sub_path_builder_shell_command;
 private static string create_promotion_json = "{'properties':{'stapler-class-bag':'true','hudson-plugins-promoted_builds-jobpropertyimpl':{'promotions':{'activeitems':{'name':'%s','isvisible':'','icon':'star-gold','hasassignedlabel':false,'assignedlabelstring':'','conditions':{'stapler-class-bag':'true'}}}}}}";
 private static final string content_type = "application/x-www-form-urlencoded";
 
 
 public static void updatepromotionshell(document jobconfigdocument, string jobname, jenkinspromotionclient jenkinspromotionclient, string promotionshell, string path) throws ioexception, documentexception {
 if (stringutils.isblank(promotionshell)) {
  return;
 }
 string promotionname = getpromotionname(jobconfigdocument, path);
 document document = jenkinspromotionclient.getjobpromotionxml(jobname, promotionname);
 document.selectsinglenode(path_promotion_command).settext(promotionshell);
 jenkinspromotionclient.updatejob(jobname, promotionname, document.asxml());
 }
 
 public static void createpromotionshell(document jobconfigdocument, string tmpjobname, string jobname, string promotionshell, string path, jenkinspromotionclient jenkinspromotionclient) throws ioexception, documentexception {
 if (stringutils.isblank(promotionshell)) {
  return;
 }
 string promotionname = getpromotionname(jobconfigdocument, path);
 document document = jenkinspromotionclient.getjobpromotionxml(tmpjobname, promotionname);
 document.selectsinglenode(path_promotion_command).settext(promotionshell);
 map<string, string> map = maps.newhashmap();
 map.put("content-type", content_type);
 map.put("json", string.format(create_promotion_json, promotionname));
 try {
  jenkinspromotionclient.createjob(jobname, map);
 } catch (exception e) {
  log.error("初创promotion时失败" + e);
 }
 jenkinspromotionclient.createjob(jobname, promotionname, document.asxml());
 }
 
 public static string getpromotionname(document jobconfigdocument, string path) {
 return jobconfigdocument.selectsinglenode(path).gettext();
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cn.caijiajia.phoenix.service.jenkins;
import com.offbytwo.jenkins.client.jenkinshttpclient;
import com.offbytwo.jenkins.client.util.encodingutils;
import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.documenthelper;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
import java.io.ioexception;
import java.util.map;
 
/**
 * @author chenlang
 * date 2018/5/4
 */
@component
public class jenkinspromotionclient {
 
 @autowired
 private jenkinshttpclient jenkinshttpclient;
 
 public jenkinspromotionclient() {
 }
 
 /**
 * 获取job的promotion配置文件
 *
 * @param jobname job名称
 * @param promotionname promotion名称
 * @return
 * @throws ioexception
 */
 public document getjobpromotionxml(string jobname, string promotionname) throws ioexception, documentexception {
 return documenthelper.parsetext(this.getjobxml(jobname, promotionname));
 }
 
 
 /**
 * 更新job
 *
 * @param jobname
 * @param promotionname
 * @param jobxml
 * @throws ioexception
 */
 public void updatejob(string jobname, string promotionname, string jobxml) throws ioexception {
 this.jenkinshttpclient.post_xml(this.tojobbaseurl(jobname, promotionname) + "/config.xml", jobxml, true);
 }
 
 /**
 * 添加job脚本
 *
 * @param jobname
 * @param jobxml
 * @throws ioexception
 */
 public void createjob(string jobname, string promotionname, string jobxml) throws ioexception {
 this.jenkinshttpclient.post_xml(this.tojobbaseurl(jobname, promotionname) + "/config.xml", jobxml, true);
 }
 
 /**
 * 添加promotion的job
 *
 * @param jobname
 * @param map
 * @throws ioexception
 */
 public void createjob(string jobname, map map) throws ioexception {
 this.jenkinshttpclient.post_form("/job/" + encodingutils.encode(jobname) + "/configsubmit?", map, false);
 }
 
 private string getjobxml(string jobname, string promotionname) throws ioexception {
 return this.jenkinshttpclient.get(this.tojobbaseurl(jobname, promotionname) + "/config.xml");
 }
 
 private string tojobbaseurl(string jobname, string promotionname) {
 return "/job/" + encodingutils.encode(jobname) + "/promotion/process/" + promotionname;
 }
 
 /**
 * promotion脚本的构建
 * @param jobname
 * @param promotionname
 * @param version
 * @param isfirstbuild
 * @throws ioexception
 */
 public void build(string jobname,string promotionname,integer version,boolean isfirstbuild) throws ioexception{
 if (isfirstbuild) {
  this.jenkinshttpclient.post("/job/"+ encodingutils.encode(jobname) + "/"+version+"/promotion/forcepromotion?name="+promotionname+"&json=%7b%7d&submit=force promotion");
 } else {
  this.jenkinshttpclient.post("/job/"+ encodingutils.encode(jobname) + "/"+version+"/promotion/"+promotionname+"/build?json=%7b%7d&submit=re-execute promotion");
 }
 }
}

其中的方法封装了对promote build插件中关于配置的增删改查,以及promotion脚本的构建

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.cnblogs.com/clovejava/archive/2018/05/13/9033704.html