调用Jenkins接口api的几个例子
2021-03-19 17:24
标签:post another 写入 admin back 推荐 警告 使用方法 iss 记录瞬间 近期操作Jenkins调用比较多,当然Jenkins本身也提供了jenkins-cli.jar的使用方法,可以直接通过命令行进行调用, 但是,由于不想引入太多的jar包,导致直接使用Jenkins api需求强烈 下面就把近期收集到的一些常见用法做一个简单总结,希望对初学者有所帮助。 调用Jenkins接口api的几个例子 标签:post another 写入 admin back 推荐 警告 使用方法 iss 原文地址:https://www.cnblogs.com/wozijisun/p/12324325.html9、直接调用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¶m2=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 ‘
文章标题:调用Jenkins接口api的几个例子
文章链接:http://soscw.com/index.php/essay/66325.html