Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user defined classpath #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/groovy/com/cloudbees/plugins/flow/FlowDSL.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import static hudson.model.Result.SUCCESS

public class FlowDSL {

def void executeFlowScript(FlowRun flowRun, String dsl, BuildListener listener) {
def void executeFlowScript(FlowRun flowRun, String dsl, String classpath, BuildListener listener) {
// Retrieve the upstream build if the flow was triggered by another job
Run upstream = null;
flowRun.causes.each{ cause ->
Expand Down Expand Up @@ -77,7 +77,20 @@ public class FlowDSL {
ic.addStaticStars(Result.class.name)
cc.addCompilationCustomizers(ic)

ClosureScript dslScript = (ClosureScript)new GroovyShell(Jenkins.instance.pluginManager.uberClassLoader,new Binding(),cc).parse(dsl)
def classpathList = []

if (classpath?.trim()){
classpath.eachLine {
def pathURL = pathToURL(it)
if (pathURL != null){
classpathList.add(pathURL)
}
}
}

def uberLoader = Jenkins.instance.pluginManager.uberClassLoader
def extendedLoader = new URLClassLoader(classpathList as URL[], uberLoader);
ClosureScript dslScript = (ClosureScript)new GroovyShell(extendedLoader,new Binding(),cc).parse(dsl)
dslScript.setDelegate(flow);

try {
Expand All @@ -99,6 +112,18 @@ public class FlowDSL {
}
}

def pathToURL(path){
try {
return new URL(path);
} catch (MalformedURLException x) {
try{
return new File(path).toURI().toURL();
}catch (MalformedURLException x1){
return null
}
}
}

private void killRunningJobs(FlowRun flowRun, BuildListener listener) {
flowRun.state.result = Executor.currentExecutor().abortResult();
Executor.currentExecutor().recordCauseOfInterruption(flowRun, listener);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/cloudbees/plugins/flow/BuildFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class BuildFlow extends Project<BuildFlow, FlowRun> implements TopLevelIt

private String dsl;
private String dslFile;

private String classpath;
private boolean buildNeedsWorkspace;


Expand All @@ -82,6 +82,14 @@ public void setDsl(String dsl) {
this.dsl = dsl;
}

public String getClasspath() {
return classpath;
}

public void setClasspath(String classpath) {
this.classpath = classpath;
}

public boolean getBuildNeedsWorkspace() {
return buildNeedsWorkspace;
}
Expand All @@ -105,6 +113,7 @@ protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
this.buildNeedsWorkspace = json.containsKey("buildNeedsWorkspace");
if (Jenkins.getInstance().hasPermission(Jenkins.RUN_SCRIPTS)) {
this.dsl = json.getString("dsl");
this.classpath = json.getString("classpath");
if (this.buildNeedsWorkspace) {
JSONObject o = json.getJSONObject("buildNeedsWorkspace");
this.dslFile = Util.fixEmptyAndTrim(o.getString("dslFile"));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/cloudbees/plugins/flow/FlowListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public synchronized void onStarted(Run<?, ?> startedBuild,
List<Cause> causes = startedBuild.getCauses();
for (Cause cause : causes) {
if (cause instanceof FlowCause) {
((FlowCause) cause).getAssociatedJob().buildStarted(
startedBuild);
((FlowCause) cause).getAssociatedJob().buildStarted(startedBuild);
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/cloudbees/plugins/flow/FlowRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class FlowRun extends Build<BuildFlow, FlowRun> {

private String dsl;
private String dslFile;
private String classpath;

private boolean buildNeedsWorkspace;

Expand Down Expand Up @@ -90,6 +91,7 @@ private void setup(BuildFlow job) {
}
this.dsl = job.getDsl();
this.dslFile = job.getDslFile();
this.classpath = job.getClasspath();
this.buildNeedsWorkspace = job.getBuildNeedsWorkspace();
startJob.buildStarted(this);
jobsGraph.addVertex(startJob);
Expand Down Expand Up @@ -150,20 +152,22 @@ public synchronized void addBuild(JobInvocation job) throws ExecutionException,
@Override
public void run() {
if (buildNeedsWorkspace) {
execute(new BuildWithWorkspaceRunnerImpl(dsl, dslFile));
execute(new BuildWithWorkspaceRunnerImpl(dsl, dslFile, classpath));
} else {
execute(new FlyweightTaskRunnerImpl(dsl));
execute(new FlyweightTaskRunnerImpl(dsl,classpath));
}
}

protected class BuildWithWorkspaceRunnerImpl extends BuildExecution {

private final String dsl;
private final String dslFile;
private final String classpath;

public BuildWithWorkspaceRunnerImpl(String dsl, String dslFile) {
public BuildWithWorkspaceRunnerImpl(String dsl, String dslFile, String classpath) {
this.dsl = dsl;
this.dslFile = dslFile;
this.classpath = classpath;
}

protected Result doRun(BuildListener listener) throws Exception {
Expand All @@ -175,9 +179,9 @@ protected Result doRun(BuildListener listener) throws Exception {
if (dslFile != null) {
listener.getLogger().printf("[build-flow] reading DSL from file '%s'\n", dslFile);
String fileContent = getWorkspace().child(dslFile).readToString();
new FlowDSL().executeFlowScript(FlowRun.this, fileContent, listener);
new FlowDSL().executeFlowScript(FlowRun.this, fileContent, classpath, listener);
} else {
new FlowDSL().executeFlowScript(FlowRun.this, dsl, listener);
new FlowDSL().executeFlowScript(FlowRun.this, dsl, classpath, listener);
}
} finally {
boolean failed=false;
Expand Down Expand Up @@ -208,15 +212,17 @@ public void cleanUp(BuildListener listener) throws Exception {
protected class FlyweightTaskRunnerImpl extends RunExecution {

private final String dsl;
private final String classpath;

public FlyweightTaskRunnerImpl(String dsl) {
public FlyweightTaskRunnerImpl(String dsl, String classpath) {
this.dsl = dsl;
this.classpath = classpath;
}

@Override
public Result run(BuildListener listener) throws Exception, RunnerAbortedException {
setResult(SUCCESS);
new FlowDSL().executeFlowScript(FlowRun.this, dsl, listener);
new FlowDSL().executeFlowScript(FlowRun.this, dsl, classpath, listener);
return getState().getResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<j:getStatic var="permission" className="hudson.model.Hudson" field="RUN_SCRIPTS"/>
<f:textarea class="fixed-width" readonly="${h.hasPermission(it,permission) ? null : 'readonly'}" codemirror-mode="clike" codemirror-config="mode: 'text/x-groovy', lineNumbers: true, matchBrackets: true, onBlur: function(editor){editor.save()}" checkMethod="POST" />
</f:entry>
<f:advanced>
<f:entry title="${%Classpath}" field="classpath">
<f:expandableTextbox/>
</f:entry>
</f:advanced>
</f:section>

<p:config-publishers2 />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
~ The MIT License
~
~ Copyright (c) 2013, CloudBees, Inc., Nicolas De Loof.
~ Cisco Systems, Inc., a California corporation
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<div>
Specify additional classpath here. Each line is one class path item.Both URL and path are supported
</div>
</j:jelly>