diff --git a/README.md b/README.md index 4bbb8f056..06efea860 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,25 @@ job('downstreamJob') { } ``` +### Pipeline support + +The plugin supports use with the Jenkins pipeline plugin, but pipeline scripts that use the Comment File trigger should ensure that they stash and unstash files appropriately as stages triggered on remote nodes such as containers will be inaccessible unless explicitly transferred to master at the end of a pipeline using similar to the following: + +``` +node(myNode) { + stage('plan'){ + sh: "command > outfile.txt" + stash name: "my-stash", includes: "outfile.txt" + } +} +node("master") { + unstash "my-stash" +} + +``` + +If the job is configured appropriately, the contents of outfile.txt will be posted as a comment to the PR. + ### Updates See [CHANGELOG](CHANGELOG.md) diff --git a/pom.xml b/pom.xml index ed96f710c..168de6407 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,16 @@ script-security 1.25 + + org.jenkins-ci.plugins.workflow + workflow-api + 1.14.2 + + + org.jenkins-ci.plugins.workflow + workflow-job + 1.14.2 + org.codehaus.groovy groovy-all diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbCommentFile.java b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbCommentFile.java index f167a709f..ce00ae074 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbCommentFile.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbCommentFile.java @@ -11,8 +11,17 @@ import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension; import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor; import org.jenkinsci.plugins.ghprb.extensions.GhprbProjectExtension; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; + +import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; +import org.jenkinsci.plugins.workflow.graph.FlowNode; +import org.jenkinsci.plugins.workflow.graph.BlockStartNode; +import org.jenkinsci.plugins.workflow.flow.FlowExecution; +import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; + import org.kohsuke.stapler.DataBoundConstructor; + import java.io.File; import java.io.IOException; @@ -42,9 +51,47 @@ public String postBuildComment(Run build, TaskListener listener) { try { String content = null; - if (build instanceof Build) { - final FilePath workspace = ((Build) build).getWorkspace(); - final FilePath path = workspace.child(scriptFilePathResolved); + // On custom pipelines, build will be an instance of WorkflowRun + if (build instanceof WorkflowRun) { + FlowExecution exec = ((WorkflowRun) build).getExecution(); + if (exec == null) { + listener.getLogger().println("build was instanceof WorkflowRun but executor was null"); + } else { + // We walk the execution flow as a run can have multiple workspaces + FlowGraphWalker w = new FlowGraphWalker(exec); + for (FlowNode n : w) { + if (n instanceof BlockStartNode) { + WorkspaceAction action = n.getAction(WorkspaceAction.class); + if (action != null) { + String node = action.getNode().toString(); + String nodepath = action.getPath().toString(); + listener.getLogger().println("Remote path is " + node + ":" + nodepath + "\n"); + + if (action.getWorkspace() == null) { + // if the workspace returns null, the workspace either isn't here or it doesn't + // exist - in that case, we fail over to trying to find the comment file locally. + continue; + } + + FilePath path = action.getWorkspace().child(scriptFilePathResolved); + + if (path.exists()) { + content = path.readToString(); + } else { + listener.getLogger().println( + "Didn't find comment file in workspace at " + path.absolutize().getRemote() + + ", falling back to file operations on master.\n" + ); + } + + } + } + } + } + } else if (build instanceof Build) { + // When using workers on hosts other than master, we simply get the workspace here. + FilePath workspace = ((Build) build).getWorkspace(); + FilePath path = workspace.child(scriptFilePathResolved); if (path.exists()) { content = path.readToString();