How should I validate Jenkins build parameters on the front-end to enable/disable the build button?
Asked Answered
F

3

10

I have complex jobs with a lot of parameters, is there any way to validate those parameters using JavaScript form validation or with some other methods, and if the user's input isn't not correct, i'll show an error message and also disable the build button. I've tried installing the Validating String Parameter Plugin , it seems like that it's only displaying errors

Fortitude answered 18/7, 2016 at 17:16 Comment(3)
did you figured out how to validates fields and not start the build if something wrong?Bracey
No... Any idea without changing the core source of JenkinsFortitude
Don't forget a Jenkins jobs can be run from CLI or REST api, so hidden the button is not a real solution. Failling the job when input isn't correct is a better option.Irmine
P
3

You can combine:

That way:

  • the validating String parameter plugins prevents users to enter invalid values
  • the script (written in any language you want) will validate those parameters again, but this time called as a "Script Condition", to decide if a step should be executed or failed.

That ensures that, even if the job is called directly through API (without any string validation from the first plugin), the script (called by the second conditional step plugin) will block/fail the job if its execution (validating the strings/parameters by script) does end with a failure status.

Patristic answered 23/10, 2017 at 20:10 Comment(0)
S
1

There is a workaround to this problem, using Active Choices Plugin.

Basically you need to create an Active Choice Reactive Reference Parameter without name (blank name makes the error message look like description of the parameter).

Error messages shown for invalid parameters

Info messages shown for valid parameters

Your Active Choice Reactive Reference Parameter should:

  1. reference the parameter's name which you want to validate.
  2. have a groovy script to validate the parameter referenced.
  3. have set the choiceType to Formatted HTML.

Your groovy script can return the Parameter value something like this:

Valid Info Message:

return "<span style='color:green'>&#9989; Valid Email Id(s)</span>"

Error Message:

return """
    <style>.jenkins-button--primary { display: none; }</style>
    <span style='color:red'>&#10060; Invalid Parameter.</span>
"""

style component in this return statement vanishes the Build button.

For more information see this blog.

Semitrailer answered 22/2 at 12:1 Comment(0)
W
0

At the moment, the validating string parameter is the closest you'll get.

Whitefaced answered 22/10, 2017 at 4:48 Comment(2)
I like the 'at the moment'. It's still the case in 2021. Jenkins is a joke, the parameters limit you so much, it's crazy.Aitken
its still the same in 2023Chavey

© 2022 - 2024 — McMap. All rights reserved.