命令行執行jenkins,構建job(可傳遞參數)


背景| 組內做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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM