2

以下“执行系统 ​​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>""")
    }

但是在单击 Continue 按钮时,请求会返回 400 Bad Request。看起来这是因为构建参数没有正确传递(如果我从另一个作业中删除构建参数并且不发送参数,那么一切正常)。

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

4

1 回答 1

7

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

以下对我有用:

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

于 2012-06-02T20:19:11.527 回答