Jenkins Active Choices Plug-in with alias names - jenkins

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

Related

Parameterized Jenkins job with dependent parameter

I am trying to create a Jenkins job that has dependent parameters.
Firstly I want to be able to choose a main parameter:
And then secondly to be able to choose from a set of options that are dependent parameters of the main parameter.
If I select a different main parameter:
I then want to have a different set of options as the dependencies to the second main parameter.
Please, can you help me with how I can achieve this?
I would suggest the Active Choices plugin (also known as "uno-choice"). (This question has references to both, though they're not the accepted answer.)
For your specific use case, you'll want to add two parameters to your job:
Active Choices Parameter
Name: MainOption
Script: Groovy Script
return ['A','B']
Active Choices Reactive Parameter
Name: DependentOption
Script: Groovy Script
def choices
switch(MainOption){
case 'A':
choices = ['Blue','Green','Yellow']
break
case 'B':
choices = ['Black','White','Grey']
break
default:
choices = ['N/A']
break
}
return choices
Fallback Script: Groovy Script
return ['Option error']
Referenced parameters:
MainOption
The "Referenced parameters" setting is the key - when that value is changed, the plugin will re-evaluate the Groovy script, giving you the dependent parameter effect.
For all of you who stumble upon the same kind of problem (like I did). There is a fairly new Jenkins plugin available that does exactly what is desired here: display a dependent set of drop-downs, updating dependent boxes when a main box changes selection.
For your job you just need to follow the following steps:
Install "Multiselect Parameter Plugin"
The "multiselect parameter plugin" can be installed from Jenkins plugin management, the documentation is available on its Jenkins Plugin page.
Add new parameter
Use "Multiselect Parameter" type
Set name to a sensible value
give a short description
configure like shown below:
H,Main option,Dependent option
V,SELECTED_MAIN,SELECTED_DEPENDENT
C,A,Blue
C,A,Green
C,A,Yellow
C,B,Black
C,B,White
C,B,Grey
Use selected values
When you run "build with parameters" in your job, the following boxes are displayed for selection:
In your build script you can simply use the configured environment variables SELECTED_MAIN and SELECTED_DEPENDENT, which contain the selected values from both select boxes.

Jenkins - Active Choice Reactive Reference

I am using Active Choices Plugin to dinamically load parameters. I want to read last line of a $workspace file as a parameter.
In this example, when selecting "pedro" username it should display linea 1" because is the last line of the document that is in "/var/lib/jenkins/workspace/Aa.test1.txt"
This is how the job is configured:
And this is when trying to build the job with parameters:
If I execute it in Jenkins Script console it displays the output correctly...
Thank you.
Instead of "choice parameter" you should use "Active choice parameter" and use the name as a referenced parameter in "Active choice Reactive parameter" and finally you can use the name given for "Active choice reactive parameter" as a referenced parameter in "Active choice Reactive Reference parameter".
I think you got the idea wrong. You're using Reactive parameter for "username" but forgetting to specify which parameter to react to. See empty "Referenced parameters" text field in "username" parameter definition.
The logic is pretty simple. When your "referenced parameter" has changed it makes Reactive choice to rebuild dependent parameter (username) and show the value according to the logic implemented as Groovy script.

Jenkins - Textarea parameter plugin set default value from property file?

I'm trying to find jenkins parameter plugins to do;
Editible textbox (multiline)
Set default text from property file (something like groovy script)
Can you please suggest any plugins?
I tried Active chioce parameter plugin, Extended choice parameter plugin. But those are only provide choice option not textarea.
And Dynamic parameter plugin also seems not support multi-line.
Exists a way in jenkis jobs configuration to pass text parameter to execute a job. Adding Text Parameter, you can define a simple text multiline parameter, where users can enter a text value, which you can use during a build, either as an environment variable, or through variable substitution in some other parts of the configuration.
To define parameters for your job select "This build is parameterized", then using the drop-down button to add as many text parameters or other type as you need.
more info look at
Parameterized Build

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

How to use Extended Choice Parameter plugin for check box

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

Resources