调用Jenkins接口api的几个例子

Posted wozijisun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用Jenkins接口api的几个例子相关的知识,希望对你有一定的参考价值。

记录瞬间

近期操作Jenkins调用比较多,当然Jenkins本身也提供了jenkins-cli.jar的使用方法,可以直接通过命令行进行调用,

但是,由于不想引入太多的jar包,导致直接使用Jenkins api需求强烈

下面就把近期收集到的一些常见用法做一个简单总结,希望对初学者有所帮助。

 

9、直接调用Jenkins的job API进行构建的方法
Simple example - sending "String Parameters":

curl -X POST JENKINS_URL/job/JOB_NAME/build   --user USER:TOKEN   --data-urlencode json={"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}
Another example - sending a "File Parameter":

curl -X POST JENKINS_URL/job/JOB_NAME/build   --user USER:PASSWORD   --form file0=@PATH_TO_FILE   --form json={"parameter": [{"name":"FILE_LOCATION_AS_SET_IN_JENKINS", "file":"file0"}]}
 
E.g.curl -X POST http://JENKINS_URL/job/JOB_NAME/build  --form file0=@/home/user/Desktop/sample.xml --form json={"parameter": [{"name":"harness/Task.xml", "file":"file0"}]}
Please note, in this example, the symbol @ is important to mention. Also, the path to the file is absolute path.
In order to make this command work, you need to configure your Jenkins job to take a file parameter and name in this command corresponds to file location field in the Jenkins job configuration.
 
In above example, harness is just a name of folder that may not exist in your workspace, you can write "name":"Task.xml" and it will place the Task.xml at root of your workspace.
Remember that name in this command should match with File location in file parameter in job configuration.


一个插件,可以通过此插件传参并调用Jenkins上的其他任务
Trigger/call builds on other projects
     Build Triggers


2.1运行jenkins-job
2.1.1无参任务curl -X POST http://localhost:8080/jenkins/job/plugin%20demo/build --user admin:admin
2.1.2含参任务
不设置参数/使用默认参数curl -X POST http://localhost:8080/jenkins/job/commandTest/buildWithParameters --user admin:admin
2.1.3设置参数方法1curl -X POST http://localhost:8080/jenkins/job/commandTest/buildWithParameters -d port=80
2.1.4设置参数方法2curl -X POST http://localhost:8080/jenkins/job/commandTest/buildWithParameters -d port=80 --data-urlencode json="{"parameter": [{"name": "port", "value": "80"}]}”
2.1.5多参数http://localhost:8080/jenkins/job/commandTest/buildWithParameters -d param1=value1&param2=value
2.2 创建job
2.2.1 需创建目录
1).创建job目录~/.jenkins/jobs/jobfromcmd
2).创建config.xml文件(可从其他工程中复制)
3).运行命令curl -X POST http://localhost:8080/jenkins/createItem?name=jobfromcmd --user admin:admin --data-binary "@config.xml" -H "Content-Type: text/xml”
2.2.2 不需创建目录
1).创建config.xml文件(可从其他工程中复制)
2).运行命令(在config.xml同一目录下)curl -X POST http://localhost:8080/jenkins/createItem?name=jobfromcmd --user admin:admin --data-binary "@config.xml" -H "Content-Type: text/xml”
2.2.3直接使用控制台,不需创建xml文件(将xml内容写入控制台中运行)echo <?xml version="1.0" encoding="UTF-8"?><project>…</project> | curl -X POST -H Content-type:text/xml -d @- http://localhost:8080/jenkins/createItem?name=jobfromcmd
2.3 删除job curl -X POST http://localhost:8080/jenkins/job/jobfromcmd/doDelete
2.4 查询job的状态 curl --silent ${JENKINS_SERVER}/job/JOB_NAME/lastBuild/api/json
2.5 自动disable Project: curl --user ${UserName}:${PASSWORD} -o /dev/null --data disable JENKINS_URL/job/JOBNAME/disable
2.6获取build的num curl --silent ${JENKINS_SERVER}/job/JOB_NAME/lastBuild/buildNumber
2.7获取最近成功的build的num curl --silent ${JENKINS_SERVER}/job/JOB_NAME/lastStableBuild/buildNumber

2.8获取某一次构建结果信息 curl -o build.tmp2 -s --header n:${newbuild} ${jobPage}buildHistory/ajax
构建中:
<img height="16" alt="pending &gt; Console Output" width="16" src="/static/ea09c638/images/16x16/grey_anime.gif" tooltip="pending &gt; Console Output" />
排队中:
<img height="16" alt="In progress &gt; Console Output" width="16" src="/static/ea09c638/images/16x16/grey_anime.gif" tooltip="In progress &gt; Console Output" />
成功:           alt="成功 &gt; 控制台输出"
<img height="16" alt="Success &gt; Console Output" width="16" src="/static/ea09c638/images/16x16/blue.png" tooltip="Success &gt; Console Output" />
警告:
<img height="16" alt="Unstable &gt; Console Output" width="16" src="/static/ea09c638/images/16x16/yellow.png" tooltip="Unstable &gt; Console Output" />
失败:
<img height="16" alt="Aborted &gt; Console Output" width="16" src="/static/ea09c638/images/16x16/grey.png" tooltip="Aborted &gt; Console Output" />

l = ["123", "234", "098"]
for i in l:
    jenkins_url = "http://127.0.0.1:8080/jenkins/job/test/lastBuild/buildNumber"
    result = requests.get(jenkins_url)
    print("last build is : " + result.text)
    buildNumber = result.text

    j_url = "http://127.0.0.1:8080/jenkins/job/test/buildWithParameters"
    j_data = {"test": i}

    result = requests.post(j_url, data=j_data)
    print(result.text)

    while 1:
        jenkins_url = "http://127.0.0.1:8080/jenkins/job/test/buildHistory/ajax"
        headers = {"n": str(int(buildNumber) + 1)}
        print(headers)
        result = requests.get(jenkins_url, headers=headers)
        if alt="成功 &gt; 控制台输出" in result.text or                 alt="Success &gt; Console Output" in result.text:
            print("Yes !!!!!...")
            break
        else:
            time.sleep(2)
            print("sleep 1 second.")
            print(result.text)
其中可以直接将token放到url链接中,格式如下:
http://USER:API_TOKEN@Jenkins_IP:8080

Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request
方法一(不推荐):
在jenkins 的Configure Global Security下 , 取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits)”的勾选

方法二:
1、获取用户API token
http://Jenkins_IP:8080/user/zhangyi/configure
点击 show API Token,假设是API_TOKEN
2、计算CRUMB
CRUMB=$(curl -s http://USER:API_TOKEN@Jenkins_IP:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb))
3、请求时附带CRUMB信息即可

curl -X POST -H "$CRUMB" http://USER:API_TOKEN@cd.web.tc.ted:8080/reload
 


可以使用如下链接对特定的job进行远程调用。
curl -X POST http://192.168.43.130:8080/jenkins/job/test/build --user xxx --data-urlencode json={"parameter":[{"name":"v1","value":"123123"},{"name":"v2","value":"asdfghjkl"},{"name":"v3","value":"qwertyuiop"}]}
获取最后一次执行的状态。
str=`curl --silent  http://192.168.43.130:8080/jenkins/job/test/lastBuild/api/json --user xxx | sed s/,/ /g | sed -r s/.*"result":"([^ ]+)" .*/1/`
echo $str
FAILURE 或者 SUCCESS 或者 null

 

以上是关于调用Jenkins接口api的几个例子的主要内容,如果未能解决你的问题,请参考以下文章

Python调用API接口的几种方式

流程控制

API调用的几种类型

如何防止api接口被恶意调用或攻击

总结一下微信扫码支付小程序支付的几个坑

速码验证码接码平台API接入示例说明