-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split GroovyScript for agent and controller
Make GroovyScript simpler by separating the parts meant only for the controller into a new class that extends GroovyScript. This also makes the intention of each class and the differences between them clearer from the names themselves.
- Loading branch information
Showing
4 changed files
with
65 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jenkinsci/plugins/scriptler/util/ControllerGroovyScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.jenkinsci.plugins.scriptler.util; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import groovy.lang.GroovyShell; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.TaskListener; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.scriptler.config.Parameter; | ||
|
||
import java.util.Collection; | ||
|
||
public class ControllerGroovyScript extends GroovyScript { | ||
private static final long serialVersionUID = 1L; | ||
private transient final AbstractBuild<?, ?> build; | ||
private transient final Launcher launcher; | ||
|
||
/** | ||
* This constructor can only be used when the script is executed on the controller, because launcher and build can not be transferred to an agent and therefore the execution will fail | ||
* @param script the script to be executed | ||
* @param parameters the parameters to be passed to the script | ||
* @param failWithException should the job fail with an exception | ||
* @param listener access to logging via listener | ||
* @param launcher the launcher | ||
* @param build the current build | ||
*/ | ||
public ControllerGroovyScript(String script, @NonNull Collection<Parameter> parameters, boolean failWithException, TaskListener listener, Launcher launcher, AbstractBuild<?, ?> build) { | ||
super(script, parameters, failWithException, listener); | ||
this.build = build; | ||
this.launcher = launcher; | ||
} | ||
|
||
@Override | ||
public ClassLoader getClassLoader() { | ||
return Jenkins.get().getPluginManager().uberClassLoader; | ||
} | ||
|
||
@Override | ||
protected void setShellVariables(@NonNull GroovyShell shell) { | ||
super.setShellVariables(shell); | ||
if (build != null) { | ||
shell.setVariable("build", build); | ||
} | ||
if (launcher != null) { | ||
shell.setVariable("launcher", launcher); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters