My parameterized Freestyle job got one string parameter. MAIL_PARAM with the default value FREESTYLE_ERROR.
I am able to print the value with:
println "MAIL_PARAM=$Mail_Param"
Inside an Groovy execute Script. Now I want to change the value of this parameter based on some conditions. But I am not able to change it. I tried:
MAIL_PARAM = 'String'
$MAIL_PARAM ='String'
${MAIL_PARAM} ='String'
def params = new StringParameterValue('MAIL_PARAM', 'String')
and some more, but none of them are working. I have to change it because based on some results my groovy script has, I need different Strings inside of my parameter.
After the groovy script I need to pass this parameter to the next job. This works fine. But I only get the default value.
If I understand correctly, replaceAction should do the trick (there is also addOrReplaceAction):
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def newMailParameter = new StringParameterValue('MAIL_PARAM', '...')
build.replaceAction(new ParametersAction(newMailParameter))
Edit : if you get error "current build does not have any parameter" then please try "build.addOrReplaceAction" in place of "build.replaceAction".
modify from setBuildParameters: http://jenkins-ci.361315.n4.nabble.com/Modifying-a-builds-parameters-in-a-system-Groovy-script-td4636966.html
def addOrReplaceParamValue = { String name,String value ->
def build = currentBuild.getRawBuild();
def npl = new ArrayList<StringParameterValue>()
def pv = new hudson.model.StringParameterValue(name,value);
npl.add(pv);
def newPa = null
def oldPa = build.getAction(hudson.model.ParametersAction.class)
if (oldPa != null) {
build.actions.remove(oldPa)
newPa = oldPa.createUpdated(npl)
} else {
newPa = new hudson.model.ParametersAction(npl)
}
build.actions.add(newPa);
};
addOrReplaceParamValue("P1","p1");
Here's a more complete example if you need to modify based in the original value:
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def transformJobParameter(Closure transform) {
build.getActions(ParametersAction).each { paramAction ->
List<ParameterValue> overrides = []
paramAction.each { param ->
// Transformation takes a ParameterValue object but returns only its new value object, if any.
def newValue = transform(param)
if (newValue != null && newValue != param.value) {
// Create whatever the original object type was, but with a new value.
def newParam = param.class.newInstance([param.name, newValue, param.description] as Object[])
overrides << newParam
println("INFO - Transformed ${param.name} parameter from '${param.value}' to '$newValue'.")
}
}
if (!overrides.empty) {
def mergedParamAction = paramAction.merge(new ParametersAction(overrides))
build.replaceAction(mergedParamAction)
}
}
}
transformJobParameter { param ->
if (param instanceof StringParameterValue) {
def value = param.value.trim()
if (param.name == 'MAIL_PARAM') {
'String'
} else {
value
}
}
}
Related
I'm trying to construct a convenient shared library for my scripted pipelines.
I want to be able to use an object from my Jenkinsfile that has several functional methods, but also stores some initial variables that are referenced by several methods.
So, I've defined a file in "vars" with a bunch of methods for functionality, but also a handful of get/set methods for the properties I want to store in the object for reference by several methods.
My initial testing is just to set some of the variables (not all of them) and call the "toString()" method which I've defined in the file (just for convention, obviously).
In my test Jenkinsfile, If I do set all of the variables and then call "toString()", it works fine and completes.
However, if I try commenting out one of the variable initializations, when it hits the line in the "toString" method that constructs the return value, Jenkins just hangs forever. I eventually just kill the job.
I've been able to avoid the hang by qualifying each reference with 'if binding.variables.containsKey("foo")) {', only referencing the variable if that is true. I made this a tiny bit cleaner by putting the binding check in each of the getter methods instead.
I don't really like this workaround. It just seems odd that I would have to do this.
I've tried several variations, but I don't set the variable and I try to reference it in a gstring, the job hangs forever, every time.
This is a brief excerpt from the "vars" file:
def setJobName(value) { jobName = value; }
//def getJobName() { return jobName }
def getJobName() { return (binding.variables.containsKey("jobName") ? jobName : "") }
def setMechIdCredentials(value) { mechIdCredentials = value; }
//def getMechIdCredentials() { return mechIdCredentials }
def getMechIdCredentials() { return (binding.variables.containsKey("mechIdCredentials") ? mechIdCredentials : "") }
... more get/set methods
String toString() {
echo "Inside uslutils.toString()."
This is an excerpt from the Jenkinsfile that uses this:
uslutils.currentBuild = currentBuild
uslutils.jobName = env.JOB_NAME
uslutils.buildURL = env.BUILD_URL
//uslutils.mechIdCredentials = "abc"
return "[currentBuild[${currentBuild}] mechIdCredentials[${mechIdCredentials}] " +
"baseStashURL[${baseStashURL}] jobName[${jobName}] codeBranch[${codeBranch}] " +
"codeURL[${codeURL}] buildURL[${buildURL}] pullRequestURL[${pullRequestURL}] QBotUserID[${QBotUserID}] " +
"QBotPassword[${QBotPassword}]]"
}
So, for instance, if I swapped the two variations of "getMechIdCredentials", leaving the "plain" one, this combination of samples will hang, until I click on the red X on the "Progress" indicator in Jenkins.
Update:
So, based on feedback, I defined variables in the file and changed my getters/setters to use the "this.#var" syntax. As a result, the build fails with "No such field: var for class: uslutils".
I imagine my syntax for defining the field isn't correct, but here's an excerpt of what I have:
def currentBuild = ""
String jobName = ""
String buildURL = ""
def mechIdCredentials = ""
def setCurrentBuild(value) { this.#currentBuild = value; }
def getCurrentBuild() { return this.#currentBuild }
def setJobName(value) { this.#jobName = value; }
def getJobName() { return this.#jobName }
def setBuildURL(value) { this.#buildURL = value; }
def getBuildURL() { return this.#buildURL }
def setMechIdCredentials(value) { this.#mechIdCredentials = value; }
def getMechIdCredentials() { return this.#mechIdCredentials }
Note that this is NOT within a "class" declaration, it's in a Groovy script file called "uslutils.groovy".
To be clear, here is an excerpt of the stack trace I get:
groovy.lang.MissingFieldException: No such field: mechIdCredentials for class: uslutils
at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2823)
at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3759)
at org.codehaus.groovy.runtime.InvokerHelper.getAttribute(InvokerHelper.java:145)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getField(ScriptBytecodeAdapter.java:306)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getAttribute(DefaultInvoker.java:42)
at com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
at uslutils.getMechIdCredentials(/opt/app/jenkins/sdt-usl/data/jobs/uslutils-tests/builds/33/libs/usl-pipeline-library/vars/uslutils.groovy:197)
at uslutils.toString(/opt/app/jenkins/sdt-usl/data/jobs/uslutils-tests/builds/33/libs/usl-pipeline-library/vars/uslutils.groovy:242)
at WorkflowScript.run(WorkflowScript:13)
Line 242 is shown here:
String toString() {
echo "Inside uslutils.toString()x."
return "[currentBuild[${currentBuild}] mechIdCredentials[${mechIdCredentials}] " + // line 242
"baseStashURL[${baseStashURL}] jobName[${jobName}] codeBranch[${codeBranch}] " +
"codeURL[${codeURL}] buildURL[${buildURL}] pullRequestURL[${pullRequestURL}] QBotUserID[${QBotUserID}] " +
"QBotPassword[${QBotPassword}]]"
}
I am working in a grails 2.4.4 application which has some legacy code that doesn't use binding or command objects in controllers, in this application the request parameters are passed from controller actions directly to java classes using their constructors.
We want to trim white spaces from strings parameters (just like Grails does by default when binding) but without using binding or command objects.
Maybe injecting a cloned version of the params varible but with trimmed values?
Have any of you done something like this?
Here's an implementation I used in a 2.x app in a filter. It supports nested objects by calling the method recursively:
def filters = {
blankToNullAndTrim(controller: '*', action: '*') {
before = {
convertBlanksToNullsAndTrim params
true
}
}
}
private static void convertBlanksToNullsAndTrim(Map<String, Object> map) {
def keys = map.keySet() as List // copy to avoid ConcurrentModificationException
for (name in keys) {
def value = map[name]
if (value instanceof String) {
value = value.trim()
if (value.length() == 0) {
map[name] = null // have to explicity set to null, not remove
}
else {
map[name] = value // update if trimmed
}
}
else if (value instanceof Map) {
// empty nested param, e.g. "location":["id":""]
convertBlanksToNullsAndTrim value
}
}
}
and here's the equivalent implementation (with type information included so you can use #GrailsCompileStatic) for use in an interceptor when you upgrade to Grails 3.x:
boolean before() {
convertBlanksToNullsAndTrim params
true
}
private void convertBlanksToNullsAndTrim(Map<String, Object> map) {
List<String> keys = map.keySet() as List // copy to avoid ConcurrentModificationException
for (String name in keys) {
def value = map[name]
if (value instanceof String) {
value = value.trim()
if (value.length() == 0) {
map[name] = null // have to explicity set to null, not remove
}
else {
map[name] = value // update if trimmed
}
}
else if (value instanceof Map) {
// empty nested param, e.g. "location":["id":""]
convertBlanksToNullsAndTrim value
}
}
}
One option will be to use Dynamically Resolved Variables in UrlMappings.groovy. For example if there is a mapping as follows:
"/airport/status(controller: 'airport', action: 'checkStatus')
and expecting a request parameter as airportName then it can be rewritten as:
"/airport/status(controller: 'airport', action: 'checkStatus') {
airportName = { params.airportName?.trim() }
}
This would make sure that the arbitrary variable is set to params after trimming the actual request parameter airportName. Care has to be taken to use the same variable name as the request parameter to support the legacy system underneath.
Another option can be to use a filter across the controller to trim the request parameters. Something like:
class UtilityFilters {
def filters = {
trim(controller: "airport", action: "*") {
before = {
// If immutable then make a copy and edit
params.airportName = params.airportName?.trim()
return true
}
}
}
}
From the called build flow job, I've tried both:
build.environment['AOEU'] = 'aoeu' // callee would `println called.environment['AOEU']`
and:
upstream.environment['AOEU'] = 'aoeu' // callee would `println build.environment['AOEU']`
with no luck.
I fought a lot with that too and found no clean way to do it. I finally used EnvInjectPlugin in some ugly way to do this.
def buildEnv = build.getEnvVars();
varsToAdd = [:]
// add here your custom properties
buildEnv.putAll(varsToAdd)
import org.jenkinsci.plugins.envinject.EnvInjectPluginAction
def envInjectAction = build.getAction(EnvInjectPluginAction.class);
envInjectAction.overrideAll(buildEnv)
... the EnvInject plugin did the magic
I first tried to implement EnvironmentContributingAction
and add it as build.addAction(...) but did not work for me for unknown reason.
Be sure to set 'Flow run needs a workspace' in the called job.
#NoelYap: I can't comment on Hristo's answer, but it's the correct answer. The missing detail is that importing EnvInject only works if your job has the 'Flow run needs a workspace' option selected.
A simpler version based on Michael's solution
import hudson.model.*
def setEnvVariable(final String key, final String value) {
Thread.currentThread().getCurrentExecutable()
.addAction(new ParametersAction(new StringParameterValue(key, value))
);
}
setEnvVariable("THIS_ENV_VAR", "Hello World")
The variable is then available in the post-build actions.
An old question I know, but this is how i get around it. The beautiful part is this code works well in Jenkins both within the system evaluated groovy and within the Build FLow Job DSL.
Just drop the import statements when running it from the System Groovy job/console as they are already available.
As a bonus this class also helps get the environment variable regardless of the Groovy context. Still in the build flow you should favor build.environment.get it is more natural.
Note: 'Flow run needs a workspace' should be checked.
//Import the required bindings for a Build Flow DSL
import hudson.model.AbstractBuild
import hudson.model.EnvironmentContributingAction
import hudson.EnvVars
//Drop the above if running with a System Groovy Console/Job
class Job {
static def getVariable(String key) {
def config = new HashMap()
def thr = Thread.currentThread()
def build = thr?.getCurrentExecutable()
def envVarsMap = build.parent.builds[0].properties.get("envVars")
config.putAll(envVarsMap)
return config.get(key)
}
static def setVariable(String key, String value) {
def build = Thread.currentThread().getCurrentExecutable()
def action = new VariableInjectionAction(key, value)
build.addAction(action)
build.getEnvironment()
}
static def setVariable(String key, Integer value) {
setVariable(key, value.toString())
}
}
class VariableInjectionAction implements EnvironmentContributingAction {
private String key
private String value
public VariableInjectionAction(String key, String value) {
this.key = key
this.value = value
}
public void buildEnvVars(AbstractBuild build, EnvVars envVars) {
if (envVars != null && key != null && value != null) {
envVars.put(key, value);
}
}
public String getDisplayName() { return "VariableInjectionAction"; }
public String getIconFileName() { return null; }
public String getUrlName() { return null; }
}
Job.setVariable("THIS_ENV_VAR", "Hello World")
Will set the environment variable THIS_ENV_VAR to "Hello World"
Background:
I have grails 1.3.7 application which uses g:createLink and g:link on many pages.
Recently I decided to make big change in url mappings - introduce preceding path element.
Currently I have: /$controller/$action?/$id?
But want to have: /$regionId/$controller/$action?/$id?
It was easy to change urlMappings, but I can't figure out how to easily change the behavior how links are built throught the application.
Basically, I don't want to go through each page and change links. But want to do this in one place.
Question
How to override ApplicationTagLib#createLink functionality so grails will use this implementation without the need of changes pages which use this tag (or function)?
Any help greatly appriciated!
I had a smilar problem. Actually you can decorate g:link tag like this.
1) TagLib
import org.codehaus.groovy.grails.plugins.web.taglib.*
class OverrideDefaultTagLib {
static namespace = "g"
def link = { attrs, body ->
def c = "1" // Get it from session or somewhere else
if (attrs.params) {
attrs.params.put("region", c)
} else {
attrs.params = [region: c]
}
def applicationTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
applicationTagLib.link.call(attrs, body)
}
}
}
2) add to UrlMappings.groovy
"/$region/$controller/$action?/$id?"{}
I was unable to solve this problem in terms of OOP. I mean I can't find way how to override closure. I tried several approaches, but with no success. And documentation says that you can't override closure, you can only replace it with new implementation (please correct me if I wrong).
But (!) I was able to solve task by copy-pasting source code of ApplicationTagLib#createLink method.
I think this is brutal solution, but after 8 hours of fighting with this simple task - it's acceptable.
So finally all I need to do - is define this class, grails will immediately use it for link generation (for all views, no need to change their code):
import java.text.SimpleDateFormat;
import groovy.time.*;
import java.text.*;
import org.codehaus.groovy.grails.commons.GrailsControllerClass
import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib;
import org.codehaus.groovy.grails.web.mapping.UrlCreator
import org.codehaus.groovy.grails.commons.ControllerArtefactHandler
import org.springframework.web.context.request.RequestContextHolder
class OverrideTagLib extends ApplicationTagLib {
def createLink = { attrs ->
// get value for regionId parameter
def regionId = regionIdFinderService.currentRegionId
// add cutsom regionId parameter
if (attrs) {
if (attrs.params)
attrs.params.put("regionId", regionId);
else {
attrs.params = ["regionId":regionId];
}
}
// process
def writer = getOut()
// prefer URI attribute
if (attrs.uri) {
writer << handleAbsolute(attrs)
writer << attrs.uri.toString()
}
else {
// prefer a URL attribute
def urlAttrs = attrs
if (attrs.url instanceof Map) {
urlAttrs = attrs.remove('url').clone()
}
else if (attrs.url) {
urlAttrs = attrs.remove('url').toString()
}
if (urlAttrs instanceof String) {
if (useJsessionId) {
writer << response.encodeURL(urlAttrs)
}
else {
writer << urlAttrs
}
}
else {
def controller = urlAttrs.containsKey("controller") ? urlAttrs.remove("controller")?.toString() : controllerName
def action = urlAttrs.remove("action")?.toString()
if (controller && !action) {
GrailsControllerClass controllerClass = grailsApplication.getArtefactByLogicalPropertyName(ControllerArtefactHandler.TYPE, controller)
String defaultAction = controllerClass?.getDefaultAction()
if (controllerClass?.hasProperty(defaultAction)) {
action = defaultAction
}
}
def id = urlAttrs.remove("id")
def frag = urlAttrs.remove('fragment')?.toString()
def params = urlAttrs.params && urlAttrs.params instanceof Map ? urlAttrs.remove('params') : [:]
def mappingName = urlAttrs.remove('mapping')
if (mappingName != null) {
params.mappingName = mappingName
}
if (request['flowExecutionKey']) {
params."execution" = request['flowExecutionKey']
}
if (urlAttrs.event) {
params."_eventId" = urlAttrs.remove('event')
}
def url
if (id != null) params.id = id
def urlMappings = applicationContext.getBean("grailsUrlMappingsHolder")
UrlCreator mapping = urlMappings.getReverseMapping(controller,action,params)
// cannot use jsessionid with absolute links
if (useJsessionId && !attrs.absolute) {
url = mapping.createURL(controller, action, params, request.characterEncoding, frag)
def base = attrs.remove('base')
if (base) writer << base
writer << response.encodeURL(url)
}
else {
url = mapping.createRelativeURL(controller, action, params, request.characterEncoding, frag)
writer << handleAbsolute(attrs)
writer << url
}
}
}
}
}
add regionId to params in createLink and g:link and grails is smart enough to match your urlmappings. i.e
${createLink(controller:'c',action:'a',id:1,params:[regionId:2])}
I don't know how to use binding with closures in Groovy. I wrote a test code and while running it, it said, missing method setBinding on the closure passed as parameter.
void testMeasurement() {
prepareData(someClosure)
}
def someClosure = {
assertEquals("apple", a)
}
void prepareData(testCase) {
def binding = new Binding()
binding.setVariable("a", "apple")
testCase.setBinding(binding)
testCase.call()
}
This works for me with Groovy 1.7.3:
someClosure = {
assert "apple" == a
}
void testMeasurement() {
prepareData(someClosure)
}
void prepareData(testCase) {
def binding = new Binding()
binding.setVariable("a", "apple")
testCase.setBinding(binding)
testCase.call()
}
testMeasurement()
In this script example, the setBinding call is setting a in the scripts binding (as you can see from the Closure documentation, there is no setBinding call). So after the setBinding call, you can call
println a
and it will print out "apple"
So to do this in the class, you can set the delegate for the closure (the closure will revert back to this delegate when a property cannot be found locally) like so:
class TestClass {
void testMeasurement() {
prepareData(someClosure)
}
def someClosure = { ->
assert "apple" == a
}
void prepareData( testCase ) {
def binding = new Binding()
binding.setVariable("a", "apple")
testCase.delegate = binding
testCase.call()
}
}
And it should grab the value fro a from the delegate class (in this case, a binding)
This page here goes through the usage of delegate and the scope of variables in Closures
Indeed, instead of using a Binding object, you should be able to use a simple Map like so:
void prepareData( testCase ) {
testCase.delegate = [ a:'apple' ]
testCase.call()
}
Hope it helps!
This is really strange behaviour: removing the "def" in front of someClosure declaration makes the script work in JDK1.6 Groovy:1.7.3
Update: This was posted in the answer above. My mistake to repeat it.
Update: Why it works? Without a def first line is taken as a property assignment which calls setProperty and makes the variable available in binding, which is resolved later.
a def should have worked as well as per (http://docs.codehaus.org/display/GROOVY/Groovy+Beans)
someClosure = {
assert "apple", a
print "Done"
}
void testMeasurement() {
prepareData(someClosure)
}
void prepareData(testCase) {
def binding = new Binding()
binding.setVariable("a", "apple")
testCase.setBinding(binding)
testCase.call()
}
testMeasurement()
I could reproduce the problem you mention by following code. But i am not sure if this is the correct way to use Binding. GroovyDocs says they are to be used with scripts. Could you point me to documentation which suggests such usage of Binding with Closures.
class TestBinding extends GroovyTestCase {
void testMeasurement() {
prepareData(someClosure)
}
def someClosure = {
assertEquals("apple", a)
}
void prepareData(testCase) {
def binding = new Binding()
binding.setVariable("a", "apple")
//this.setBinding(binding)
testCase.setBinding(binding)
testCase.call()
}
}
This was answered on groovy mailing list:
In a script, def foo will create a local variable, not a property
(private field + getter/setter).
Think of a script a bit like if it's the body of a run() or main() method.
That's where and how you can define local variables.