将学习变成乐趣,一直在路上
每天1990

导航

 
背景| 组内做UI测试,需要每天晚上执行一遍jenkins任务,jenkins任务本身是参数化构建的。但是因为jenkins本身的定时执行没有办法指定特殊的参数,所以考虑使用命令行方式启动jenkins
 

第一步:下载jenkins-cli.jar,查看帮助文档

不同版本的jenkins有自己对应命令行版本jar,所以最好从jenkins机上命令行说明页下载jar
 
访问jenkins的命令行说明页:http://192.168.111.111:8080/jenkins/cli
 
该页面可下载jenkins-cli.jar,并且介绍了一些命令
1.查看jenkins-cli.jar命令的帮助
java -jar jenkins-cli.jar -s http://192.168.111.111:8080/jenkins/ -help
 
如果想查看具体的某个jenkins-cli命令,可以在-help加上command
例如:查看build的具体使用方法
java -jar jenkins-cli.jar -s http://192.168.111.111:8080/jenkins/  help build
 
注意:最好用jenkins的ip加端口访问,我这里用域名访问会超时
 

第二步.使用build命令构建一个JOB

java -jar jenkins-cli.jar -s http://jenkinsurl  build JOBNAME -p tag=xxx --username xxx --password xxx
 
说明:
1.build后面直接跟JOB的名字
2.-p后面跟参数化构建的参数,使用key=value格式。如果有多个参数就写多个-p
3.—username和--password提供jenkins的账号密码
 
例如,执行jenkins的命令如下:
java -jar jenkins-cli.jar -s http://1192.168.111.111:8080/jenkins/  build UITest -p tag=20170922 -p ifRunUI=true -p  --username tester --password 123456
 
build的使用方法:
JOB : Name of the job to build
-c  : Check for SCM changes before starting the build, and if there's no
       change, exit without doing a build
-f  : Follow the build progress. Like -s only interrupts are not passed
       through to the build.
-p  : Specify the build parameters in the key=value format.
-s  : Wait until the completion/abortion of the command. Interrupts are passed
       through to the build.
-v  : Prints out the console output of the build. Use with -s
-w  : Wait until the start of the command
[root(sunmin06@58OS)@bjm6-193-96 script]# java -jar jenkins-cli.
 

其他:获得历史构建的参数

在命令行执行时,参数我们可能需要历史构建的参数
 
获取上次构建结果:
curl "http://192.168.111.111:8080/jenkins/view/app/job/JOBNAME/lastBuild/api/xml”
 
然后可以解析结果,可以通过expr在shell中解析,获得需要的参数,例如获得tag:
expr "$result" : '.*<name>tag</name><value>\([a-zA-Z0-9_-]*\)</value></parameter>'
 
说明:$result为请求上次构建的结果,tag这里匹配的是字母数字和下划线中横线
 
 
更多获取构建结果的文章参考:http://blog.csdn.net/ljj_9/article/details/70270977
posted on 2017-09-28 23:51  每天1990  阅读(14133)  评论(5编辑  收藏  举报