2012-06-02 40 views
0

下面的“执行系统Groovy脚本”建设任务更新生成的描述添加一个按钮,将提交其他詹金斯工作,这是参数:如何通过REST API提交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>""") 
    } 

但在点击Continue按钮,请求返回400错误请求。它看起来像是因为构建参数没有正确传递(如果我从其他作业中删除构建参数并且不通过参数发送,事情就会正常工作)。

我不确定问题是由于引用不良或我通过构建参数发送的方式。

回答

5

您需要使用JSON。请参阅Submitting Jobs

以下为我工作:

<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> 

什么是有点怪是工作时旁边会出现在生成列表生成按钮被按下,但不会出现在按钮上工作构建描述本身。

+0

是不是上面在参数字段中使用JSON? –

+0

我看不到单词'json':) –

+0

对不起,我不明白;我是所有这些技术的新手。 curl命令中的json =参数有什么作用? PrototypeJS的等价物是什么? –