0

i used the code from the answer of ...

How to submit Jenkins job via REST API?

... to insert a button to the job build descriptions.

Jenkins version : 1.645

The button is visible, but the onClick action is simply not created.

Do i have to install additional jenkins plugins to allow javascript or ajax actions? Thanks for your support!

job.builds
.findAll { build -> build.number == buildNumber }
.each { build ->
    build.setDescription("""
        <button
            type='button'
            onclick='javascript:
                console.log("OnClick Action triggered!");
                var another_job = function() {
                      new Ajax.Request("http://jenkins.alalala/hudson/job/AA_dummy_job_AUTO/build", {
                      method: "post",
                      parameters: {json: Object.toJSON({parameter: [{name: "SOME_JOB_PARA", value: "MY_VALUE"}]})}
                  });
                };

                another_job();
                location.reload();'>Do Something!</button>""")
}

Example of the button and the ZWS-inserted code part

Community
  • 1
  • 1
simon
  • 113
  • 2
  • 9

2 Answers2

0

Generally it seems like a bad idea to inject buttons into the UI, rather than using built-in features like downstream jobs, or the Promoted Builds Plugin, or the Parameterized Trigger plugin.

But you haven't given many details — do you see your log text being output in the JS console? Are there any other errors?

For what it's worth, it should work; Ajax.Request should be available on the job page by default, and pasting that JavaScript into the JS console in Chrome works for me — a new build is started with the parameter.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Thanks for the test! Which jenkins version did you use? i have seen no further errors in the browser log/console. – simon Feb 16 '16 at 14:07
  • I think I used 1.642.1. Are you sure the job URL is correct? (BTW, you should preferably be using the stable [`/buildWithParameter` API](https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build#ParameterizedBuild-Launchingabuildwithparameters) for starting a build with parameters) – Christopher Orr Feb 16 '16 at 14:12
  • The problem is, as you see in the image, the button is created but without the onClick="javascript:..." part. I can not test the JS code, because there is no generated js code 'under' the button. – simon Feb 16 '16 at 15:39
  • 1
    @simon Ah, sorry, I missed that! The `onClick` attribute is being stripped out in order to protect against attempted XSS exploits by users. This is part of why it's not a great idea to insert buttons into build descriptions! :) But I have another answer which explains how this behaviour (HTML stripping) is configurable in Jenkins: http://stackoverflow.com/a/18867643/234938 – Christopher Orr Feb 16 '16 at 16:00
  • thanks! I will use the 'anything goes' plugin and let you know if everything works fine... – simon Feb 17 '16 at 08:55
  • It seems the 'anything goes' plugin is not working with jenkins versions >1.5x – simon Feb 17 '16 at 14:43
0

Thanks to Christopher,

since the 'anything goes'-Formatter plugin is not available for jenkins versions >1.5 i use an alternative.

Promoted Builds Plugin

Fits my needs in most cases.

simon
  • 113
  • 2
  • 9