jenkinsdeploymentjenkins-pipelinejenkins-cliautomated-deploy

Is there a way to use external variable as choice parameters in jenkins?


I want to fetch data from my sql Table and use them as a choice parameters in Jenkins

+----+
| ID |
+----+
| 11 |
| 23 |
| 45 |
| 72 |
|  5 |
| 16 |
| 71 |
| 18 |
+----+

I want to use these IDs as a choice parameter in Jenkins in order to build using one of these ID i.e. build with parameters using these ID numbers as Parameter

I simply want a drop down with these ID numbers as the choices before I build and the selected choice can be used as parameter to the script in the build step.

What would be the way of achieving this? Please help


Solution

  • You can achieve this using Extended Choice Parameters in combination with a properties file.

    1. Create a properties file with content key=value1,value2,value3,..., e.g., key=11,23,45,72,5,16,71,18.
    2. Place this file in your Jenkins master filesystem, e.g., /var/lib/jenkins/userContent/build.properties. You need to write an automation script/create another job to update this file at regular intervals.
    3. In your job configuration, select This project is parameterized > Extended Choice Parameter.
    4. Enter a name for your parameter, e.g., TABLE_VALUE.
    5. Select Basic Parameter Type > Single Select.
    6. Enter the delimiter that you used to separate values in your properties file. In this case, the comma ,.
    7. Select Property File and enter the path to the properties file in Jenkins master - /var/lib/jenkins/userContent/build.properties.
    8. Enter key as the Property Key.
    9. Save the job configuration.
    10. Access the value of the selected parameter using the variable TABLE_VALUE in your pipeline.

    Pipeline Code

    You can achieve the same in your pipeline by placing this block at the top:

    properties([
        parameters([
            extendedChoice(
                name: 'TABLE_VALUE', 
                description: '', 
                type: 'PT_SINGLE_SELECT',
                multiSelectDelimiter: ',', 
                propertyFile: '/var/lib/jenkins/userContent/build.properties', 
                propertyKey: 'key',
                quoteValue: false
            )
        ])
    ])
    

    See the source code for all possible parameters and PT constants for all possible values of type.

    Job Configuration

    enter image description here

    Build Parameters

    enter image description here