Skip to content
Browse files
Fix JENKINS-14056
  • Loading branch information
gboissinot committed Jun 17, 2012
1 parent c70d2fe commit d295b675c16fabc939f3453e7cc79c1498c8f681
Showing 3 changed files with 111 additions and 0 deletions.
@@ -0,0 +1,76 @@
package org.jenkinsci.plugins.sharedobjects.type;

import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.model.AbstractBuild;
import org.jenkinsci.plugins.sharedobjects.MultipleSharedObjectType;
import org.jenkinsci.plugins.sharedobjects.SharedObjectException;
import org.jenkinsci.plugins.sharedobjects.SharedObjectType;
import org.jenkinsci.plugins.sharedobjects.SharedObjectTypeDescriptor;
import org.jenkinsci.plugins.sharedobjects.service.SharedObjectLogger;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.HashMap;
import java.util.Map;

* @author Gregory Boissinot
public class GroovySharedObjectType extends MultipleSharedObjectType {

private String content;

public GroovySharedObjectType(String name, String profiles, String content) {
super(name, profiles);
this.content = content;

public String getContent() {
return content;

public Map<String, String> getEnvVars(AbstractBuild build, SharedObjectLogger logger) throws SharedObjectException {
if (content == null) {
return new HashMap<String, String>();

if (content.trim().length() == 0) {
return new HashMap<String, String>();
}"Evaluation the following Groovy script content: \n%s\n", content));
GroovyShell shell = new GroovyShell();
Object groovyResult = shell.evaluate(content);
if (groovyResult != null && !(groovyResult instanceof Map)) {
throw new SharedObjectException("The evaluated Groovy script must return a Map object.");

Map<String, String> result = new HashMap<String, String>();
if (groovyResult == null) {
return result;

for (Map.Entry entry : (((Map<Object, Object>) groovyResult).entrySet())) {
result.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
return result;


public static class GroovySharedObjectTypeDescriptor extends SharedObjectTypeDescriptor {

public String getDisplayName() {
return "Groovy script";

public Class<? extends SharedObjectType> getType() {
return GroovySharedObjectType.class;
@@ -0,0 +1,22 @@
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:entry title="${%Type}">

<f:entry title="${%Name}"
<f:textbox name="name" value="${}"/>

<f:entry title="${%Profiles}"
<f:textbox name="profiles" value="${instance.profiles}"/>

<f:entry title="${%Groovy script}"
<f:textarea name="content" value="${instance.content}"/>

@@ -0,0 +1,13 @@
Evaluate a Groovy script and inject a map result.<br/>
The groovy script must return a map Java object.<br/>
For example, the following script injects the SOMEVAR environment variable with the 'someValue' value:<br/>
def map = [SOMEVAR: "someValue"] <br/>
return map <br/>
} <br/>

0 comments on commit d295b67

Please sign in to comment.