Article
命令行调用Jenkins2.63打包
Jenkins给集成打包带来了很多的便捷,让不懂开发的同事也能轻松的打包。但是对于开发和运维来说,可能还需要在打包之外做一些事情,以及批量的处理N个打包。
对于研发来说,重复是最难忍受的。Jenkins可以直接通过api来调用查看和处理各种请求。
网络上资料其实挺多的。也有直接一个脚本直接搞定部署的。知其然知其所以然,还是需要自己下功夫理解人家的脚本这样才能更好的用(先不说自己写了)。主要的就是三个步骤:
- 怎么登陆: JenkinsScriptConsole-Remoteaccess .|. RemoteaccessAPI-CSRFProtection
- 执行build:Running jenkins jobs via command line .|. Triggering Jenkins builds by URL
- 检查结果:checkJenkins.sh
# crumb
首先来看看crumb是啥
[root@iZ9416vn227Z opt]# curl -X POST $JENKINS_PROJ_AUTH_URL/build
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /job/helloworld/build. Reason:
<pre> No valid crumb was included in the request</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>
</body>
</html>
这里CSRF 相当于jenkins做的一个权限控制,有两种方式处理:
方法一:取消控制
在菜单 系统管理 –> Configure Global Security 中调整设置: 取消 防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits) 的勾选。 如果还坚持要启用“防止跨站点请求伪造”,就需要先动态获取crumb。
方法二:获取token
- How to request for Crumb issuer for jenkins
- Calling a jenkins job with a bash script
- https://support.cloudbees.com/hc/en-us/articles/218889337-How-to-build-a-job-using-the-REST-API-and-cURL-
通过URL: crumbIssuer/api/json 获取token的键值,然后把它附加到build请求的HEADER。
# 命令行通过URL请求jenkins进行编译
- 使用shell脚本curl调用jenkins进行构建并判断是否构建成功
- Remote access API
- https://wiki.jenkins.io/display/JENKINS/Authenticating+scripted+clients
- https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
JENKINS_ID="admin:PASSWORD"
JENKINS_PROJ_AUTH_URL=http://$JENKINS_ID@localhost:18080/job/helloworld
JENKINS_PROJ_URL=http://localhost:18080/job/helloworld
curl $JENKINS_PROJ_AUTH_URL/lastBuild/api/json
#Get the current configuration and save it locally
curl -X GET $JENKINS_PROJ_URL/config.xml
curl 'http://'$JENKINS_ID'@localhost:18080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
Jenkins-Crumb:a4296173a91d900c11af07d932559fcd
curl -X POST -H "Jenkins-Crumb:a4296173a91d900c11af07d932559fcd" $JENKINS_PROJ_AUTH_URL/build
curl -s $JENKINS_PROJ_AUTH_URL/lastBuild/api/json | jq .
# --- TODO ---
progress(排队中)|pending(构建中),每三秒去重新获取结果进行判断
while grep -qE "In progress|pending" build.tmp2;
if grep -qE "Success" build.tmp2 ;then
elif grep -qE "Unstable" build.tmp2 ;then
elif grep -qE "Failed|Aborted" build.tmp2 ;then
echo "#Open Link: ${jobPage}${newbuild}/console see details"
BuildName
- https://wiki.jenkins.io/display/JENKINS/Build+Name+Setter+Plugin
- https://stackoverflow.com/questions/42172320/how-to-set-the-jenkins-build-name-based-on-some-conditions
- https://stackoverflow.com/questions/30111298/how-to-use-build-name-setter-plugin
jenkins的使用案例
# 参考
API使用
- https://gist.githubusercontent.com/julianchurchill/8780920/raw/ae3ab0c120857b0fe69fe3718d720cb4ef94c4b8/checkJenkins.sh
- Triggering Jenkins builds by URL
登录/权限问题
- https://stackoverflow.com/questions/10698419/how-can-a-jenkins-user-authentication-details-be-passed-to-a-script-which-uses
- http://www.scmgalaxy.com/tutorials/ways-to-login-jenkins-using-command-line
- https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console#JenkinsScriptConsole-Remoteaccess
- Calling a jenkins job with a bash script
- No valid crumb was included in the request in kubernetes
–END
Related
Related posts
-
develop-environment-prepare
2017-01-25
-
hiera and facts
2016-05-03
-
杀鸡焉用牛刀:DuckDB 正取代部分 Spark 场景
2026-02-16
-
n8n 终于还是部署到 Docker 了,经验就是要反反复复地去验证:要想少走弯路,就按官方推荐的最佳实践
2025-12-29