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
Related
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
In my previous question I was looking for a way to access and store return value of the function in qaf step. I was provided with the following:
When create new user using "{'name':'user1','password':'user123'}"
And store into 'newUser'
Then system should have user '${newUser}'
Now, I'd like to know how to get value from object/collection stored.
If it is a simple object named newUser which has field Id. How would I pass Id on next step?
And, if return is List, how to get by index from stored list?
Resolved issue on my own. If anyone faces same unknowns, here is how I solved it.
For requirements to work around response data, parsing same stored objects in properties by specific fields or collecting data from other structures such as Maps or Lists, create common functions with #QAFTestStep annotation to get data for class member name, map by key or list by index and so on... Add those in common steps and then write stepname text in gherkin format with parameters specified. Let me know if someone needs help, always ready to help out...
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.
I have a use case where in I have to pick up selected data from a spanner table and dump to BigQuery.
The catch here is that for the batch job the name of the table and the columns to select will only be known at runtime.
It seems that dataflow's SpannerIO doesn't accept the table and the columns at runtime. Please refer below code for better understanding:
p.apply(SpannerIO.read().withSpannerConfig(spannerConfig)
.withTable("tablename")
.withColumns(list or array of columns))
It only accepts string and not ValueProviders. How to make this work?
To access runtime values you need to use the ReadAll transform and build an instance of ReadOperation in the previous step.
See Reading data from all available tables from the examples.
Yes, the withColumn and withTable methods of SpannerIO.Read do not take in a ValueProvider by default.
Could you write code outside your function to get the table and column names and then pass them into withTable and withColumn as a list of strings at runtime?
If they can be passed in as commandline arguments, consider using PipelineOptions.
Here is a simple example. More docs on using dataflow connectors from Cloud Spanner can be found here.
I used read operation as suggested by Mairbek
p.apply(Create.ofProvider(options.getMyParamValueProvider(), StringUtf8Coder.of()))
.apply(MapElements.via(new SimpleFunction<String, ReadOperation>() {
#Override
public ReadOperation apply(String value) {
return ReadOperation.create()
.withTable("TableName").withColumns(value);
}
}))
.apply(SpannerIO.readAll().withSpannerConfig(spannerConfig));
When accessing a Config.groovy property using grailsApplication.config.myapp.something
Is it possible to build the property key programmatically somehow? e.g. grailsApplication.config.myapp. + somethingVar.toString()
Groovy allows you to use GString expressions for property accesses, so
grailsApplication.config.myapp."${somethingVar}"
will do what you want as long as somethingVar doesn't contain any dots. If you have a variable that contains the whole config key including the dots then you can use flatConfig:
def key = "myapp.something"
def value = grailsApplication.flatConfig."${key}"
or if the variable is part of the "path" but not the whole:
def key = "some.thing"
def value = grailsApplication.flatConfig."myapp.${key}" // gives myapp.some.thing
or you can avoid the flatConfig by using a trick with inject
def key = "some.thing"
def value = key.split(/\./).inject(grailsApplication.config.myapp) { co, part ->
co."${part}"
}
The inject method calls the closure once for each item in the array we're iterating over, each time passing in the value that the last iteration returned (I've called it co as it will be a ConfigObject) and the value for this iteration (part). The overall result of inject is the value returned by the last iteration.