You have two options.
In your jenkinsfile, call 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's, simple? Cons: Only works if the steps succeed and do not exit early.
Alternative: Dynamically adjust during the build (We put these functions in a shared library and call them whenever we need to update a param, such as an artifact list). Note: may required script security approvals
/**
* 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);
}
In the above situations, these methods live under /vars within the shared library. /vars/jobParams.groovy. So from Jenkinsfile or another shared library, you can invoke these using the following syntax:
jobParams.addChoice("ARTIFACT_NAME", "da-name")