Choice parameter in Jenkinsfile dynamic evaluation

I am trying to add a dynamic selection parameter to a Jenkins file with declarative syntax using something like this:

def myChoices = listBranchesFromGithub(MY_REPO)

pipeline {
    parameters {
        choice(name: 'mychoice', choices: myChoices)
    }
}

but listBranchesFromGithub(MY_REPO)is evaluated once (when the Jenkins file is being processed), and not every time I run the task.

Is there a way to fill in choicesevery time a task starts?

+4
source share
1 answer

You have two options.

In your jenkins file, call property properties again after processing.

#!groovy
@Library('shared') _

properties()

// Do pipeline

properties()

def properties() {
    // Set Jenkins job properties
    properties([
        buildDiscarder(logRotator(numToKeepStr: '20')),
        parameters([//params])
    ])
}

Pro, just? Cons: It works only if the steps are successful and do not exit earlier.

: ( , , , ). : script

/**
 * Change param value during build
 *
 * @param paramName new or existing param name
 * @param paramValue param value
 * @return nothing
 */
def setParam(String paramName, String paramValue) {
    List<ParameterValue> newParams = new ArrayList<>();
    newParams.add(new StringParameterValue(paramName, paramValue))
    $build().addOrReplaceAction($build().getAction(ParametersAction.class).createUpdated(newParams))
}

/**
 * Add a new option to choice parameter for the current job
 *
 * @param paramName parameter name
 * @param optionValue option value
 * @return nothing
 */
def addChoice(String paramName, String optionValue) {
    addChoice($build().getParent(), paramName, optionValue)
}

/**
 * Add a new option to choice parameter to the given job
 * @param job job object
 * @param paramName parameter name
 * @param optionValue option value
 * @return
 */
def addChoice(Job job, String paramName, String optionValue) {
    ParametersDefinitionProperty paramsJobProperty = job.getProperty(ParametersDefinitionProperty.class);
    ChoiceParameterDefinition oldChoiceParam = (ChoiceParameterDefinition)paramsJobProperty.getParameterDefinition(paramName);
    List<ParameterDefinition> oldJobParams = paramsJobProperty.getParameterDefinitions();
    List<ParameterDefinition> newJobParams = new ArrayList<>();

    for (ParameterDefinition p: oldJobParams) {
        if (!p.getName().equals(paramName)) {
            newJobParams.add(0,p);
        }
    }

    List<String> choices = new ArrayList(oldChoiceParam.getChoices());

    choices.add(optionValue);
    ChoiceParameterDefinition newChoiceParam = new ChoiceParameterDefinition(paramName, choices, oldChoiceParam.getDefaultParameterValue().getValue(), oldChoiceParam.getDescription());
    newJobParams.add(newChoiceParam);

    ParametersDefinitionProperty newParamsJobProperty = new ParametersDefinitionProperty(newJobParams);
    job.removeProperty(paramsJobProperty);
    job.addProperty(newParamsJobProperty);
}

/vars . /vars/jobParams.groovy. Jenkinsfile , :

jobParams.addChoice("ARTIFACT_NAME", "da-name")
0

Source: https://habr.com/ru/post/1682836/


All Articles