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

Add support for Workflows when loading comment files #682

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ job('downstreamJob') {
}
```

### Pipeline support

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, in the above dsl sample code there is no way to use the comment file. do you know the syntax for this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like #637 is needed for dsl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it'd be really nice to have this.


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)
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
<artifactId>script-security</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>1.14.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>1.14.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,9 +51,50 @@ 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);
FilePath workspace;
FilePath path;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like these don't need to be declared in the outerscope. it's used within each if block anyway. workspace isn't even used in your if block.


// 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) {
nosmo marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}

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.
workspace = ((Build<?, ?>) build).getWorkspace();
path = workspace.child(scriptFilePathResolved);

if (path.exists()) {
content = path.readToString();
Expand Down