How to read parameter from Paramterized Build using ruby? - jenkins

I'm using Paramterized Build, and know I can get paramter by name in bash like $name.
But I want to write script with ruby, how can I get the paramter?
Thanks

you should be able to get them using env variables
http://ruby.about.com/od/rubyfeatures/a/envvar.htm

To send parameters to your ruby script, you may use the following way:
call the script and pass the parameters from the command line (for Jenkins, you may use execute shell script option)
$ irb ruby_script.rb parameter1 parameter2
and in ruby_script.rb, you can get these parameters by using builtin ruby syntax, ARGV:
parameter_one = ARGV[0] // refer to the first paramter
parameter_one = ARGV[1] // refer to the second paramter
You may refer to this issue

Related

Jenkins Extended Choice Parameter with groovy shell command

I try to collect data for an Extended Choice Paramater.
I use the code:
def ingo = sh(script: 'mktemp', returnStdout: true)
return ingo
inside the groovy script part, but it seem that this is not allowed or well formated.
The choice is always empty. Anybody has some experience with shell command inside this part of the pipeline?
Sense is, I want to collect data with curl here. But simple shell query is not working.
Please see image
You can't execute Jenkins steps within the Extended Choice Parameter. You have to use pure Groovy within the Parameter. For example, if you want to do an HTTP call you can use a Groovy script like below. Here I'm fetching content from a GitHub file. A Full example can be found here.
def content = new URL ("https://raw.githubusercontent.com/xxx/sample/main/testdir/hosts").getText()

Jenkins pass variable into groovy script

Hi I need to pass a variable from select into the groovy script and I haven't a clue how to do it in variable bindings, anyone has an idea how to do it?
I tried with:
version=version
version=$version
version=${version}
version="${version}"
If you are going to use ${ ... } Notation you should enclose it double quotes, i.e.:
versionVar = "${version}"
The other think that keeps bothering me is that you are using the same variable name. I haven't tried but I think that using the same name you could are trying to use the same variable.
Where is the 'version' variable came from?
If you are trying to use Environment variables, then you should try like,
version = env.version

JMeter: more than nine parameters from Jenkins

I am trying to pass more than nine parameters from Jenkins to JMeter4.0.
As I was reading, I found out that JMeter does not accept more than 9 parameters. As a workaround, I want to pass all the parameters as a string and split it in JMeter BeanShell.
java -jar -Xms512m -Xmx2048m C:\JMeter4\bin\ApacheJMeter.jar -Jjmeter.save.saveservice.output_format=csv -Jjenkinsparams="%Timetorun%,%Users%" -n -t %JMeterPath%\bin\tests\tests.jmx -l %WORKSPACE%\Results.csv
The tests run on a Windows machine. From this call I have
jenkinsparams = "300,2"
I use a BeanShell PreProcessor like this:
String line = "${__P(jenkinsparams)}";
String[] words = line.split(",");
vars.put("timetorun",words[0]);
vars.put("users",words[1]);
log.info(words[1]);
log.info(users);
I tried few log.info to check the values. For words[1] I have the correct value sent from Jenkins: 2. For the users the value displayed is: void.
I am trying to use it for Number of Threads as: ${__P(users,1)}.
What am I doing wrong? The values clearly arrive from Jenkins but I have a problem passing it to my variable. Thank you
You don't have a script variable named users, so you should either log words[0]:
log.info(words[0]);
Or you can log the value of JMeter variable called users:
log.info(vars.get("users"));
Or you can assign words[0] to variable called users:
String users = words[0];
log.info(users);
Also you are saving it as variable, not property, so you can retrieve it elsewhere in script as
${users}
The syntax __P refers to property, so if you want to use it as property, you need to change how you are saving it:
props.put("users", words[1]);
If you do that, ${__P(users,1)} should work
Now, if you want to use this value as number of threads, then you need to do this:
Have Setup thread group with 1 thread, and your script
In the script you must save users as property, otherwise it won't pass between threads
Next thread group then can use it as number of threads
As long as your command line fits into 8191 characters it should not be a problem to pass as many arguments to JMeter as you want, here is an evidence from Debug Sampler and View Results Tree listener combination
So keep calm and pass as many parameters as needed via -J command line arguments.
Be aware that starting from JMeter version 3.1 users are recommended to use JSR223 Test Elements and Groovy language instead of Beanshell so going forward please consider switching to Groovy.

How to pass URL in ruby command line?

I have written a ruby program in which it takes the input as url and it displays the components in it.
I passed the URL inside the ruby file but now I want to pass it through ruby command line like ruby filename.rb url.
How to pass an argument inside the code.
The ARGV constant holds an array which contains all of the arguments passed in on the command line. So in your script, you could get the url passed in via ARGV[0].
ARGV is an array which contains all the arguments passed into your script via the command line.
ARGV.each do|arg|
puts "Argument: #{arg}"
end
Just use ARGV constant , you can puts ARGV.to_s in log to see what`s in it , to helps you understand how to use it .

How can I pass the arguments to the lua file through the lua CLI

I have a LUA CLI which takes in the lua command,
Something like this
(lua)> #
Now here inorder to execute the lua file I run the command
(lua)> # dofile("a.lua")
I want a command which will execute the file and also pass a argument to it.
Now here I want to pass a argument to the "a.lua" file which will take this argument and call one more lua file, and this 2nd lua file is called according to the argument, So, I need to parse this argument.
Please, can somebody tell me about the parsing commands that will be used in a.lua. I mean what are the functions to be used to parse it.
Please, can somebody tell me how to pass a argument to this file "a.lua".
Now here inorder to execute the lua file I run the command
This is generally not how you execute Lua files. Usually, if you have some Lua script, you execute it with this command: lua a.lua. You don't type lua and then use the interface there to execute it.
Using a proper command line to execute the script, you can pass string parameters to the file: lua a.lua someParam "Param with spaces". The a.lua script can then fetch these parameters using the standard Lua ... mechanic:
local params = {...}
params[1] -- first parameter, if any.
params[2] -- second parameter, if any.
#params -- number of parameters.
However, if you insist on trying to execute this using your method of invoking the interpreter (with lua) and typing commands into it one by one, then you can do this:
> GlobalVariable = assert(loadfile(`a.lua`))
> GlobalVariable(--[[Insert parameters here]])
However, if you don't want to do it in two steps, with the intermediate global variable, you can do it in one:
> assert(loadfile(`a.lua`))(--[[Insert parameters here]])

Resources