Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Jenkins Build button based on Parameters

Tags:

jenkins

I have Jenkins jobs with parameters. I would like to disable the build button and enable only user enters valid value in string parameter. How can i do that ?

like image 475
user1550159 Avatar asked Sep 26 '22 19:09

user1550159


2 Answers

There is one more extension to the above shown method - Above method checks for BUILD button on every Jenkins page as Simple Theme Plugin will be used.

In order to make the change at Job level - we can use Active Choices (https://plugins.jenkins.io/uno-choice/)

  1. Go to configure section of the respective job
  2. Select This project is parameterized option in General Tab
  3. Add a new String Parameter named "Validating_Parameter"
  4. Add another parameter of type Active Choice Reactive Reference Parameter
  5. In Active Choice Reactive Reference Parameter - name can be empty and Choice Type should be of Formatted Hidden HTML
  6. Add below groovy script by selecting Groovy:
return '''
<script>
function custom() {
    if (!document.querySelector("td.setting-main>div>input[value='Validating_Parameter']")) {
        return;
    } else {
        var inputElement = "";
        if (document.querySelector("td.setting-main>div>input[value='Validating_Parameter']").parentNode) {
            var childNodes = document.querySelector("td.setting-main>div>input[value='Validating_Parameter']").parentNode.childNodes;
            if (Object.keys(childNodes).length) {
                for (var key in childNodes) {
                    if ((Object.prototype.toString.call(childNodes[key]) == "[object HTMLInputElement]") && (childNodes[key].name == "value")) {
                        inputElement = childNodes[key];
                    }
                }
            }
        }
    }


    /* init */

    if (inputElement) {

        var buildNumber = inputElement.value;

        setTimeout(function () {
            if (document.getElementsByName("parameters")[0].getElementsByTagName("button")[0]) {
                document.getElementsByName("parameters")[0].getElementsByTagName("button")[0].disabled = buildNumber.match(/^[a-zA-Z0-9\\s,\\!.-]{15,}$/) ? false : true;
            }
        }, 100);
    }

    /* Register keyup event */

    if (inputElement) {

        inputElement.onkeyup = function () {

            var buildNumber = inputElement.value;

            if (document.getElementsByName("parameters")[0].getElementsByTagName("button")[0]) {
                document.getElementsByName("parameters")[0].getElementsByTagName("button")[0].disabled = buildNumber.match(/^[a-zA-Z0-9\\s,\\!.-]{15,}$/) ? false : true;
            }
        }
    }

}

document.addEventListener('DOMContentLoaded', custom, false);
</script>
'''

Updated code so it doesnt fail on yui-gen3-button not found element. you can reference answer by @Mayur

Code referenced from above code by @Renjith

like image 112
Simeen Khan Avatar answered Dec 19 '22 22:12

Simeen Khan


You could use the validating string parameter plugin.

Or if you wanted to roll one yourself check out the documentation.

From the docs: "This is called "form validation", and Jenkins performs this on the server side." Meaning you wouldn't want to disable the build button if you use this method of validation.

like image 24
Jeremiah Avatar answered Dec 19 '22 23:12

Jeremiah