Skip to content

Commit

Permalink
JENKINS-13208 Use EnvVarsResolver for groovy scripts
Browse files Browse the repository at this point in the history
Get the environment variables for groovy scripts via the EnvVarsResolver
  • Loading branch information
edalquist committed Mar 23, 2012
1 parent 14b1178 commit 726ad01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -113,16 +116,24 @@ public Action[] invoke(File f, VirtualChannel channel) throws IOException, Inter
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;
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;

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

/**
* @author Gregory Boissinot
Expand All @@ -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);
Expand All @@ -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.");
}
Expand All @@ -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.