How to convert MultiSelect values from Active Choice Parameter in Jenkins into an array or list? - jenkins

I have parameterized job with 'list' MultiSelect parameter in Jenkins:
I want to build this job only with selected values:
And for this purpose, I want to pass this selected values to pipeline script in this job and convert them into an array or list.
The following code doesn't work properly:
Please, help me to write the proper code in groovy to convert selected values to array or list.
Thanks,
Anastasiia

Assuming parameter name "list" and property setting described in question, below pipeline code worked for me.
String[] arr= "${params.list}".split(',');
for (x in arr) {
echo "$x \n"
}
This will print each selected values on new line.

Related

Can we Read Config file(managed files .properties file) in jenkins active choice parameters?

I want to Read My properties File in active choice parameters grovy script , my properties file is stored in manged files .
Properties file look like these
[1]: https://i.stack.imgur.com/flvP5.png
I want to call these properties file in Active Choice Reference Parameter Groovy script and retrive
all the values as per my previous selection.I had been using differnt not able to retrive values is their any way that we could retrive values?
The properties file would like this 'cat /var/jenkins_home/workspace/ss.properties'
100.1.1.1=outside_in,wireless_in
200.x.x.x=mgmt_in,inside_in
Create a Parameter called "Active Choices Parameter", call it "hostnames" and write the following groovy script there. In the following groovy script, we are simply reading the keys from the property file, converting it into a list and populating the same in a choice parameter. The choice type for this in my case is single select or you can set it according to your needs.
Properties properties = new Properties()
File propertiesFile = new File('/var/jenkins_home/workspace/ss.properties')
def stream = propertiesFile.newDataInputStream()
properties.load(stream)
Enumeration e = properties.propertyNames();
List<String> list = Collections.list(e);
return list
Create another Parameter called "Active Choices Reactive Parameter" and call it "config_list" and paste the following groovy script there
Properties properties = new Properties()
File propertiesFile = new File('/var/jenkins_home/workspace/ss.properties')
def stream = propertiesFile.newDataInputStream()
properties.load(stream)
Enumeration e = properties.propertyNames();
def var = Arrays.asList(properties.getProperty(hostnames).split(','))
return var
The Referenced Parameter for the above would be "hostnames". That would mean, based on the selection of hostnames choice parameter, the data will be populated in this parameter. The choice type for this in my case is single select or you can set it according to your needs.
Save the configuration and click on build with parameters in your Jenkins Job, it should look like the following

How to convert array variable from trigger to javascript array in dynaform panel?

In Processmaker 3 I use Trigger to query the database and store the result in Array.
I want to use this array in Dynaform panel control as a Javascript array so I can display it using Javascript.
How I can Convert it from normal PHP array to javascript array?
I attempted to use JSON convert functions but it never worked for me.
hidden : https://forum.processmaker.com/viewtopic.php?t=437
in a trigger pass #=hiddenFieldName to result of query
then use hiddenFieldName value with :
$('#hiddenFieldName').getValue()

currentBuild() in jenkins

I have a DSL FreeStyleJob in jenkins. Let's say job/project named A is calling another job/project B as a post-build actions. I am making use of downstreamParameterized and passing currentBuild() paramaters as below
downstreamParameterized {
trigger(B) {
condition('SUCCESS')
parameters {
currentBuild()
}
}
}
And I know job/project B to be parameterized (Also DSL job). But how do I accept currentBuild() as a parameter in B. But I don't see any option related to it. Can anyone suggest?
See this pic for parameter options
I have already tried with predefined properties in job/project A and string parameters in job/project B and it works fine. But I don't want to follow this as there are many different type parameters in current build parameters.
I can't take file parameter options as both job workspace are different.
Solution is straight forward if the parameters are same for both the jobs. All you need to do is to declare the same set of parameters in JOB B as in JOB A. Jenkins will take care of passing the parameters to the downstream job.
As long as you have the same set of parameters in both the jobs it should work.
Reference :
Job 1 :
Job 2 :
Parameterized trigger in JOB 1
Thanks

Not able to read parameters from properties file in jenkins

Here is what I am trying to achieve. - I have two 'Choice Parameters' in my Jenkins job. The values of the first Choice Parameter are hard coded. The second choice list should be populated based on the first choice list selection. I have one properties file saved in Jenkins, which has key-value pairs. The values in first choice list and the Keys in the file are same. On selecting a value in first choice list i want a code to read the properties file and populate the second choice parameter with the values from file corresponding to that key.
For second choice list i am trying with 'Active Choice Reactive Parameter' , Referenced parameters= first_choice and below groovy script. But this is not returning any values. Please help!
def firstChoice = [first_choice]
Properties props = new Properties()
def stream = new FileInputStream('C:/Jenkins/books.properties')
try{
props.load(stream)
}
catch (Exception ex){
println "Exception"
}
finally {
stream.close()
}
def values = props.getProperty(firstChoice).split(",")
return values
Are the parameters defined in your job? if you're trying to inject parameters that are not defined in the job you need to either define them or add exception in Jenkins when you're loading the master service.
More reading:
https://wiki.jenkins-ci.org/display/JENKINS/Plugins+affected+by+fix+for+SECURITY-170
Follow this answer and use active choices parameter. This will help you achieve what you want to
https://stackoverflow.com/a/73742809/4781673

jQuery UI Autocomplete question on label and value

In the label we have item.ID + '-'+ item.Description
the value is item which is the object returned from the service.
When a selection is made 001-MyChoice for ex. from the autocomplete, the value which is the datacontract object is bound to the autocomplete field as [Object object]. How can I get 001-MyChoice to be bound for the selection ???
Pls help..
It's difficult to tell when you haven't posted any code to review. What format is your data returned from the service? JSON, XML?
I'm sure you're already looked at this link, but here it is just in case:
http://jqueryui.com/demos/autocomplete/#remote
Post some sample code of your js and some sample data of what your service returns for a better answer.
The autocomplete tags returned from your web service need to contain the tag 'term' and I think "ID", then the default call can help pull them out. Although not a perfect example if you look at the Jasonp example in the jQueryUI docs, that may help.
This is from memory but something like
{"term":"dog", "ID": "123"}, {"term":"cat","ID": "2"}
So you have 001-MyChoice as the label and you want it bound as the value?
Then don't specify the object returned as the value. Label and value are not required by the autocomplete. You can specify both if you want one in the dropdown select (label) and the other (value) used as the value of the select.
If you want the select item and the value to be the same, then put the same item in either the label or the value property.
From the jquery autocomplete docs:
The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.
If you need to use the datacontract object, then just assign it to a variable in the select callback function of the autocomplete and you can use it as that.

Resources