2

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.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144

1 Answers1

7

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.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87
  • Isn't the above using JSON in the parameters field? – Noel Yap Jun 02 '12 at 20:33
  • Sorry, I don't understand; I'm new to all these technologies. What does the json= parameter in the curl command do? What would be the equivalent in PrototypeJS? – Noel Yap Jun 03 '12 at 14:46
  • Should the parameters element look like "parameters: {json: {parameter: ..."? – Noel Yap Jun 03 '12 at 14:53
  • It's like posting a form that has a field named 'json'. Not sure how to do it exactly with PrototypeJS. If you do not manage it yourself, I'll play with your code tomorrow. – malenkiy_scot Jun 03 '12 at 14:55
  • I tried setting the json field but still no luck. It looks like it's headed in the right direction so something else is probably wrong with what I have above (which I've edited to include the 'json' change). – Noel Yap Jun 03 '12 at 20:59
  • SUCCESS! The last problem was one of quoting. I'll post the working solution. Thanks for all your help @malenkiy_scot. – Noel Yap Jun 03 '12 at 21:26