Skip to content
Permalink
Browse files

JENKINS-13208 Use EnvVarsResolver for groovy scripts

Get the environment variables for groovy scripts via the EnvVarsResolver
  • Loading branch information
edalquist committed Mar 23, 2012
1 parent 14b1178 commit 726ad01dc8d9603f7e8c6c1b93c99cc8e7138332
@@ -6,6 +6,9 @@
import hudson.Util;
import hudson.model.*;
import hudson.remoting.VirtualChannel;

import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.service.EnvVarsResolver;
import org.jenkinsci.lib.xtrigger.XTriggerDescriptor;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.scripttrigger.AbstractTrigger;
@@ -113,16 +116,24 @@ protected String getDefaultMessageCause() {
protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws ScriptTriggerException {

GroovyScriptTriggerExecutor executor = getGroovyScriptTriggerExecutor(log);

EnvVarsResolver envVarsResolver = new EnvVarsResolver();
Map<String, String> envVars;
try {
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, pollingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
}

if (groovyExpression != null) {
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, getGroovyExpression());
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, getGroovyExpression(), envVars);
if (evaluationSucceed) {
return true;
}
}

if (groovyFilePath != null) {
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, groovyFilePath);
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, groovyFilePath, envVars);
if (evaluationSucceed) {
return true;
}
@@ -10,6 +10,7 @@
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;

import java.io.IOException;
import java.util.Map;

/**
* @author Gregory Boissinot
@@ -20,16 +21,17 @@ public GroovyScriptTriggerExecutor(XTriggerLog log) {
super(log);
}

public boolean evaluateGroovyScript(Node executingNode, final String scriptContent) throws ScriptTriggerException {
public boolean evaluateGroovyScript(Node executingNode, final String scriptContent, final Map<String, String> envVars) throws ScriptTriggerException {

if (scriptContent == null) {
throw new NullPointerException("The script content object must be set.");
}
try {
return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
public Boolean call() throws ScriptTriggerException {
final String groovyExpressionResolved = Util.replaceMacro(scriptContent, EnvVars.masterEnvVars);
log.info(String.format("Evaluating the groovy script: \n %s", scriptContent));
log.info("Replacing script vars with: " + envVars);
final String groovyExpressionResolved = Util.replaceMacro(scriptContent, envVars);
log.info(String.format("Evaluating the groovy script: \n %s", groovyExpressionResolved));
GroovyShell shell = new GroovyShell();
//Evaluate the new script content
Object result = shell.evaluate(groovyExpressionResolved);
@@ -44,7 +46,7 @@ public Boolean call() throws ScriptTriggerException {
}
}

public boolean evaluateGroovyScriptFilePath(Node executingNode, String scriptFilePath) throws ScriptTriggerException {
public boolean evaluateGroovyScriptFilePath(Node executingNode, String scriptFilePath, final Map<String, String> envVars) throws ScriptTriggerException {
if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
}
@@ -54,7 +56,7 @@ public boolean evaluateGroovyScriptFilePath(Node executingNode, String scriptFil
}

String scriptContent = getStringContent(executingNode, scriptFilePath);
return evaluateGroovyScript(executingNode, scriptContent);
return evaluateGroovyScript(executingNode, scriptContent, envVars);
}

}

0 comments on commit 726ad01

Please sign in to comment.
You can’t perform that action at this time.