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 ?
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/)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With