Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Key Value Pair in Jenkins

This is my requirement to have parameters in Jenkins:

1. User selects 3 Values from Dropdown: DEV, QA, PROD
2. Upon selection I need to return single value as parameter as like this:
 If DEV selected, return "Development http://dev.com 1"
 If QA selected, return "QA http://qa.com 2"
 If PROD selected, return "Production http://prod.com 3"
3. Once the value is returned in a variable, I will use that variable value in next step of 'Windows batch command'.

Where and How can define Key/Values. I tried to use Extended Choice Parameter plugin, but not sure how to do this.

like image 338
stoniel2 Avatar asked Apr 18 '16 02:04

stoniel2


People also ask

What is a key-value pair and how are they used?

A key-value pair (KVP) is a set of two linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data that is identified or a pointer to the location of that data. Key-value pairs are frequently used in lookup tables, hash tables and configuration files.

How do I assign a choice parameter in Jenkins?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

What is a key-value pair example?

A key-value pair consists of two related data elements: A key, which is a constant that defines the data set (e.g., gender, color, price), and a value, which is a variable that belongs to the set (e.g., male/female, green, 100). Fully formed, a key-value pair could look like these: gender = male. color = green.

How do you create a drop down list in Jenkins?

To use the DropDown ViewsTabBar go to Manage Jenkins > Configure System, select it in the Views Tab Bar drop down box and save the configuration.


3 Answers

I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin, it's not as complicated as the other answer here, and it actually buried in the comments on the plugin page itself.

To get a key/value pair select dropdown list parameter in Jenkins (i.e. show a human readable value, but pass a different key to the build. You simply need to use a map rather than a list when writing your groovy script. The map key is what the parameter will be set to if the user selects this option. The map value is what will be actually displayed to the user in the dropdown list.

For example the script: return ['Key1':'Display 1', 'Key2':'Display 2', 'Key3':'Display 3'] will display a dropdown containing: Display1, Display2 and Display3 to the user. However the build parameter will actually be set to Key1, Key2 or Key3 depending on what is selected.

For this specific question, here are the steps.

  1. Ensure you have the Active Choices Plugin installed.
  2. Open the configuration of your Jenkins job, select This project is parameterised.
  3. Click Add Parameter and select Active Choices Parameter.
  4. Name your parameter and click the Groovy Script check box.
  5. In Groovy Script enter content: return ['Development http://dev.com 1':'DEV', 'QA http://qa.com 2':'QA', 'Production http://prod.com 3':'PROD'] For this example the user will see a dropdown with 3 options: DEV, QA, and PROD. The value passed for this parameter will be Development http://dev.com 1 etc. Now having a parameter with space and URLs may cause issue depending on how you use it later on in the build, but the concept is really what I'm trying to illustrate.

Active Choices Parameter configuration

like image 148
ptha Avatar answered Sep 28 '22 00:09

ptha


This can be achieved using the Active choice plugin, Find below the link and the image for your reference

Plugin reference: https://wiki.jenkins-ci.org/display/JENKINS/Active+Choices+Plugin

enter image description here

Another method without any plugins

Using shell script this can be achieved

Add a build step as shell script and add the below script that will return your values. Lets say the dropdown paramater name is "env"

if [ $env == "DEV" ]
then
   url = "Development http://dev.com 1"
elif [ $env == "QA" ]
then
   url = "QA http://qa.com 2"
elif [ $env == "PROD" ]
then
   url = "Production http://prod.com 3"
fi

The $url variable will be having the expected value that can be used in your next build steps

Shell Script Reference: http://www.tutorialspoint.com/unix/if-elif-statement.htm

like image 40
Ganesan Srinivasan Avatar answered Sep 28 '22 01:09

Ganesan Srinivasan


You can do the mapping in a groovy script. If you have a parameter named InputParam, you can map it to a new parameter called OutParam in a System Groovy Script like so:

import hudson.model.*
def parameterMap=[:]
parameterMap.put('DEV','Development http://dev.com 1')
parameterMap.put('QA','QA http://qa.com 2')
parameterMap.put('PROD','Production http://prod.com 3')

def buildMap = build.getBuildVariables() 
def inputValue=buildMap['InputParam']
buildMap['OutParam']=parameterMap[inputValue]

setBuildParameters(buildMap)

def setBuildParameters(map) {
    def npl = new ArrayList<StringParameterValue>()
    for (e in map) {
        npl.add(new StringParameterValue(e.key.toString(), e.value.toString()))
    }
    def newPa = null
    def oldPa = build.getAction(ParametersAction.class)
    if (oldPa != null) {
        build.actions.remove(oldPa)
        newPa = oldPa.createUpdated(npl)
    } else {
        newPa = new ParametersAction(npl)
    }
    build.actions.add(newPa)
}

Choose Execute system Groovy script as the first build action. You can then access the output param as an environmental variable in the windows shell, eg. ECHO %OUTPARAM%

like image 24
IanAWP Avatar answered Sep 28 '22 00:09

IanAWP