Changing variable deletes other variable in Jenkins groovy-script - jenkins

I use a free style job in Jenkins that has 2 Parameters that the user can change at the start:
ReleaseBuild - boolean
PluginVersion - string
I use a system groovy script to read an change the variables
First I read the content of ReleaseBuild:
def isRelease = build.buildVariableResolver.resolve("ReleaseBuild").toString();
println "Is ReleaseBuild: " + isRelease
The output shows the correct value: Is ReleaseBuild: true
I need to replace the content of the second variable:
def verParameter = new StringParameterValue('PluginVersion', '1.0')
build.addOrReplaceAction(new ParametersAction(verParameter))
Now I check the content of ReleaseBuild variable again:
def isStillRelease = build.buildVariableResolver.resolve("ReleaseBuild").toString();
println "Is ReleaseBuild: " + isStillRelease
Now the variable seems to be gone. Output: Is ReleaseBuild: null
How can I change the content of PluginVersion without deleting ReleaseBuild variable?

I solved my problem meanwhile with a workaround:
When I want to update the value, I update all values (this is ok here, since I only have two)
def verParameter = new StringParameterValue('PluginVersion', ver)
def relParameter = new BooleanParameterValue('ReleaseBuild', isRelease)
build.addOrReplaceAction(new ParametersAction(verParameter, relParameter))
Still hoping to get a better solution but at least it works.

Related

Is there a way to replace mapping values with another variables value in jenkins pipeline

I have following mapping defined
def e_environments = [["demo1#mystuff.com":"file1"], ["demo2#mystuff.com":"file1"], ["insurance1#mystuff.com":"file2"], ["insurance2#mystuff.com":"file2"]]
Is there a way to replace URL's with same file name from a variable value. For example
def demo_env = ["demo1#mystuff.com","demo2#mystuff.com"]
def insurance_env= ["insurance1#mystuff.com","insurance2#mystuff.com"]
def def e_environments = [[${demo_env}:"file1"], [${insurance_env}:"file2"]]
I hope you must have made some efforts from your side before asking here.
Here is something for you to work upon,
def e_environments = [ "demo1#mystuff.com":"file1", "demo2#mystuff.com":"file1" , "insurance1#mystuff.com":"file2" ,"insurance2#mystuff.com":"file2" ]
def temp = []
e_environments.keySet().each{
temp.add(e_environments[it])
}
temp.unique().each{ val ->
println e_environments.findAll{it.value==val}
}
Got all keys from Map
Created a unique list of it
And a little bit of groovy magic for you to fix and figure it out.

Unable to access Choice Parameter selected values inside next "extended choice parameter" value (groovy script)

step1 - Declared "appname" as choice parameter with 10 values.(single select)
step2 - To get the latest artifact of the above appname, used "artifact_name" as extended choice param
with 'max count-10' delimeter as ','
So the "artifact_name" values , i mean the last artifacts like 10 should be displayed inside
"single select" list box of "artifact_name"
-> Groovy Source script for "artifact_name" value is below.
def filter= "${params.appname}" //Not able to get above declared variable to new variable and use it dynamically in below url to filter the artifacts)
def con = def connection = new URL( "https://hostname/artifactory/source-artifactory/$filter/?list&deep=1&listFolders=0&mdTimestamps=1&includeRootPath=0" )
.openConnection() as HttpURLConnection
So am not able to access the "appname" value and assign it to a new variable like "def filter = "${params.appname}" and use the new variable in connection url for every application name to get the artifacts. S
Hardcoded value working but i have 10 apps can't create ten jobs with hardcoded values.

Error with index global in Lua recipe for foldit.org

I am just starting writing scripts for Foldit. This is my first experience of Lua, so my status is "noob with insight". In my first script, I want to print out contactmap data. I have copied and pasted the functions from http://foldit.wikia.com/wiki/Foldit_Lua_Functions. The error I get is:
attempt to index global 'structure' (a nil value)
Here is my code:
segmentCount = structure.GetCount()
print ("Segment count: " .. segmentCount)
for source = 1, segmentCount do
for target = source + 1, segmentCount do
inContact = contactmap.IsContact(source, target)
heat = contactmap.GetHeat(source, target)
print("Segments "..source..", ".. target..": heat = "..heat)
end
end
Thanks in advance for any enlightenment.
EDIT
The problem was that I had originally loaded a V1 recipe, and foldit continued to treat it as V1 syntax. The solution was to create a New (ScriptV2) recipe, and load exactly the same code.
You seem to be doing everything according to the documentation. The error means that contactmap is not defined (has nil value) and the code fails on accessing its elements. I'd check the version of Foldit you are using as the documentation says that contactmap is new in version 2.

changing a variable using gets.chomp()

im trying to write to a file using this code:
puts "-------------------- TEXT-EDITOR --------------------"
def tor(old_text)
old_text = gets.chomp #
end
$epic=""
def torr(input)
tore= $epic += input + ", "
File.open("tor.txt", "w") do |write|
write.puts tore
end
end
loop do
output = tor(output)
torr(output)
end
i have read the ultimate guide to ruby programming
and it says if i want to make a new line using in the file im writing to using File.open
i must use "line one", "line two
how can i make this happend using gets.chomp()? try my code and you will see what i mean
thank you.
The gets method will bring in any amount of text but it will terminate when you hit 'Enter' (or once the STDIN receives \n). This input record separator is stored in the global variable $/. If you change the input separator in your script, the gets method will actually trade the 'Enter' key for whatever you changed the global variable to.
$/ = 'EOF' # Or any other string
lines = gets.chomp
> This is
> multilined
> textEOF
lines #=> 'This is\nmultilined\ntext'
Enter whatever you want and then type 'EOF' at the end. Once it 'sees' EOF, it'll terminate the gets method. The chomp method will actually strip off the string 'EOF' from the end.
Then write this to your text file and the \n will translate into new lines.
File.open('newlines.txt', 'w') {|f| f.puts lines}
newlines.txt:
This is
multilined
text
If you dont use .chomp() the \n character will be added whenever you write a new line, if you save this to the file it also will have a new line. .chomp() removes those escape characters from the end of the input.
If this doesnt answer your question, i am sorry i dont understand it.

Cannot access params map using Groovy

I want little help which I suspect is due to my lack of understanding for Groovy syntax. So, here's the thing:
On the GSP page I want to set a field's value from the params map which is
["id":"107901", "Field_10.value":"2", "Field_10":["value":"2"],"Field_11.value":"", "Field_11":["value":""],action:'abc']
On the gsp page, I want to find the value against the key Field_{some-id}.value
So I am calling a tag like, g.testTag(id:field.id) with its implementation as
def testTag = { attrs,body->
println "params are ${params}"
def result = ""
def keyRequired = "Field_${attrs.id}.value"
println "keyRequired >>>>> ${keyRequired.toString()}"
params.each { key,value->
println "key is ${key}"
println "Value is ${value}"
if (key.equals(keyRequired.toString())) {
result = params.value
}
}
println "Final result is >>>>>> ${result}"
}
The value passed in id is 10 and with my params printed as above, I was expecting a value
of 2 which is corresponding to the key in the params to show up. But apparently I see the
result as null..
What am I doing wrong ? Can anyone help please...
Thanks
Not result = params.value, but result = value.
You have to change the line:
result = params.value
to:
result = value
At the each loop, you're basically saying that inside the params iteration, you're naming every key "key" and every value "value". So, params.value will actually look for the key value inside your params map, which is null.
Funny that you do that right with key but not with value. Probably just got distracted.
it is likely what you want to do, the groovy way (no need to loop over the keys of the map) to access "Field_10.value":"2"
result=params["Field_${attrs.id}.value"]
Alternatively, this also works because you have "Field_10":["value":"2"] in your map
result=params["Field_${attrs.id}"].value

Resources