I would like to do the following as debugging output in a Jenkinsfile:
echo fileExists("path/to/some/file")
This does, however, not work. fileExists returns a boolean, which can apparently not be implicitly cast to a string, and the echo command wants a string.
So what's the correct way to print a boolean?
echo "${fileExists("path/to/some/file")}"
or more generally
echo "${boolean_expression}"
works. Is this how you're supposed to do it?
Related
This is inside my trigger email
SQLScripts=["Query1","Query2","Query3","Query4","Query5","Query6"]
What gets read in string parameter for Jenkins build is following
This line of code executes
echo %SQLScripts%
Prints
echo ["Query1","Query2","Query3",
Initially I thought this could be some problem with how I wrote variable name, I tried $SQLScripts and "$SQLScripts". But problem is with reading the variable from email.
As I have manually added value inside jenkins build configuration and echo printed the entire value.
Please any help is appreciated.
in Jenkins file one of the variable is having the comma separated values like below.
infra_services=[abc,def,xyz]
when I write the below code it was throwing an error.
if ("{$Infra_Services}".contains("xyz"))
then
echo "$Infra_Services"
fi
yes you can do if statements in a Jenkinsfile. However if you are using declarative pipeline you need to brace it with the step script.
Your issue comes from the fact you did not put any double quotes around "abc" and all the elements of your array
infra_services=[abc,def,xyz]
β
A second error will raise after you fix this. If infra_services is an array, to manipulate it you should not try to cast it as string. It should throw when you do "{$Infra_Services}"
here is a working example
βdef Infra_Services = ["abc","def","xyz"]
if (Infra_Services.contains("xyz")) {
println "found"
}ββ
My advice is to test your groovy before running it on jenkins, you will gain precious time. Here is a good online groovy console I use to test my code. running the groovy console from terminal is an alternative
https://groovyconsole.appspot.com/
I have set one String parameter for Jenkins parametrized job
String: MOV
Default value: 5
But when I log output as echo: "${MOV}", that value is null.
I tried with single quotes, without any quotes, without dollar sign but every time my value is null.
Can anyone help me?
Try params.MOV:
echo "${params.MOV}"
https://jenkins.io/doc/book/pipeline/syntax/#parameters
I'm using a grunt shell of PIGLATIN and I am trying to print a simple message, like in shell ECHO "result is :" and then result given by Pig script .
However I have done all the searches and no luck so far.
Echo returns error , same as print.
I can't use UDFs...
You can DUMP the alias or STORE the alias in to file to see the alias values.
Refer :
http://chimera.labs.oreilly.com/books/1234000001811/ch05.html#pl_dump
http://chimera.labs.oreilly.com/books/1234000001811/ch05.html#pl_store
I want to pass array in argument in such a way suppose process.rb is my script and the argument will be like:
i/p
process.rb server{1..4}
process.rb prodserver{2..3}
process.rb devserver3
The process.rb should accept all the inputs and parse it in such a way that when I print the variable which holds the arguments give me below result.
o/p
puts arguments
server1
server2
server3
server4
or
prodserver2
prodserver3
or
devserver3
I have a shell script which does the same:
for i in "$#"
do
echo $i
done
i/p
server{1..4}
o/p
server1server2server3server4
I wanted to have the same logic in the ruby.
Since I am a new bie in ruby I am not able to find the same on google.
Please let me know how I can get this output or any article about the related to my question
The list is expanded by the shell before it ever hits your script. In other words, both your shell script and your Ruby script do not receive a single argument server{1..4} but rather they receive four arguments server1 server2 server3 server4, before they even start interpreting the arguments themselves.
You can simply just iterate over those, there is no need to parse the {1..4} shell expansion syntax yourself because you will never see it! It is already parsed and expanded by the shell before the shell passes off the arguments to your script.
ruby -e 'p ARGV' -- server{1..4}
# ["server1", "server2", "server3", "server4"]
#!ruby
ARGV.each do |i|
puts i
end
Basically ARGV holds all arguments passed to program, and puts prints string with new line added (the same as echo without -n flag in shell).
Command-line arguments in Ruby end up in ARGV. You can duplicate your shell script's functionality by iterating over that:
ARGV.each do |a|
puts a
end
If I understand you correctly you want to expand the range that comes in string form from your argument ARGV[0] ? My samples use a string to demonstrate it workd, replace the string by ARGV[0]
def expand_range arg
string, range = arg.split("{") #split arg in string part and rangestring part
if range #if a range is given
# parse the rangestring to an range by splitting the string on ..
# and splash this array to both its elements, convert them to integer
# and transform into a real range
# and enumerate each number in the range
Range.new(*range.split("..").map(&:to_i)).each do |val|
#concatenate the string part with the number
p "#{string}#{val}"
end
else #else just pass the string
p string
end
end
expand_range 'server{1..4}'
# "server1"
# "server2"
# "server3"
# "server4"
expand_range 'devserver3'
#"devserver3"
Personally I would return an array and print that instead of printing in the method itself, that would be more multifunctional.