Commander Asdasd
Commander Asdasd

Reputation: 41

How can I pass custom HTML+js code to Jenkins Input step page?

I'm trying to achieve flexible UI in Jenkins "Input step" from pipeline library, currently I'm using Extended choice parameters plugin (https://wiki.jenkins.io/display/JENKINS/Extended+Choice+Parameter+plugin), that built on top of Json Editor library (https://github.com/json-editor/json-editor) and used to provide various HTML input elements (created from JSON schema) and generating JSON output.

Does "Input step" can accept some html template code to fill it with data, collected during build and print it on "Input step" page with some custom HTML elements and js binding, beyond input forms provided by "Extended choice parameters"?

Now I'm generating dynamic drop-down list via groovy shared library script, but can't make it print on "Input step page" non-input elements.

Example of what I need on input step

Upvotes: 1

Views: 5089

Answers (1)

Commander Asdasd
Commander Asdasd

Reputation: 41

Everything was quite simple, ExtendedChoiceParameterDefinition have parameter "String javascript", that can take any js string which can not only return JSON string for Json-editor, but also can modify input page itself.

Example pipeline:

stage('UserInput'){
        steps {
            script {
                    //...groovy script is omitted, it need only return json in valid form
                    def jsString = '''
                    var bodyElement = document.createElement('div');
                    bodyElement.innerHTML = '<h1 id="title">Some Title</h1><span style="display:inline-block; width=100px;">Some arbitrary text</span>';
                    document.getElementsByTagName('body')[0].appendChild(bodyElement);'''
                    def jsonParams = new ExtendedChoiceParameterDefinition(
                        'Cookbooks', //String name,
                        'PT_JSON', //String type,
                        null, //String value,
                        null, //String projectName,
                        null, //String propertyFile,
                        jsonGroovyScript, //String groovyScript (that returns JSON for generating Json-Input forms),
                        null, //String groovyScriptFile,
                        "jsonText=$jsonText", //String bindings,
                        '', //String groovyClasspath,
                        null, //String propertyKey,
                        null, //String defaultValue,
                        null, //String defaultPropertyFile,
                        null, //String defaultGroovyScript,
                        null, //String defaultGroovyScriptFile,
                        null, //String defaultBindings,
                        null, //String defaultGroovyClasspath,
                        null, //String defaultPropertyKey,
                        null, //String descriptionPropertyValue,
                        null, //String descriptionPropertyFile,
                        null, //String descriptionGroovyScript,
                        null, //String descriptionGroovyScriptFile,
                        null, //String descriptionBindings,
                        null, //String descriptionGroovyClasspath,
                        null, //String descriptionPropertyKey,
                        null, //String javascriptFile,
                        jsString, //String javascript (js code that modify input page itself),
                        false, //boolean saveJSONParameterToFile,
                        false, //boolean quoteValue,
                        10, //int visibleItemCount,
                        '', //String description,
                        ',' //String multiSelectDelimiter
                    ))
                    parameterList << jsonParams
                    def form = input(
                        id: 'form', message: 'input parameters', parameters: parameterList
                    ) // generating input page step and store it in "form" var
                    env.FORM = form
                }
              }
            }

Upvotes: 2

Related Questions