Jenkins Extended Choice Parameter - using the values - jenkins

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/

Related

Trigger nested Jenkins job from VSTS build

I've configured a service hook in VSTS to connect to Jenkins. I'm able to use the Jenkins extension to trigger a Jenkins job if it's not in a subfolder. E.g. http://myhost/job/Always%20succeed/
In that case, I can simply connect like this and run my job:
If my job is nested, however, I can't figure out how to connect. Here's an example: http://myhost/view/Production/job/Automation/job/Test/job/My%20Job
I've tried using just the name (e.g. "My Job"), the whole url, and a dot notation (Production.Automation.Test.My Job). How can I make this run and where can I find more documentation?
It's pretty nuanced and one could argue buggy. First off, I can reach the same job with two urls:
http://myhost/view/Production/job/Automation/job/Test/job/My%20Job
http://myhost/job/Automation/job/Test/job/My%20Job
Turns out the latter is the way to go.
I tried the following name, and it tried reaching the corresponding endpoint:
Automation/job/Test/job/My%20Job <- name used in VSTS "Job name" field
/job/Automation/job/job/Test/job/My%20Job/build <- url attempted, failed (404)
Note the double job/. Then I tried the following with better results:
Automation/Test/job/My%20Job <- name used
/job/Automation/job/Test/job/My%20Job/build <- url tried, success
It's a bit concerning that the pattern isn't consistent regarding the double "job/" part but whatever.

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.

How to display all keep forever build number using Jenkins REST API for a specific job?

I read the following post which does what I want for all jobs (and I made it works)
List all keep-forever builds in Jenkins?
here is what I have now :
http://jenkinsmediaprocessor:7880/api/xml?depth=2&xpath=/hudson/job/build[keepLog=%22true%22]/url&wrapper=forever
But I only want to display a specific task. However I don't know where to set the job name,
I tried the followings
1-) job/MYJOBNAME
2-) JOBPATH/job/MYJOBNAME
3-) replace job with MYJOBNAME
I can't make it works.
Also would it be possible to only display a list of number? Instead of having to use a grep on the *.xml.
This works for me:
JENKINS_URL/job/JOB_NAME/api/xml?depth=2&xpath=//build[keepLog=%22true%22]/number&wrapper=forever
Sample output:
<forever>
<number>688</number>
<number>687</number>
</forever>
forever is a wrapper parameter. You will find more details if refer to JENKINS_URL/api:
For XPath that matches multiple nodes, you need to also specify the "wrapper" query parameter to specify the name of the root XML element to be created so that the resulting XML becomes well-formed.

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