Groovy as shared library to add an additional parameter to a jenkins job

I have tried this solution but duplicating the parameters at every run.
I want to be able to create booleanParam and gitParameter parameters for different repos in jenkinsfile stages

@NonCPS
def addParam (repo_name, git_branch) {
// Get existing ParameterDefinitions
existing = currentBuild.rawBuild.parent.properties
.findAll { it.value instanceof hudson.model.ParametersDefinitionProperty }
.collectMany { it.value.parameterDefinitions }
// Create new params and merge them with existing ones
jobParams = [
booleanParam(name: "$**{repo_name}", defaultValue: '', description: "Select to deploy ${repo_name}"),
gitParameter(branch: '',
branchFilter: '.*',
defaultValue: 'develop',
description: "${repo_name}** git parameter build",
listSize: '0',
name: "$**{git_branch}",
quickFilterEnabled: true,
selectedValue: 'NONE',
sortMode: 'DESCENDING_SMART',
tagFilter: '*',
type: 'PT_BRANCH_TAG',
useRepository: "${repo_name}**")
] + existing

// Create properties
properties([
parameters(jobParams)
])
}



Can anyone help me?


You are adding your new parameters to the existing list of parameters. It’ll always be an ever growing list.

This will affect the next job, not this job.

I’ve not used it, but Active Choices is a common way to have dynamic properties.

1 Like