Search code examples
jenkins

How do I set up a pair/array of strings for a Jenkins build parameter?


I am currently trying to set up a Jenkins pipeline build that takes an array of string pairs as a build parameter.

The said parameters will be used to switch certain subdirectories of the working copy to specified branches, effectively granting Jenkins the ability to work with mixed branches: i.e. if [{"A1", "A2"}, {"B1", "B2"}, {"C1", "C2"}] is given as the parameter, Jenkins will switch the local subdirectory A1 to the URL "A2", local subdirectory B1 to the URL "B2", and so on.

Taking a list and switching branches for each pair can be simply done by a pipeline script; the problem is finding a way to actually pass an array of strings, or a pair of strings.

Initially I was trying to use the multi-line string parameter for the user to input the pairs one per each line, separated by a comma. However my PM insisted that I must not use this method for the input string being hard to read/type, so that's out of the question.

Searching for Jenkins plug-ins with the tag "Build Parameters" was fruitless as none of the plug-ins provided the functionality I am looking for.

Is there any way in Jenkins where you can set either:

  • a build parameter comprised of two user-input strings, such as a key-value pair,
  • or a build parameter that is a variable-length array of user-input strings, that you can add/delete to?

Or should I just create my own Jenkins plug-in to do that?


Solution

  • You can use Active Choice Reactive Reference Parameter for this purpose. Add an Active Choice Reactive Reference Parameter named PARAM in your job, and provide this groovy script:

    return """<table>
        <tr>
            <td>
                <input style="border:none" class="jenkins-form-label" name="value" Value="A1" readonly size="40"></input>
            </td>
            <td>
                <input class="jenkins-input" name="value" Value="A2" size="100"></input>
            </td>
        </tr>
        <tr>
            <td>
                <input style="border:none" class="jenkins-form-label" name="value" Value="B1" readonly size="40"></input>
            </td>
            <td>
                <input class="jenkins-input" name="value" Value="B2" size="100"></input>
            </td>
        </tr>
        <tr>
            <td>
                <input style="border:none" class="jenkins-form-label" name="value" Value="C1" readonly size="40"></input>
            </td>
            <td>
                <input class="jenkins-input" name="value" Value="C2" size="100"></input>
            </td>
        </tr>
    </table>"""
    

    Approve the script and select Formatted HTML in Choice Type dropdown.

    In pipeline script you can use this param as env.PARAM, the value you will get is A1,A2,B1,B2,C1,C2,.