javascript 如何通过 REST API 提交 Jenkins 作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10865538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to submit Jenkins job via REST API?
提问by Noel Yap
The following 'Execute system Groovy script' Build Task updates the build's description to add a button that will submit another Jenkins job which is parameterized:
以下“执行系统 Groovy 脚本”构建任务更新了构建的描述以添加一个按钮,该按钮将提交另一个参数化的 Jenkins 作业:
import hudson.model.Cause
import hudson.model.Job
import jenkins.model.Jenkins
final JOB_NAME = 'my-job'
final jenkins = Jenkins.instance
final job = jenkins.getItemByFullName(JOB_NAME, Job.class)
final currentBuild = Thread.currentThread().executable
final buildNumber = currentBuild.getNumber()
job.builds
.findAll { build -> build.number == buildNumber }
.each { build ->
build.setDescription("""
<button
type='button'
onclick='javascript:
var another_job = function() {
parameters = {json: {parameter: [{name: "P4_CHANGELIST", value: "0"}]}};
new Ajax.Request("http://builds/job/another-job/build", {
method: "post",
parameters: Object.toJSON(parameters)
});
};
another_job()'>Continue</button>""")
}
But upon clicking the Continue button, the request returns a 400 Bad Request. It looks like it's because the build parameters aren't being passed through correctly (if I remove the build parameters from another-job and don't send through parameters, things work fine).
但是在单击“继续”按钮时,请求会返回 400 错误请求。看起来是因为构建参数没有被正确传递(如果我从另一个作业中删除构建参数并且不通过参数发送,一切正常)。
I'm not sure if the problem is due to bad quoting or the way I'm sending through the build parameters.
我不确定问题是由于引用错误还是我通过构建参数发送的方式。
回答by malenkiy_scot
You need to use JSON. See Submitting Jobs.
您需要使用 JSON。请参阅提交作业。
The following worked for me:
以下对我有用:
<button
type='button'
onclick='javascript:
var another_job = function() {
new Ajax.Request("http://localhost:8081/job/JReport2/build", {
method: "post",
parameters: {json: Object.toJSON({parameter: [{name: "foo", value: "fobar"}]})}
});
};
another_job()'>
Start Job
</button>
What's a bit strange that is works when the button that appears next to the build in the build list is pushed, but does not work with the button that appears on the build description itself.
有点奇怪的是,当按下构建列表中构建旁边的按钮时有效,但不适用于构建描述本身上的按钮。