How to set up dynamic string parameter in Jenkinsfile?

Jenkins setup: I don’t have sufficient permission to get the Jenkins setup. But I could see my Jenkins version is 2.387.1 from right bottom of the Jenkins web console.

I have a two questions related to parameter setup in Jenkinsfile with Groovy script.

  1. Is there any way to specify the default value in a drop-down list parameter instead of the first choice in the drop-down list as the default value?

  2. I would like to set up the defaultValue for a string type parameter. However, this defaultValue is not a static constant, but a dynamic variable, which comes from another parameter value selected by the current user who is building this pipeline with parameters. For example, in the below code, I would like the “SubnetGroupName” defaultValue to come from the “DeploymentAccount” parameter value with string concatenation. However, as per my testing result, I could see the “SubnetGroupName” defaultValue comes from the “DeploymentAccount” of last build instead of the current selection of this “to-be” build.

choice(
description: “The AWS account where the ElastiCache Redis will be deployed.”,
name: ‘DeploymentAccount’,
choices: [‘RSACN’,‘RSBCN’,‘RSICN’,‘RSIDCN’,‘RCCCN’]
),
string(
name: ‘SubnetGroupName’,
description: “Please specify the ElastiCache Subnet group name. By default it is "DeploymentAccount-vpc-elasticachesubnetgrp". For example, "rsbcn-vpc-elasticachesubnetgrp".”,
defaultValue: “${params.DeploymentAccount.toLowerCase()}-vpc-elasticachesubnetgrp”
)

You will need the Active Choices plugin to achieve this

Hi Markus, You meant 1st question or 2nd question? Or both?

Is there anybody able to help me on my 2nd question?

active choice is mainly for the second question. but it can probably also be used for your first problem.

your construct

choice(
description: “The AWS account where the ElastiCache Redis will be deployed.”,
name: ‘DeploymentAccount’,
choices: [‘RSACN’,‘RSBCN’,‘RSICN’,‘RSIDCN’,‘RCCCN’]
),
string(
name: ‘SubnetGroupName’,
description: “Please specify the ElastiCache Subnet group name. By default it is "DeploymentAccount-vpc-elasticachesubnetgrp". For example, "rsbcn-vpc-elasticachesubnetgrp".”,
defaultValue: “${params.DeploymentAccount.toLowerCase()}-vpc-elasticachesubnetgrp”
)

will lead to the behaviour that you modify the parameter for the next run execution with a value from the current execution.