如何通过REST API提交Jenkins作业? [英] How to submit Jenkins job via REST API?

查看:165
本文介绍了如何通过REST API提交Jenkins作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的'执行系统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(

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



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

您需要使用JSON。请参阅提交作业



以下方法适用于我:

 <按钮
类型='按钮'
onclick ='javascript:
var another_job = function(){
new Ajax.Request(http:// localhost:8081 / job / JReport2 / build,{
method: post,
参数:{json:Object.toJSON({参数:[{name:foo,value:fobar}]})}
});
};
another_job()'>
开始工作
< /按钮>

有点奇怪,当构建列表中的构建旁边出现的按钮是推送,但不适用于构建描述本身出现的按钮。


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:

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

I'm not sure if the problem is due to bad quoting or the way I'm sending through the build parameters.

解决方案

You need to use JSON. See Submitting Jobs.

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.

这篇关于如何通过REST API提交Jenkins作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆