How to use Extended Choice Parameter plugin for check box - jenkins

Currently I am using Choice parameter to input the hostname in a drop-down like below.
server1
server2
server3
I am include the selected value in property.
Server=%Hostname%
Also I am invoking the same in post build actions-->Email Notification-->Subject-->Login success for $Hostname.
But my requirement is instead of a drop-down I need a check box option for server1, server2 & server3 so that I can select multiple servers at one time and build the job.
Also I need to include the hostname in property and in email subject. But the Email subject should not contain the actual hostname (server), instead it has to be a different name.
Let's say,
server1 = DEV
server2 = QA
The property should take "server1" value and the email suject should take "DEV".
I am trying to use Extended Choice Parameter plugin, but I am stuck, so any help would be really appreciated!

Extended Choice Parameter plugin is the way to go for such requirement. You need to select Extended Choice Parameter from the drop-down list as shown below.
Once you select that option, you will see another drop-down with the name Parameter Type as shown in the snapshot below. Select Multi Select from that drop-down. Enter the server names in Value box. Comma (,) is the delimiter.
Now if you run the command echo "Server: $Hostname" on *nix systems after selecting one (or more than one) server, you will get all the selected server(s) in the command output.
Now to address your query of displaying names such as Dev/QA instead of actual server names, you will obviously have to do some amount of scripting. Since you are taking the server names in a string, you will first have to split the string using comma (,) as delimiter to fetch individual values (servers). And then you will have do check each server and assign values to it. A pseudo code such as:
if ( str eq server1 ) {
host = QA;
} elsif ( str eq server 2 ) {
host = Dev;
} and so on...
I wrote a similar script in Perl few back back. You can use your language of choice (bash, batch etc.)
To pass these variables in subject line of your mail etc., you will have to use EnvInject Plugin as suggested by Slav. You can write the value (QA/Dev) in some file while running your if...else code so that it could be later used by the EnvInject plugin. Just in case, if you want an alternative way, you can simply use the system's mail command depending on the flavor you have.

With the Extended Choice Parameter, you can change between dropdown/multiselect/checkbox/radio-button by selecting the value called "Parameter Type". If you don't see that, you probably have a very old version of that plugin.
As for the second part of your question: you are going to have to do that mapping in your scripts, inject it using EnvInject plugin, and then use the injected value in your email

Related

Dynamic parameter selection on jenkins

I have a build/deploy job on jenkins, and user can select multiple items to be deployed with multi select parameter. I retrieve those values dynamically from db table with a groovy script.
Problem is that some of the variables should be linked. Meaning; when user selects item X, item A and item B also should be selected.
Is there a way of triggering a selection event on jenkins multi select? or should I use something else?
Thanks.
After spending some time I found a way to do this using Active Choices Plugin
1- I kept my initial extended choice parameter lets name it COMPONENTLIST
2- Then I created another paramateter as Active Choices Reactive Reference Parameter lets name it COMPONENT_IDS
2a- I added COMPONENTLIST as referenced parameter in COMPONENT_IDS. And set the Choice Type as Formatted HTML and also selected Omit value field
2b- I used following groovy script to collect initial selection and make modifications on it and returned as in step 2c
def output = COMPONENTLIST.split(',').collect{it as int}
2c- tricky part here! It is different how you pass the paramater to build stage. Following line helped me to pass COMPONENT_IDS to build.
output = output.join(",")
return "<b>${output}</b><input type=\"hidden\" name=\"value\" value=\"${output}\" />"

Jenkins Extended Choice Parameter - using the values

I'm new to Jenkins so this is probably an easy one. I have the Extended Choice Parameter plugin installed. I'm using the Multi Select parameter type to pick from a list of servers (SERVER1,SERVER2,SERVER3) I've set Source for Value, Default Value, and Value Description.
I save it, and it looks great. I can pick any or all servers for the build. Now for the big question.. how do I use these values in the build? Basically I have a step in the build that can take in a comma separated list that is called by a shell command:
d:\python\deploy.py?serverlist=$blah
What do I put in for $blah to use that list of servers?
Just to be clear, if I'm on command line I would do the following:
d:\python\deploy.py?serverlist=SERVER1,SERVER2,SERVER3
I'm sure it's something simple but I just cannot find it in the docs or an example.
We could get the servers list like this
d:\python\deploy.py?serverlist=$SERVERLIST
or this on Windows
d:\python\deploy.py?serverlist=%SERVERLIST%
To see the list of environment variables which we could you, try this URL (change localhost by your Jenkins URL, TEST by the job name, 10 by the build number)
https://localhost:8080/job/TEST/10/injectedEnvVars/
UPDATE to #sniperd's edition:
This URL will shows us the parameters list in the Job:
http://localhost:8080/job/TEST/59/parameters/

Jenkins Active Choices Plug-in with alias names

I'm using jenkins to build an ant project.
The target names in the build.xml are too verbose.
How can I give the target a more user-friendly name?
For example:
I want to change server_remote_stop to "Stop Server", and server_remote_start to "Start Server" without modifying the target names in build.xml (because this build.xml is used by other programs).
Is this possible?
Yes, it is possible, but you will need to use a different Active Choices parameter type: Reactive Reference Parameter.
Here are the settings that I used:
One active choices parameter named PARAMETER1 with some random server names (AAA, BBB)
One active choices reactive reference parameter, that is watching PARAMETER1, has Choice type as "Formatted HTML", has the Advanced option "Omit value" checked, and the following script:
html ="""
Start Server $PARAMETER1
Stop Server $PARAMETER1
"""
This kind of Formatted HTML is rendered in the UI as HTML, and you can mimic HTML components used in Jenkins, like a select box (you can use a radio, checkbox, etc). What is important is that you return some element whose name is "value" and omit the value field that is automatically created by the plugin (thus the advanced option Omit Value).
Since it is a reactive parameter, you can reference other parameters in your Groovy script as well :-)
Hope that helps,
Bruno

How to select a node name as a parameter in jenkins using a selection list of some sort

I need to know if there is a plugin of some sort that you can select a node from a jenkins job and use that node name as a parameter to be passed to a Windows batch command
I have played with the Configuration Matrix using an Elastic-Axis or Slaves (Screenshot below where you can tick the names) plugins
But these all go and execute the Windows batch command on that selected node.
I don't want to execute it on that server but rather on the main node and only pass the value of the slave/label to the windows batch command.
I were able to do it as described here but that involves 2 jobs and a groovy scripts to interrogate the slaves/nodes config. Write it to a properties file and pass the properties file to the next job.
Jenkins: How to get node name from label to use as a parameter
I need to do about 30 jobs of these and hence would like to try to do all in one job - if I used my solution in the link above, 30 jobs would double in 60 jobs and maintenance would be kind of a nightmare.
I also would not like to have a string parameter and hard code the name of the slave/node as that will not ensure the use of only the available slaves/nodes but any server name can be entered and that would can be a problem where someone can mistype a server name for example pointing to a Production server instead of a test server.
https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin
After installing this plugin you will have an option to add a Node parameter to Jenkins job. It will have a list of all slaves available on current master.
You can use the active choice parameter plugin
https://plugins.jenkins.io/uno-choice/
with a little groovy script to list node names in a parameter selection box.

Jenkins Parameterized build to use key/value pairs

I have a Jenkins parameterized build. I tick the "this build is parameterized" and I set a "Choice" environment name to be "ENVIRONMENT" and then as choices I define human readable names such as "Test env1", "Test env2", etc. However I want these keys to actually contain different values, for example "Test env1" key would container a file path as its value. How can this be done?
I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin, the answer was actually buried in the comments on the plugin page itself.
You simply need to use a map rather than a list when writing your groovy script to define the dropdown options. 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 (i.e something human readable) in the dropdown list.
Here are the steps.
Ensure you have the Active Choices Plugin installed.
Open the configuration of your Jenkins job, select This project is parameterised.
Click Add Parameter and select Active Choices Parameter.
Name your parameter ENVIRONMENT and click the Groovy Script check box.
In Groovy Script enter content: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2'] For this example the user will see a dropdown with 2 options: Test env1 and Test env2. The keys: env1 file path value and env2 file path value are what the Jenkins build parameter will be set to if the option is selected. Modify these as necessary.
In this case ENVIRONMENT is the key and "Test env1", "Test env2", etc. are the possible values. Choice parameter is to restrict the possible inputs.
Based on the value of %ENVIRONMENT% you can execute multiple pathways in your batch scripts or whichever scripts you are executing

Resources